This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 5 years ago.
when divide in c# Answer is different and when i divide from calculator answer is different kindly solve it.
c#
double div=(double)100000/30/9;
Answer 370.37037037037038
370.37037037037038*9*30 //100000.0000000000026
in calculator
Answer 370.3703703703704
370.3703703703704*9*30 //100000
i need exact answer like a calculator.
The difference is simply one of rounding. That is not a rational number, it has a repeating sequence, and cannot be perfectly expressed in binary code. It cannot even be expressed with a finite number of digits in decimal. The only difference there is that your calculator displays fewer digits than the C# double.
The final '04' is simply your calculator rounding '038' upward.
Related
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 4 months ago.
I have a problem with conversation of double values.
In the picture Project's framework is .NET6 and i tried on .NET5 and i get same value again on x64.
Can you explain this situation to me?
And how can I get the value unchanged on x64?
Thank you.
This is expected behavior: floating point numbers have a limited number of significant digits, and a number that has a finite number of digits in their decimal representation may require infinite digits in their binary representation.
E.g., the decimal 0.1 is, in binary, 0.00011001100110011.... repeating. Storing this in a float or double, the number of digits is truncated.
Floating point numbers behave similar, but not identical to "real" numbers. This is something you should be aware of.
For financial mathematics, in C#, use the decimal type.
Here you find a good "what every developer should know" overview: https://floating-point-gui.de/.
The standard governing the most common (almost ubiquitous) implementation of floating point types is IEEE 754.
This question already has answers here:
c# string to float conversion invalid?
(3 answers)
Closed 5 years ago.
float.Parse("534818068")
returns: 534818080
I understand that there are many complications with float and decimal values. But maybe someone could explain this behaviour to me.
Thanks!
Floating point numbers have a relative precision, i.e. something like 7 or 8 digits. So only the first 7 or 8 digits are correct, independent of the actual total size of the number.
Floating point numbers are stored internally using the IEEE 754 standard (a sign, a biased exponent and a fraction).
float numbers are stored with a 32 bits representation, which means they will have a precision of 7 digits.
On the other hand, double are stored with a 64 bits representation, thus having 15-16 digits (source).
Which is why you shouldn't usually compare floats for equality for instance.
This question already has answers here:
Why is floating point arithmetic in C# imprecise?
(3 answers)
decimal vs double! - Which one should I use and when? [duplicate]
(7 answers)
Closed 6 years ago.
double sth = 250 - 249.99;
Console.WriteLine(sth);
Why does this return sth like 0.009994507, instead of 0.01?
Floating point numbers (in this case doubles) cannot represent decimal values exactly. For more info, see this page here
If you need a more accurate representation, use decimal instead.
because when you print the double you print the all double value not just the first x after point digits.
you can use String.Format to print only the first 2 numbers.
double sth = 250.00d - 249.99d;
string sthString = String.Format("{0:0.00}", sth);
Console.WriteLine(sthString);
There are a lot of decimals that have infinite binary representation. What you're experiencing is exactly this case.
For more on this topic see: http://www.exploringbinary.com/why-0-point-1-does-not-exist-in-floating-point/
This question already has answers here:
Double vs Decimal Rounding in C#
(2 answers)
Closed 7 years ago.
I am doing following equation but the result is not as expected
double dasdas = Math.Abs(3.2 - 1.9);
The result is
1.3000000000000003
However the correct result should be
1.3
What may be the reason of this?
c# 4.5.2
This is because you're using a double - which is a floating point. These by definition this cannot store the exact number.
You need to use Decimal.
Take at look at here and What Every Computer Scientist Should Read About Floating Point
Use decimal:
decimal dasdas = Math.Abs(3.2m - 1.9m);
This question already has answers here:
Round to nearest five
(5 answers)
How do you round a number to two decimal places in C#?
(15 answers)
Closed 8 years ago.
I'm trying to round the following number as follows:
6.89 <- dollars and cents
I need to use this number in calculations rounded down to the nearest 10c.
Therefore I need: 6.80
To clarify I need a way to obtain the following example results:
1.32 --> 1.30
1.55 --> 1.50
6.89 --> 6.80
I can't seen how this can be done with Round or Floor.
Just use this, I don't see any error with that, and works well with your examples.
Math.Floor (number*10)/10