Possible Duplicate:
what the difference between the float and integer data type when the size is same in java?
As you probably know, both of these types are 32-bits.int
can hold only integer numbers, whereas float
also supports floating point numbers (as the type names suggest).
How is it possible then that the max value of int
is 231, and the max value of float
is 3.4*1038, while both of them are 32 bits?
I think that int
's max value capacity should be higher than the float because it doesn't save memory for the floating number and accepts only integer numbers. I'll be glad for an explanation in that case.
Integers and floats are two different kinds of numerical data. An integer (more commonly called an int) is a number without a decimal point. A float is a floating-point number, which means it is a number that has a decimal place. Floats are used when more precision is needed.
The reason you get different output is you perform a different computation: division behaves differently for integers and floating point numbers, except when the numerator is a multiple of the denominator.
Floating-point decimal values generally do not have an exact binary representation due to how the CPU represents floating point data. For this reason, you may experience a loss of precision, and some floating-point operations may produce unexpected results.
int can hold only integer numbers, whereas float also supports floating point numbers (as the type names suggest). How is it possible then that the max value of int is 231, and the max value of float is 3.4*1038, while both of them are 32 bits?
Your intuition quite rightly tells you that there can be no more information content in one than the other, because they both have 32 bits. But that doesn't mean we can't use those bits to represent different values.
Suppose I invent two new datatypes, uint4
and foo4
. uint4
uses 4 bits to represent an integer, in the standard binary representation, so we have
bits value 0000 0 0001 1 0010 2 ... 1111 15
But foo4
uses 4 bits to represent these values:
bits value 0000 0 0001 42 0010 -97 0011 1 ... 1110 pi 1111 e
Now foo4
has a much wider range of values than uint4
, despite having the same number of bits! How? Because there are some uint4
values that can't be represented by foo4
, so those 'slots' in the bit mapping are available for other values.
It is the same for int
and float
- they can both store values from a set of 232 values, just different sets of 232 values.
A float might store a higher number value, but it will not be precise even on digits before the decimal dot. Consider the following example:
float a = 123456789012345678901234567890f; //30 digits Console.WriteLine(a); // 1.234568E+29
Notice that barely any precision is kept.
An integer on the other hand will always precisely store any number within its range of values.
For the sake of comparison, let's look at a double precision floating point number:
double a = 123456789012345678901234567890d; //30 digits Console.WriteLine(a); // 1.23456789012346E+29
Notice that roughly twice as much significant digits are preserved.
These are based on IEEE754 floating point specification, that is why it is possible. Please read this documentation. It is not just about how many bits.
The hint is in the "floating" part of "floating point". What you say basically assumes fixed point. A floating point number does not "reserve space" for the digits after the decimal point - it has a limited number of digits (23 binary) and remembers what power of two to multiply it by.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With