Reputation: 75
When I perform the following division and cast to float, I get the following value:
float fltval = (float)(1183588296 / 65536.0); //18060.125
Performing the same division without cast gives the following double value:
double dblval = 1183588296 / 65536.0; //18060.124145507813
Can I find the nearest double value for the given float value?
When I do the following:
double nearestdbl = (double)fltval;
I get the float value itself and not the nearest double value:
//nearestdbl = 18060.125
How can I get the more accurate value (18060.124145507813), or a closer value in this case?
I want to be able to store the result in 32 bits (float) and still be able to derive the closer double value by assigning the float value to double variable.
However, the following code gives a more accurate double value:
float f = 125.32f; //125.32
double d = (double)125.32f; //125.31999969482422
Why does it find a closer value in the 2nd example and not in the 1st example?
Thanks.
Upvotes: 2
Views: 4207
Reputation: 270770
(Actually, when I run the code, I got 18060.13
instead of 18060.125
, but I will keep using the latter in my answer.)
Can I find the nearest double value for the given float value?
You seem to somehow think that the nearest double value for the float 18060.125
is 18060.124145507813
? This is not true. The nearest double value for the float 18060.125
is 18060.125
. This value can be represented by double
and float
equally accurately.
Why does casting
18060.124145507813
tofloat
gives18060.125
then?
Because the nearest float
to the double
18060.124145507813
is 18060.125
. Note that this is the other way round from your understanding. This does not imply that the nearest double
to the float
18060.125
is 18060.124145507813
, because there are many double
values in between 2 adjacent float
values.
It is impossible to go back to "the double
that you got the float
from" because when you cast to float
, you are losing information. You are converting from a 64-bit value to a 32-bit one. That information isn't going back.
Why does casting 125.32f work then?
Because float
cannot represent the number 125.32 as accurately as double
can, so when you cast to double, it tries to approximate it even further. Although it might seem float
can represent 125.32
100% accurately, that's just an illusion created by the ToString
method. Always format your floating point numbers with some kind of formatting method, e.g. string.Format
.
Upvotes: 1
Reputation: 42
When you convert double to float, the double value is rounded to the nearest float value. If the double value is too small or too large to fit into the float type, the result is zero or infinity. check out this link: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/numeric-conversions
Upvotes: 0
Reputation: 823
When converting data types, you'll always end up with the type with less precision... Consider the case when converting int to double
int intVal = (int)(10.0/4);
double dblVal = 10.0/4;
//dblVal = 2.5 --- intVal = 2
dblVal = intVal;
//dblVal = 2.00;
Bottom Line: You can't save memory and save precision at the same time...
Upvotes: 0