fma()

[Math]
double fma(double x, double y, double z);

Description

Returns the value of x * y + z as a floating-point number.

Parameters

x: The first value to multiply.

y: The second value to multiply.

z: The value to add.

Returns

The value of x * y + z.

Example Code

The code reads the value of three potentiometers connected to pins INX, INY and INZ and places the greater of the two values on pin OUT.

void setup() {
    pinLabel(0, "INY");  // set the label of pin 0 to "INY"
    pinLabel(1, "INX");  // set the label of pin 1 to "INX"
    pinLabel(2, "OUT");  // set the label of pin 2 to "OUT"
    pinMode(2, OUTPUT);  // set pin 2 as output
    pinLabel(3, "OUT2"); // set the label of pin 3 to "OUT2"
    pinMode(3, OUTPUT);  // set pin 3 as output
    pinLabel(4, "INZ");  // set the label of pin 4 to "INZ"
    pinLabel(5, "VCC");  // set the label of pin 5 to "VCC"
    powerPin(5);         // set pin 5 as power pin
    pinLabel(6, "GND");  // set the label of pin 6 to "GND"
    groundPin(6);        // set pin 6 as ground pin
}
void loop() {   
    int val_x = analogRead(0); // read the value of the potentiometers
    int val_y = analogRead(1);
    int val_z = analogRead(4);   
    double z = fma(val_x, val_y, val_z); // calculate the multiply-add operation
    analogWrite(2, (int) z); // write the value of z to pin 2
}