Converting from Single to double error

  • Thread starter Thread starter Tor Aadnevik
  • Start date Start date
T

Tor Aadnevik

Hi,
I have a problem converting values from Single to double.
eg. When the Single value 12.19 is converted to double, the result is
12.1899995803833.

Anyone know how to avoid this?

Regards Totto
 
There is the possibility of small amounts of error when dealing with
singles/doubles as they are estimations not exact values

There are really 3 options ..

1) use Math.Round to convert the value
2) use a lossless number format (i.e. BigDecimal) which operates on strings
or a fraction class
http://geekswithblogs.net/gyoung/archive/2006/05/01/76869.aspx includes
links to both
3) it seems like you might be doing monetary calculations here ... a common
pattern is to not treat things as "Dollars" but to put the value in an
integer representing pennies ... this is much faster for calculations as
well.
 
Hi Greg
Tanks for your answere.
Both the Single and the Double values are types that belongs to systems that
are out of my control.
But mybe if I use your option number 3 and multiply the Single value with a
big number so its without decimals and then convert it to double. I will
give it a test!

Tor
 
1) I meant math.round after the operation to the proper precision
2) BigDecimal is another type :) (you can find it in the J#
redistributeable).
 
The Single value is not exactly 12.19 to begin with, but the difference
is so small that the value gets rounded to 12.19 when displayed.

When you convert it to a Double, the difference between the actual value
and 12.19 remains the same (or changes very little), but as Double has
higher precision the difference becomes visible.

So the problem is not that the value changes, but that you suddenly see
how low precision the original value had.
 
Greg said:
It has to do with the way floating point numbers are stored, they are
approximated. It is not that you are just not seeing precision on the

*All* doubles are not approximated, it's just that some floating point
values cannot be represented accurately in binary. It the same reason
that some numbers cannot be accurately represented in the decimal
number system like PI or the square root of 2 or the fraction 1/3.
 
I did some testing, and I couldn't find any number where converting the
number from float to double and then back to float would not give the
exact same number as before converting.

Wouldn't that mean that there is no loss of data at all when converting
from float to double?
 
Back
Top