8
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
mskfisher
  • 3,291
  • 4
  • 35
  • 48

4 Answers4

10

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.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • what is the exact length in number of digit and precision in float. – Mohammad Jahangeer Ansari Dec 04 '10 at 06:16
  • A float has 23 bits of precision. http://en.wikipedia.org/wiki/Single_precision_floating-point_format – Ignacio Vazquez-Abrams Dec 04 '10 at 06: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 04 '10 at 07: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 04 '10 at 07:42
  • if we can assign a long onto a float because 8 is larger than 4 then my dear try long in to int, because they are same,, but it'll not work why,,, – Mohammad Jahangeer Ansari Dec 04 '10 at 09:04
  • long b=999999999999999999;//18 float d=99999999999999999999999999999999999999.99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999F; // 38. – Mohammad Jahangeer Ansari Dec 04 '10 at 09:12
  • 1
    float is based on scientific notation, dedicating some bits to the exponent, while long just stores the number in base 2. It's because of this that float can store much larger numbers, but will lose more and more precision the further the exponent is from 0. – TheBoxyBear Feb 26 '21 at 01:47
6

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?

Community
  • 1
  • 1
Javed Akram
  • 15,024
  • 26
  • 81
  • 118
1

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.

Devendra D. Chavan
  • 8,871
  • 4
  • 31
  • 35
  • 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 04 '10 at 07:07
0

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.

Jason Williams
  • 56,972
  • 11
  • 108
  • 137