简体   繁体   中英

C# float to double conversion

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.

(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 to float gives 18060.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, eg string.Format .

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...

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://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/numeric-conversions

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM