Question

I have a number: 94,800,620,800

Float is 4-byte data-type. Int32 is also 4-byte data-type.

float f = 94800620800; // ok
Int32 t = 94800620800; // error

Please explain this problem. Why I get a error when using Int32. Why I can use this number for float data-type because both of them are 4-byte data-type. Thanks.

Was it helpful?

Solution

Because the number you are trying to assign is larger than the largest possible value for a number of type Int32, which happens to be 2,147,483,647. To note, the maximum value for a Single is 3.402823 × 1038.

OTHER TIPS

The max value for Int32 is 2,147,483,647 - which is less than 94,800,620,800.

A float can take a value in the following range: ±1.5 × 10−45 to ±3.4 × 1038

Also, check out this SO question - what the difference between the float and integer data type when the size is same in java?. It's a Java question, but the concept is the same and there's a detailed explanation of the difference, even though they're the same size.

Because that number is too big for a 4 byte int. Scalar values like Int32 have a minimum and maximum limit (which are -231 and 231 - 1 in this case, respectively), and you simply can't store a value outside this range.

Floating point numbers are stored totally differently, so you won't get compiler errors with huge values, only possible precision problems later, during runtime.

Because of those types internal representation.

float uses something like i,d ^ n where i is the integral part, d is the decimal part and n is the exponent (of course, this happens in base 2).

In this way, you can store bigger numbers in a float, but it won't be accurate as a, say, Int32 in storing integral numbers. If you try to convert

float f = 94800620800; 

to an integral type large enough to store its value, it may not be the same as the initial 94800620800.

Integers types are exact representations, while floating point numbers are a combination of significant digits and exponent.

The floating point wiki page is explaining how this works.

Maybe you should read the error message? ;)

Error   Integral constant is too large

the maximum valiue of a 32 bit int is 2,147,483,647

the float on the other hand works because it stores a mantissa and exponent, rather than just a single number, so it can cope with a much bigger range at the expense of possibly losing precision

try printing out your float and you will get 94800620000 instead of 94800620800 as the lower bits are lost

Int32 has a max value of 2,147,483,647. Your value is much higher.

Take a look at system.int32.maxvalue

Provided value of the constant expression is not within the range of int datatype.

The range of Int32 goes from − 2,147,483,648 to 2,147,483,647. Your variable is way out of range.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top