0

There are some float and integer numbers and I must multiply them.

float length = 40;
float width = 25.7f;
float height = 27.6f;
int count = 361;
var result = length * width * height * count;

The output of this code is equal to 10242826

When I use a calculator, the output is: 10242580.8

Why is the result of multiplying several numbers different from the result of multiplying in the calculator?

enter image description here

Mahi
  • 1,019
  • 9
  • 19
Farzaneh Talebi
  • 835
  • 4
  • 22
  • 47
  • 4
    If I run your code, I get `10242580`. Float has a precision or 6-9 digits (see https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/floating-point-numeric-types), so the two values are equal within the precision. If you need more, use `double` or `decimal`. – Klaus Gütter Nov 02 '20 at 06:50
  • it's due to precision. using double (which has greater precision) might be what you are looking for. BTW, I consider it to a a bug if you use 'var' to assign datatypes for numerical calculations. You should be explicit. – Mitch Wheat Nov 02 '20 at 06:53
  • As for the different results that you and we are seeing, see https://stackoverflow.com/q/6683059/11683 and https://stackoverflow.com/q/90751/11683. – GSerg Nov 02 '20 at 07:00
  • what version of visual studio, .NET framework, OS? I just created a .NET Core console app. in VS 2019 using your exact code (on windows 10) and if I hover over result I get 10242581 – Mitch Wheat Nov 02 '20 at 07:00
  • @MitchWheat, Vs 2019 and .Net core 3.0. – Farzaneh Talebi Nov 02 '20 at 07:51

3 Answers3

2

The difference is just caused by the floats used in the code.
They have smaller precision.

To fix the code just replace them with doubles:

double length = 40;
double width = 25.7d; // <-Remofe "f" IMPORTANT! or use "d" instead (e.g.: 25.7d)
double height = 27.6d; // <-Remofe "f" IMPORTANT! or use "d" instead (e.g. 27.6d)
int count = 361;
var result = length * width * height * count;

This will produce the same result as in the calculator:

10242580.8

P.S. You could also use a simplifed version like this:

var length = 40;
var width = 25.7; // No need of "d", as the type is automatically set to double
var height = 27.6; // No need of "d", as the type is automatically set to double
var count = 361;
var result = length * width * height * count;
Just Shadow
  • 10,860
  • 6
  • 57
  • 75
  • 1
    Note that changing to doubles is not the universal solution, it's a workaround that works for these small numbers. Make the numbers bigger and there will be a difference with the calculator again. – GSerg Nov 02 '20 at 07:11
1

To add a different approach to @Just Shadow's excellent answer, you could also use a different measurement unit and avoid using floats or doubles:

If you have 25.7 meters, you could represent that as 2570 centimeters, which is an integer.

This technique is sometimes used when dealing with currency, so instead of having a dollar representation 1.25 you would base everything on cents, and get 125 cents

As long as you know what level of precision you need, you can do these conversions and get rid of problematic and expensive floating-point arithmetic.

Alenros
  • 816
  • 7
  • 23
1

float and double internally represent numbers in base 2. Numbers expressible in base 2 are represented precisely for float and double. So most literals with a fractional component (which are in base 10) will not be represented precisely.

But you can use decimals to get the precise values in this case, Because real literals are in base 10, decimal can precisely represent numbers such as 25.7.

MBB
  • 1,635
  • 3
  • 9
  • 19