I really dont unterstand why, but my double array just sometimes round my variables, even though it shouldn't. The weird thing is, that it only does this sometimes, as you see in the picture. All of the array elements should have this long precision. The Variable "WertunterschiedPerSec" is also at this precision every time, but still, if i add it to Zwischenwerte[i], then it sometimes just get less precisie, even though i dont do anything anywhere. Does anybody know why?
Asked
Active
Viewed 915 times
1
-
3`double` and `float` are floating point approximations, and as such will have rounding errors such as this for values that can't be represented exactly. `decimal` I believe is more exact, which is why it is recommended strongly when working with money values. Is this small rounding error causing issues for you? Is so, would it be solved by having a margin of error with any checks on these numbers? – St. Pat Mar 14 '19 at 19:14
-
2There are an infinite number of real numbers. There are a finite number of numbers represented by `double`. As a result, not every real number can be represented by a double (and, because it's binary, the rules are different from what your "I understand decimal numbers" mind thinks). In decimal land, how do you represent 1/3, for example? Take a look at: https://stackoverflow.com/questions/618535/difference-between-decimal-float-and-double-in-net?rq=1 – Flydog57 Mar 14 '19 at 19:15
-
`float` and `double` types are binary floating point types and they cannot represent most decimal numbers accurately. Never use them if the decimally written representation of the values matters. For that purpose use `decimal` instead. – György Kőszeg Mar 14 '19 at 19:25
2 Answers
3
I would suggest using a decimal
, but let's get into the exact details:
double
, float
and decimal
are all floating point.
The difference is double
and float
are base 2 and decimal
is base 10.
Base 2 numbers cannot accurately represent all base 10 numbers.
This is why you're seeing what appears to be "rounding".

Zer0
- 7,191
- 1
- 20
- 34
0
I would use the decimal variable instead of double because you can basically do the same functions as a double except for using the Math function. Try to use Decimals and if you need to convert than use:
Double variable = Convert.ToDouble(decimal var);
Decimals are meant for decimals so they will hold more information than a float or decimal

Mom
- 13
- 3