Take the 2-minute tour ×
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
share|improve this question
add comment

4 Answers

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.

share|improve this answer
what is the exact length in number of digit and precision in float. –  jak Dec 4 '10 at 6:16
A float has 23 bits of precision. en.wikipedia.org/wiki/Single_precision_floating-point_format –  Ignacio Vazquez-Abrams Dec 4 '10 at 6:27
As your sizeof() calls show, long is 8 bytes while float is 4 byes. You can assign the float value into the long because 4 is smaller than 8. You cant assign a long onto a float because 8 is larger than 4. –  IanNorton Dec 4 '10 at 7:30
@IanNorton: No, that's not it. Also, you're reading it wrong; it's allowing assignment from the long to the float, but not from the float to the long. –  Ignacio Vazquez-Abrams Dec 4 '10 at 7:42
@jak oh boy, that is a looong comment! –  gideon Dec 4 '10 at 13:09
show 2 more comments

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


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?

share|improve this answer
add comment

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.

share|improve this answer
With the exception of a few numbers, it's also not possible to convert from an integer to a float without losing information. –  ssube Dec 4 '10 at 7:07
add comment

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.

share|improve this answer
add comment

Your Answer


