Long in Float, why?

2020-04-03 03:45发布

问题:

long b = 99;  
float c = 99.0F;  
//b = c; //Error : Cannot implicitly convert type 'float' to 'long'.
c = b; // Running Successfully.  Why?

Why is there no problem regarding size of data type and implicitly converting?
The size of float and long is different as we know and which is given below...

Console.WriteLine("Long  : " + sizeof(long)); // Output --> Long : 8
Console.WriteLine("Float : " + sizeof(float));// Output --> Float: 4

回答1:

A float's range (approx ±3.4e38) is much larger than a long's range (approx. ±9.22e18), even though a long has higher precision.



回答2:

There are 2 reasons

1. Range of values (i.e. Max value).

+--------------+-----------------------------+
|  data type   |        Maximum Value        |
+--------------+-----------------------------+
|              |                             |
|   long       |     9223372036854775807     |
|              |                             |
|   float      |     3.402823E+38            |
|              |                             |
+--------------+-----------------------------+

As, maximum value of float is greater than long i.e. long is contained inside float.

So, float= long  is possible
but, long = float is not possible

2. Superiority
You can't directly assign a floating point value into a integer(a non floating ) value without an explicit conversion.

float a=90     //correct
float b=90.0f; //correct

but

long a=90     //correct
long b=90.0f; //wrong

Here, also from above example it seems that float can contain long's data, but vice versa is not possible.

EDIT: regarding size of data type see my question Is Range of value depends upon Size of datatype?



回答3:

long represent Int64 type i.e an integral number while float represents Single type i.e a floating point number. And even though the size of long is larger than that of float, it is not possible to convert from a float to an integer without loosing information.

For more information on long and float type refer msdn.



回答4:

People have mentioned the range of float and long, but a simpler point is that you can't represent most possible floating point numbers in an integer. If your float is 3.14159, the two closest values a long can represent are 3 or 4 - both completely wrong. A float can represent billions of values that lie between 3 and 4. It is also highly unlikely that a programmer would use a float for a value in the first place if they want to represent integer-like values - it would be a very poor design decision. So it's simply too dangerous to implicitly convert float->int because the data you are almost certain to lose is almost certainly important. It's not something we intentionally do very often, and when we do, we usually like to make it explicit (Math.Floor(floatValue)).

Going in the opposite direction (int -> float), a float can usually "adequately" represent an integer value. The caveat with floating point is that numbers can only be stored as an approximation - the larger the value, the less accurately it can be represented. However, the accuracy of a float is around 6 significant figures - lossy, but you'll only lose around a millionth of the original value. In many cases that will be insignificant. (In the cases where it will be significant, programmers will always be very careful about introducing the vagaries of floating point into their calculations, so it seldom causes a problem in practice). Hence, it is more convenient/useful to allow the implicit conversion in this direction, as it's something we intentionally do rather a lot.