Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Long in Float, why?

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
like image 212
Mohammad Jahangeer Ansari Avatar asked Dec 04 '10 06:12

Mohammad Jahangeer Ansari


People also ask

Why is long a subtype of float?

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.

Can long be stored in float?

From float to long you could lose all behind the floating point so there will be no implicit cast because normally you do not want to lose this information. You may well lose information going from long to float .

Is long a float?

The long float is a K&R C first edition type that existed. It is synonymous with double . After the first standard C89/C90, long float is removed. It is not deprecated.

Is long an int or float?

long (in c) is equivalent to long int . The size of this type varies between processors and compilers, but is often, as you say, 32 bits. At 32 bits, it can represent 232 different values. Since we often want to use negative numbers, computers normally represent integers using a format called "two's complement".


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

like image 175
Ignacio Vazquez-Abrams Avatar answered Oct 20 '22 02:10

Ignacio Vazquez-Abrams