In C#, Math.FusedMultiplyAdd(x, y, z) calculates x * y + z, but it does the whole thing as one operation with one rounding. With the normal way, x * y + z, the computer rounds twice: once after the multiplication and once after the addition. Each rounding can lose a tiny bit of accuracy.
Think of weighing flour in two steps and rounding the number written down after each step, instead of weighing everything once. The "fused" version keeps the full precision of the product until the addition is done. Modern processors have a special instruction for this (called FMA), so it is also fast. It is useful in maths libraries, graphics, and for evaluating polynomials. You do not need it for everyday code, and for plain whole numbers the answer is the same as x * y + z.
This is a static member of the static class System.Math. You call it with the class name, such as Math.FusedMultiplyAdd(...). There is no Math object to create. All the work is done by the method you call, and it never changes your variables. Math.FusedMultiplyAdd was added in .NET Core 3.0. It is not in classic .NET Framework.
Syntax
Pass the two numbers to multiply, then the number to add. All three are double values.
C#
double a = Math.FusedMultiplyAdd(2, 3, 4); // 2 * 3 + 4 = 10
double b = Math.FusedMultiplyAdd(10, 10, 1); // 101
double c = 2 * 3 + 4; // same answer, two roundings
Infinity times 0 is not defined, so the answer is NaN.
For small whole numbers, the result is exactly the same as x * y + z. The difference only shows for numbers with many digits, where the product does not fit exactly in a double. The method works on double, and MathF.FusedMultiplyAdd does the same for float. It never throws.
Library declarations
C#
public static double FusedMultiplyAdd(double x, double y, double z);
Parameters
Parameter
Description
x
Required. The first number to multiply.
y
Required. The second number to multiply.
z
Required. The number to add to the product.
Return Value
Return Value
Description
double
The value of x * y + z, rounded once. NaN for cases with no answer, such as infinity times zero.
C# Examples
Example 1 — Multiply and Add in One Call
Try small whole numbers, where the fused result is simply x * y + z.
C#
using System;
class Program {
static void Main() {
Console.WriteLine(Math.FusedMultiplyAdd(2, 3, 4));
Console.WriteLine(Math.FusedMultiplyAdd(10, 10, 1));
Console.WriteLine(Math.FusedMultiplyAdd(-2, 5, 3));
Console.WriteLine(Math.FusedMultiplyAdd(0, 100, 5));
}
}
Output
10
101
-7
5
How It Works
2 x 3 + 4. The product is 6 and the sum is 10.
10 x 10 + 1. The product is 100, the sum is 101.
A negative. -2 x 5 is -10, and -10 + 3 is -7.
Zero. Zero times anything is zero, so the answer is just 5. For numbers like these, the ordinary x * y + z gives the same result.
Example 2 — Evaluate a Polynomial with Horner's Method
Evaluate 3x squared + 2x + 1 at x = 4 using a chain of fused steps.
C#
using System;
class Program {
static void Main() {
double x = 4;
double result = 3;
result = Math.FusedMultiplyAdd(result, x, 2);
Console.WriteLine(result);
result = Math.FusedMultiplyAdd(result, x, 1);
Console.WriteLine(result);
Console.WriteLine(3 * x * x + 2 * x + 1);
}
}
Output
14
57
57
How It Works
Horner's idea. The polynomial 3x² + 2x + 1 can be written as (3x + 2)x + 1. Each step is "multiply, then add", which is exactly what FusedMultiplyAdd does.
First step. Start with the first coefficient, 3. Then 3 x 4 + 2 is 14.
Second step. 14 x 4 + 1 is 57. That is the value of the polynomial at x = 4.
Check. Writing the polynomial out the long way gives 57 too. With longer polynomials, the fused chain uses fewer steps and keeps more accuracy.
Example 3 — One Rounding Instead of Two
Build a case where the normal multiply-then-add loses a tiny amount that the fused method keeps (needs a CPU or library that supports exact FMA, which .NET provides).
C#
using System;
class Program {
static void Main() {
double a = Math.BitIncrement(1.0);
double z = -(1.0 + 2 * Math.ScaleB(1, -52));
Console.WriteLine(a * a + z);
Console.WriteLine(Math.FusedMultiplyAdd(a, a, z) > 0);
Console.WriteLine(double.IsNaN(Math.FusedMultiplyAdd(double.PositiveInfinity, 0, 1)));
}
}
Output
0
True
True
How It Works
Setup.a is the number just above 1, which is 1 + 2-52. Squared exactly, it is 1 + 2-51 + 2-104. The last piece is far too small for a double to keep.
The normal way.a * a is rounded first, which drops that tiny piece. Then adding z (minus 1 + 2-51) leaves exactly 0.
The fused way.FusedMultiplyAdd keeps the tiny piece until after the addition. The answer is 2-104, a very small but positive number, so the test prints True.
Undefined cases. Infinity times 0 has no value, so FusedMultiplyAdd(Infinity, 0, 1) is NaN, and double.IsNaN reports True.
Frequently Asked Questions
It computes x * y + z as one operation with a single rounding. That makes it a little more accurate than multiplying and then adding, which rounds twice.
It was added in .NET Core 3.0, so it is in modern .NET. It is not in classic .NET Framework.
It can be, because many processors have a single FMA instruction. The main benefit is accuracy, and the speed depends on the hardware.
Use it in numeric code such as polynomial evaluation, dot products, and maths libraries, where tiny rounding errors add up. For everyday arithmetic, x * y + z is fine.
No. Math is a static class, so you call Math.FusedMultiplyAdd(...) straight on the class name. You cannot write new Math(), and there is no variable to keep.