This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 7 years ago.
below code is weird. when i debug it, i can see both "result" and "round" are 5, but output is "false". Any idea?
double result = Math.Log (243, 3); // 5
double round = Math.Round (result); // 5
Console.WriteLine (result == round);
Comparison of floating point with equality operator could have loss of precision while rounding values. You can fix this by compare a difference with epsilon. Use a Tolerance like this:
Console.WriteLine(Math.Abs(result - round) < 0.0000001); // True
Related
This question already has answers here:
Is floating point math broken?
(31 answers)
Why are floating point numbers inaccurate?
(5 answers)
Closed 1 year ago.
My code snippet as below.
int nStdWorkDays;
double dbStdWorkDaysMin;
nStdWorkDays = 21;
dbStdWorkDaysMin = nStdWorkDays * 0.9;
Here, I found the value of dbStdWorkDaysMin is 18.900000000000002 (other than
18.9) when I debugged and added a watch in Visual Studio.
The error resulted in that '18.9 < dbStdWorkDaysMin' is true!
I wonder why this is happening. What are the similar traps? How can we get the correct calculation result?
Thank you all in advance.
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 3 years ago.
Why I get incorrect result.(for example n=3.83 I expect temp=0.83 but when trace my code temp=0.83000000000000007)
notice:temp,m and n are double.
n = double.Parse(Console.ReadLine());
m = Math.Floor(n);
Console.WriteLine(m);
temp = n - m;
The reason is precision of double. The store numbers only approximately. Therefore, you can have difference in lower digits. Usually it's not critical, but bad when:
You want an exact answer.
The error accumulates.
This question already has answers here:
Possible Loss of Fraction
(5 answers)
Closed 4 years ago.
I'm trying to print out this integer variable which in the case of my test numbers should be 5,2333 but only 5 gets printed.
int finalTimeInt = ((N * 60 + n) / 2) / 60;
Console.Writeline(finalTimeInt);
As stated already I'm expecting an output of 5,2333 but am only getting a 5.
Integers can't hold decimals. If you want a variable to store decimals, you should be using a floating-point type, like float, or double if you want more accuracy.
This question already has answers here:
Floating point inaccuracy examples
(7 answers)
Closed 6 years ago.
double a=60.5;
double a=60.1;
Console.WriteLine(a-b);
Return value is 0.399999999999999 not 0.4
It is because you use double: double is floating point, which is not precise; decimal, on the other hand, is precise. If you change both variable to decimal, it will be the exact number.
That is why in certain domains, like financial industry, decimal is desired for accuracy and precision.
This question already has answers here:
Find number of decimal places in decimal value regardless of culture
(20 answers)
Closed 8 years ago.
If I have a number, how can I determine the number of decimals?
e.g. for 0.0001 I want to get the result 4
The duplicate suggested above is less suitable than this one because
they are taking about culture-independent code but this question is
just about decimal oriented code (i.e. after the decimal). So no need
to introduce any more overhead:
Finding the number of places after the decimal point of a Double
but they both are good threads.
You can't really. A double is a floating point precision data type, so it's never precise.
You could hack something around, using ToString:
double d = 0.994562d;
int numberOfDecimals = d.ToString(CultureInfo.InvariantCulture).Length
- d.ToString(CultureInfo.InvariantCulture).IndexOf('.')
- 1
;