Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do float and int have such different maximum values even though they're the same number of bits? [duplicate]

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.

like image 220
idish Avatar asked Jun 22 '12 14:06

idish


People also ask

Why are floats and integers different?

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.

Why does using float instead of int give me different results when all of my inputs are integers?

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.

Why are floats not exact?

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.

What is a limitation of ints compared to floats?

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?


4 Answers

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.

like image 146
AakashM Avatar answered Oct 03 '22 19:10

AakashM


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.

like image 42
Rotem Avatar answered Oct 03 '22 19:10

Rotem


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.

like image 23
kosa Avatar answered Oct 03 '22 19:10

kosa


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.

like image 44
harold Avatar answered Oct 03 '22 19:10

harold