A float is a single-precision floating-point number. A double is a double-precision floating-point number. I read somewhere that computers cannot store floating-point numbers precisely so I checked this out myself in program prog51:
andrew@UBUNTU:~/Java$ cat prog51.java
public class prog51
{
public static void main (String args[])
{
float f1 = 81.3f * 1.94f;
System.out.println ("f1 = " + f1);
double d1 = 81.3d * 1.94d;
System.out.println ("d1 = " + d1);
}
}
andrew@UBUNTU:~/Java$ javac prog51.java
andrew@UBUNTU:~/Java$ java prog51
f1 = 157.72202
d1 = 157.72199999999998
andrew@UBUNTU:~/Java$
Judging by this, neither of these data types would be suitable for applications where accuracy is important such as the production of bank statements. The only way I found to calculate the correct answer to the multiplication sum above was as shown in prog52:
andrew@UBUNTU:~/Java$ cat prog52.java
public class prog52
{
public static void main (String args[])
{
float f1 = 813f * 194f / 1000f;
System.out.println ("f1 = " + f1);
double d1 = 813d * 194d / 1000d;
System.out.println ("d1 = " + d1);
}
}
andrew@UBUNTU:~/Java$ javac prog52.java
andrew@UBUNTU:~/Java$ java prog52
f1 = 157.722
d1 = 157.722
andrew@UBUNTU:~/Java$
When I find a better way, I will let you know.
Showing posts with label float. Show all posts
Showing posts with label float. Show all posts
Sunday, 21 December 2014
Precision in Floating Point Numbers in Java
Labels:
double,
float,
floating point,
Java,
precision
Location:
West Sussex, UK
Tuesday, 30 April 2013
Java float and double Variables
If you have a number with digits to the right of the decimal point, you can store it in a float or a double variable. However, if you try to assign it directly, it doesn't work:
UBUNTU > cat prog12.java
public class prog12
{
public static void main (String args[])
{
float f1 = 1/5;
double d1 = 1/5;
System.out.println ("f1 = " + f1);
System.out.println ("d1 = " + d1);
}
}
UBUNTU > javac prog12.java
UBUNTU > java prog12
f1 = 0.0
d1 = 0.0
UBUNTU >
You can add an f (or F) at
the end of the number before assigning it to a float variable and you
can add a d (or D) at the end before assigning it to a double. Notice how the double has greater precision:
UBUNTU > cat prog13.java
public class prog13
{
public static void main (String args[])
{
float f2 = 1/7f;
double d2 = 1/7d;
System.out.println ("f2 = " + f2);
System.out.println ("d2 = " + d2);
f2 = 1/9F;
d2 = 1/9D;
System.out.println ("f2 = " + f2);
System.out.println ("d2 = " + d2);
}
}
UBUNTU > javac prog13.java
UBUNTU > java prog13
f2 = 0.14285715
d2 = 0.14285714285714285
f2 = 0.11111111
d2 = 0.1111111111111111
UBUNTU >
public class prog13
{
public static void main (String args[])
{
float f2 = 1/7f;
double d2 = 1/7d;
System.out.println ("f2 = " + f2);
System.out.println ("d2 = " + d2);
f2 = 1/9F;
d2 = 1/9D;
System.out.println ("f2 = " + f2);
System.out.println ("d2 = " + d2);
}
}
UBUNTU > javac prog13.java
UBUNTU > java prog13
f2 = 0.14285715
d2 = 0.14285714285714285
f2 = 0.11111111
d2 = 0.1111111111111111
UBUNTU >
Alternatively, you can add (float) before a float variable or (double) before a double. This is called casting:
UBUNTU > cat prog14.java
public class prog14
{
public static void main (String args[])
{
float f3 = (float) 1/13;
double d3 = (double) 1/13;
System.out.println ("f3 = " + f3);
System.out.println ("d3 = " + d3);
}
}
UBUNTU > javac prog14.java
UBUNTU > java prog14
f3 = 0.07692308
d3 = 0.07692307692307693
UBUNTU >
public class prog14
{
public static void main (String args[])
{
float f3 = (float) 1/13;
double d3 = (double) 1/13;
System.out.println ("f3 = " + f3);
System.out.println ("d3 = " + d3);
}
}
UBUNTU > javac prog14.java
UBUNTU > java prog14
f3 = 0.07692308
d3 = 0.07692307692307693
UBUNTU >
As well as giving greater precision, a double can also store much larger numbers than a float:
UBUNTU > cat prog15.java
public class prog15
{
public static void main (String args[])
{
System.out.println
("Largest float = " + Float.MAX_VALUE);
System.out.println
("Largest double = " + Double.MAX_VALUE);
}
}
UBUNTU > javac prog15.java
UBUNTU > java prog15
Largest float = 3.4028235E38
Largest double = 1.7976931348623157E308
UBUNTU >
public class prog15
{
public static void main (String args[])
{
System.out.println
("Largest float = " + Float.MAX_VALUE);
System.out.println
("Largest double = " + Double.MAX_VALUE);
}
}
UBUNTU > javac prog15.java
UBUNTU > java prog15
Largest float = 3.4028235E38
Largest double = 1.7976931348623157E308
UBUNTU >
If you have a Java book on Amazon, which you would like to advertise here for free, please write to me at international_dba@yahoo.co.uk.
Labels:
(double),
(float),
casting,
cat,
D,
double,
Double.MAX_VALUE,
f,
float,
Float.MAX_VALUE,
Java,
javac,
Poornachandra Sarang,
System.out.println,
UBUNTU
Location:
West Sussex, UK
Subscribe to:
Posts (Atom)