Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is float range that large with 4 bytes (±3.40282347E+38) [duplicate]

Both int and float types are of 4 bytes in java.

Then how can int represent a range of just -2,147,483,648 to 2,147,483,647 where float has approximately ±3.40282347E+38F while both have the same limited amount of bytes?

According to my understanding both should have the same range as they have the same amount of bytes. Can some one explain me how float can represent a range that large?

like image 969
preetk Avatar asked Aug 12 '19 06:08

preetk


People also ask

How is a float stored in 4 bytes?

Scalars of type float are stored using four bytes (32-bits). The format used follows the IEEE-754 standard. The mantissa represents the actual binary digits of the floating-point number. The power of two is represented by the exponent.

Is float always 4 bytes?

Yes it has 4 bytes only but it is not guaranteed.

What is the range of float with 64 bits?

A double precision, floating-point number is a 64-bit approximation of a real number. The number can be zero or can range from -1.797693134862315E+308 to -2.225073858507201E-308, or from 2.225073858507201E-308 to 1.797693134862315E+308.

What is the range of a 32 bit float?

32-bit single precision, with an approximate range of 10 -101 to 10 90 and precision of 7 decimal digits. 64-bit double precision, with an approximate range of 10 -398 to 10 369 and precision of 16 decimal digits.


1 Answers

"Floating" point means that the number of digits for the fractional part of your number can change to represent your number "as best as possible" given the constraints dictated by its size.

Let's forget for the time being of the 4 bytes of the float datatype and assume that you your "floating point" type can store up to 10 digits plus the negative symbol. This means you can accurately represent numbers from :-9 999 999 999 to +9 999 999 999. However, if you want one decimal, you can accurately represent numbers from -999 999 999.9 to +999 999 999.9. As you can see, the range has effectively changed.

Now, let's formalize a bit the explanation by talking about significand and exponent:

  • the significand contains your significant digits
  • the exponent represents the exponent of the 10 multiplier or, if it's easier, by how many positions you have to move the decimal point (with 0 being just before the first significant digit).

Let's say that your "floating point" data type can have up to 4 digits in its significand and up to 1 digit in its exponent as well, plus the minus symbol in both significand and exponent. You will be able to represent numbers from -0.9 999 * 10^9 = -999900000 to +0.999 9 * 10^9 = +999900000. As you can see while the numbers are pretty large you can't accurately represent most large numbers as you only have 4 digits you can use for your representation. This loss in precision is compensated by the ability to represent very small numbers so for example you can represent 0.999 9 * 10^-9 = 0.000 000 000 999 9.

This explains why the range is so large despite the size being only 4 bytes, as stated in your question.


To complete your knowledge on the matter, bring the above concepts to binary (your typical float uses 4 bits for the exponent, 23 for the significand and 1 bit for the sign of the significand).

Wikipedia is a good starting point. The major takeaway from programming purposes, usually, is to understand how many decimal digits you can store given your datatype (your "precision") as that will determine what specific decimal format fits your purposes best.

See the following link for more information: https://en.wikipedia.org/wiki/Floating-point_arithmetic#IEEE_754:_floating_point_in_modern_computers

Please note that understanding the concept of floating point number on a binary system is extremely important in information technology as even the most simple computations are heavily affected by it.

Floating points as represented on a computer (binary) are for example the reason why writing things like:

public class MyClass {
    public static void main(String args[]) {
      double x=0.1f;
      double y=0.2f;
      double z=0.3f;

      if(x+y == z) {
          System.out.println("something");
      }
      else {
          System.out.println("something else");
      }
    }
}

will counter-intuitively output something else but if you start playing with the numbers or change the type to float it will produce the correct output.

So be aware: you will need to understand the concept fully.

like image 155
Filippo Possenti Avatar answered Oct 09 '22 16:10

Filippo Possenti