FLOATING POINT ROUNDING ERROR

for(float a=5; a!=0.0;a-=0.1)
{
System.out.println(a);
}
5.0
4.9
4.8
4.7000003
4.6000004
4.5000005
4.4000006
4.3000007
4.200001
4.100001
4.000001
3.900001
3.8000011
3.7000012
3.6000013
3.5000014
3.4000015
3.3000016
3.2000017
3.1000018
3.000002
2.900002
2.800002
2.7000022
2.6000023
2.5000024
2.4000025
2.3000026
2.2000027
2.1000028
2.0000029
1.9000028
1.8000028
1.7000028
1.6000028
1.5000027
1.4000027
1.3000027
1.2000027
1.1000026
1.0000026
0.9000026
0.8000026
0.70000255
0.6000025
0.5000025
0.4000025
0.30000252
0.20000252
0.10000252
2.5197864E-6
-0.09999748
-0.19999748
-0.29999748
-0.39999747
-0.49999747
-0.59999746
-0.6999975
-0.7999975
-0.89999753
-0.99999756
-1.0999975
-1.1999975
-1.2999976
-1.3999976
-1.4999976
-1.5999976
-1.6999977
-1.7999977
-1.8999977
-1.9999977
...

IEEE 754 floating point standard

IEEE 754 Single Precision.
IEEE 754 Double Precision.
IEEE 754 Long Double Precision
12 -> 1100
0.7 -> 1011011011011011011011011....
12.7 -> 1100.1011011011011011011011011....
12.7 -> 1.1001011011011011011011011011.... x 2³
10000010
10010110110110110110110
0 | 10000010 | 10010110110110110110110

0 | 10000010 | 10010110110110110110111

BigDecimal

BigDecimal(double val)
BigDecimal(int val)
BigDecimal(String val)
            BigDecimal a = new BigDecimal(5);
BigDecimal b = new BigDecimal(0.2);
a = a.subtract(b);
System.out.println(a.toString());
4.799999999999999988897769753748434595763683319091796875
            BigDecimal a = new BigDecimal(5);
BigDecimal b = new BigDecimal(0.2,new MathContext(1));
a = a.subtract(b);
System.out.println(a.toString());
bigDecimal.add(BigDecimal obj) -> adds the two values.
bigDecimal.subtract(BigDecimal obj) -> substracts the obj value from the bigDecimal value.
bigDecimal.multiply(BigDecimal obj)-> multiplies the two values.
bigDecimal.divide(BigDecimal onj) -> divides the two values.
BigDecimal a = new BigDecimal("5");
BigDecimal b = new BigDecimal("0.2");
boolean val = a.equals(c)
int res = a.compareTo(c)
            BigDecimal a = new BigDecimal("5");
BigDecimal b = new BigDecimal("0.1");
BigDecimal c = new BigDecimal("0.0");
for(;a.equals(c)!=true;a=a.subtract(b))
{
System.out.println(a);
}
System.out.println("Loop Succesfully Finished");
 5.0
4.9
4.8
4.7
4.6
4.5
4.4
4.3
4.2
4.1
4.0
3.9
3.8
3.7
3.6
3.5
3.4
3.3
3.2
3.1
3.0
2.9
2.8
2.7
2.6
2.5
2.4
2.3
2.2
2.1
2.0
1.9
1.8
1.7
1.6
1.5
1.4
1.3
1.2
1.1
1.0
0.9
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1

When to use BigDecimal

--

--

--

Associate Software Engineer at Virtusa

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Dynamic libraries in C

Are Some Infinities Bigger Than Others?

CS373 Fall 2021: Varad Thorat

My Side Hustle Journey

Cloud Computing: New Horizon in Seismic Technology

Announcement of UQC listing on Bibox Exchange with UQC/BTC and UQC/ETH Pairs.

How Agoda manages 1.5 Trillion Events per day on Kafka

How to install ASP.NET Core application on Raspberry Pi 4

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ravindran Kugan

Ravindran Kugan

Associate Software Engineer at Virtusa

More from Medium

MAVEN commands and interview questions answers

Lexorank — Managing Sorted Tables With Ease

Rabbit MQ Consume message as batch

What is Eclipse MicroProfile?