Understanding double and float data type – Computer Sir Ki Class
  

Login
[lwa]



Code Learning #JAVA#3840    siteicon   siteicon  

Understanding double and float data type

Double and float data type declaration its max value and sizing.

Learning Objectives

  • Declaration and initialisation of double and float types.
  • Limits of values
  • Maximum value
  • Size in bits

Source Code

TC++ #3840

Source Code

class DoubleFloat
{
public static void main(String[] args)
{
Double x=2342.34567;
float y=2342.34567f;
System.out.println(x);
System.out.println(y);
Double X=x;
Float Y=y;
System.out.println("Double type max value = "+X.MAX_VALUE);
System.out.println("Float type max value = "+Y.MAX_VALUE);
System.out.println("Double type size = "+X.SIZE);
System.out.println("Float type size = "+Y.SIZE);
}
}

Test it !

Run Output

2342.34567
2342.3457
Double type max value = 1.7976931348623157E308
Float type max value = 3.4028235E38
Double type size = 64
Float type size = 32

Code Understanding

Double x=2342.34567;
Here we are declaring and initialising a double data type. Note that the value 2342.34567 doesn’t have any suffix.

float y=2342.34567f;
Here we are declaring and initialising a float data type. Note that the value 2342.34567f has a f suffix which indicate that store this value in 32 bits only even if there is loss of precision.

System.out.println(x);
This will print the value 2342.34567 as expected. This is due to the fact that double can allow upto 15 decimal digits of precision in its decimal representation form.

System.out.println(y);
This will print the value 2342.3457 as against 2342.34567 as expected. This is due to the fact that float can allow upto 7 decimal digits of precision in its decimal representation form. Here 8 digits are printed and the last digit is likely out of precision.

Double X=x; Float Y=y;
Here we are enveloping both double and float primitive types to its wrapper class variation so that the required methods and fields can be used.

System.out.println(“Double type max value = “+X.MAX_VALUE);
Here we print maximum possible positive value for a double number. this will have 15 digit precision and maximum scientific notation power of 308. This is actually a very large number.

System.out.println(“Float type max value = “+Y.MAX_VALUE);
Here we print maximum possible positive value for a float number. this will have 7 digit precision and maximum scientific notation power of 38.

System.out.println(“Double type size = “+X.SIZE);
The storage data size of double which is 64 bit is printed here. In its binary storage form 1 bit is reserved for sign, 8 bits for exponent and 23 bits are there to store the significant digits base part.

System.out.println(“Float type size = “+Y.SIZE);
The storage data size of double which is 32 bit is printed here. In its binary storage form 1 bit is reserved for sign, 11 bits for exponent and 52 bits are there to store the significant digits base part.

Notes


Suggested Filename(s): DoubleFloat.java



Share

sunmitra| Created: 19-Mar-2018 | Updated: 19-Mar-2018|






Back