Incorrect result of difference two double numbers [duplicate] - c#

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.

Related

Why does the floating-point number calculation result in C# have an extra tail? [duplicate]

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.

Console.Writeline(integer); prints number without decimals even though there should be decimals [duplicate]

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.

C# Remove certain amount of numbers end of line [duplicate]

This question already has answers here:
How to limit a decimal number? [duplicate]
(6 answers)
Closed 6 years ago.
So,
I got example number: 0.00857382942837523 which length is 18, but I would like to remove from the end example 9 numbers. Thing is, that number is different every time, so I can't "predict" numbers what are going to be at the end.
//My question was marked to duplicate, but it can't be solved like that. Mine has 0.00, if I would do math.round it would give up 0...
You need Math.Truncate
decimal d= 0.00857382942837523m;
d= Math.Truncate(d * 1000000000m) / 1000000000m;
Console.WriteLine(d); // 0.008573829

Weird C# Math calculation [duplicate]

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

How to determine the # of decimals in a double? [duplicate]

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
;

Categories

Resources