User Milan Vucic
Milan Vucic
Programming Tutor at Codementor.io

Java Float Keyword

Published in the Java Developer group
At some point in the development of computing technology, it became clear that central processing units needed hardware devices to process floating point numbers. Today, all computer architectures can work effectively with such numbers. Of course, in programming languages, you also cannot do without the corresponding data types. There are two floating point data types in Java: float and double. The Java Float keyword defines a real number that occupies 32 bits in memory. We will talk about such numbers in this article.

Floating point numbers. How are real numbers stored in a computer?

To store real numbers in computer memory, a certain number of bits are allocated. A real number is stored as a sign (plus or minus), mantis, and exponent. What is the mantissa and exponent is best explained with an example. The approximate mass of the moon is 7* 1022. Here 7 is the mantis, in 22 the exponent. When displaying large or vice versa, very small numbers on the screen, you can see an entry like 7E22. This is the floating point number, and 7 here is the mantis, and 22 is the exponent or power of 10. This notation is called exponential notation.

Java float Keyword and Java Double Keyword

Float values (Floating point numbers or real numbers) in Java are represented by the types float and double. It is these keywords that are used to store values ​​up to a certain sign after the decimal point. double are numbers with double precision, as close as possible to the values ​​given or obtained as a result of calculations. Java Double is used for any mathematical calculations (square root, sine, cosine, ..), as well as for all calculations where a certain accuracy is important. float data type is used for a less precise floating point type. It is used very rarely in order to save memory. Below here we’ve got a table with the main information about float and double, as well as their differences.
Float Double
Main Float is a single-precision value It is a double-precision value
Default Size 4 bytes (32 bits) 8 bytes (64 bits)
Default Value 0.0f 0.0
Range from 1.4e–045 to 3.4e+038 from 4.9e–324 to 1.8e+308
What for it is used To save memory to work with fractional numbers relatively accurately
So Float keyword means a number, a single-precision value that takes 32 bits or 4 bytes in memory. On some processors, working with such numbers is faster, and, as already mentioned, they take up less space when compared with numbers with double precision. However, it is impossible to say unequivocally with speed. Let's say that some modern processors process exactly double precision numbers faster.

Java Float and Double Declaration

You can declare a number of type double in the same way as numbers of other types:

double myDouble = 2.7;
However, if you represent a floating point number in this way, the compiler will require you to change the type of the number to double. Here is an incorrect example of float variable:

public class FloatExample {
   public static void main(String[] args) {
//double and float variables 
       double myDouble = 2.7;
       float myFloat = 3.14;
   }
}
Here's what happens if you run this program:

Error:(4, 25) java: incompatible types: possible lossy conversion from double to float
The fact is that it is undesirable to use float numbers, and this should be done only to save memory. All real fractional numbers in Java are Double by default, and the syntax of the language also emphasizes this. If you really want to work with the float type, you need to specify it explicitly with an f terminating the number.

public class FloatExample {
   public static void main(String[] args) {
//double and float variables
       double myDouble = 2.7;
       float myFloat = 3.14f;
   }
}
By the way, float and double numbers can be written in exponential form.

float myFloat2 = 2E22f;
double myDouble2 = 3e10;
If you use large enough numbers in their "normal" representation, Java will display them immediately in exponential form. Let’s have an example:

public class FloatExample {
   public static void main(String[] args) {
//float variables
               float myFloatNumber1=2182818284590.45f;
               float myFloatNumber2=19822612787260.141592181f;
               System.out.println("myFloatNumber1 = " + myFloatNumber1);
               System.out.println("myFloatNumber2 = " + myFloatNumber2);
       System.out.println("myFloatNumber1 + myFloatNumber2 = " + myFloatNumber1 + myFloatNumber2);
           }
       }
The result of this program work is here:
myFloatNumber1 = 2.1828183E12 myFloatNumber2 = 1.98226121E13 myFloatNumber1 + myFloatNumber2 = 2.1828183E121.98226121E13
Java Float Keyword - 1

Special float and double numbers example

There are three special floating point numbers in Java language, that are used to indicate overflows and errors. Here they are:
  • Positive infinity is the result of dividing a positive number by 0. Represented by the constants Double.POSITIVE_INFINITY and Float.POSITIVE_INFINITY.

  • Negative infinity is the result of dividing a negative number by 0. Represented by the Double.NEGATIVE_INFINITY and Float.NEGATIVE_INFINITY constants.

  • NaN (not a number) represents the calculation of 0/0 or taking the square root of a negative number. Represented by the constants Double.NaN and Float.NAN.

Here is an example of using these special floating point numbers:

public class FloatExample {
   public static void main(String[] args) {
       int myInt = 1;
       float zero = 0.0f;
       double negZero = -0.0;
       double negativeInfinity = Double.NEGATIVE_INFINITY;
       double positiveInfinity = Float.POSITIVE_INFINITY;

       System.out.println(myInt / zero);
       System.out.println(myInt / negZero);
       System.out.println(zero == negZero);
       System.out.println(negativeInfinity * 0);
       System.out.println(positiveInfinity+negativeInfinity);

   }
}
The result is:
Infinity -Infinity true NaN NaN

Is Double precision sufficient?

In fact, despite the double precision of the Double type, using floating point numbers, for example, in financial calculations, is not the best idea because rounding errors are unacceptable. So, try to display the output of the following program on the screen.

public class FloatExample {
   public static void main(String[] args) {
       System.out. println( "2.0 - 1.1 = " + (2.0 - 1.1));
   }
}
You’ll get the following result:
2.0 - 1.1 = 0.8999999999999999
It would be logical to assume that the result will be 0.9. However, such errors are quite common and are associated with the internal binary representation of numbers. We cannot, for example, represent the exact value of ⅓ as a decimal fraction; of course, there are similar restrictions in the binary system. If the task requires eliminating rounding errors, Java has the BigDecimal class for this.
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION