2

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.

mskfisher
  • 3,291
  • 4
  • 35
  • 48
Leo Vo
  • 9,980
  • 9
  • 56
  • 78
  • 2
    `Int32` maxvalue is 2,147,483,647, that's why there is the error – Steve B Sep 09 '11 at 09:11
  • A float is a floating point value. An int is an integer value. – David Heffernan Sep 09 '11 at 09:12
  • 4
    You should correctly title your question. You are willing to understand why two types, both represented with 4 Bytes, don't have the same restrictions. – Steve B Sep 09 '11 at 09:12
  • 2
    I'll add that float has 7 digits precision. Try `Console.WriteLine(94800620800f == 94800620801f);` – xanatos Sep 09 '11 at 09:16
  • But both of them are 4-byte data-type. Why Int32 can't store this value??? – Leo Vo Sep 09 '11 at 09:19
  • Lu Lu try reading how does float store data... – Umer Sep 09 '11 at 09:21
  • 2
    [what the difference between the float and integer data type when the size is same in java?](http://stackoverflow.com/questions/4806944/what-the-difference-between-the-float-and-integer-data-type-when-the-size-is-same) and/or [floating point entry](http://en.wikipedia.org/wiki/Floating_point) – Tim Sep 09 '11 at 09:22

9 Answers9

5

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.

Grant Thomas
  • 44,454
  • 10
  • 85
  • 129
  • Because of the internal mechanisms used to calculate the desired numbers from the data stored in bytes, for instance multiplication with integers and exponentiation with real numbers. – Grant Thomas Sep 09 '11 at 09:22
  • No, value you are trying to pass is 5 bytes - 0x16128EAD00 – sll Sep 09 '11 at 09:28
5

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.

Community
  • 1
  • 1
Tim
  • 28,212
  • 8
  • 63
  • 76
3

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.

Péter Török
  • 114,404
  • 31
  • 268
  • 329
3

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.

Simone
  • 11,655
  • 1
  • 30
  • 43
1

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

jk.
  • 13,817
  • 5
  • 37
  • 50
1

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.

Steve B
  • 36,818
  • 21
  • 101
  • 174
0

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

Take a look at system.int32.maxvalue

Bas Slagter
  • 9,831
  • 7
  • 47
  • 78
0

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

KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
0

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