How to deal with Floating-Point Rounding Error

Image: Calculations using Floating-Point numbers 01
Image: Answer 01

IEEE 754 Standard

Image: IEEE 754 Standard for Single Precision (By: www.geeksforgeeks.org)
  • Sign: This is basically representing the sign of the number (Positive or Negative).
  • Exponent: This field represents both positive and negative exponents. A bias is added to the actual exponent in order to get the stored exponent.
  • Mantissa: This is part of a number in scientific notation or a floating-point number, consisting of its significant digits.
Image: Single, Double, Long-Double precision’s representation
Image: Converting 9.1 to binary
  • In IEEE standard the first bit represents the sign. So, if the sign positive the value will be “0”, and if the sign is negative the value will be “1”. In our case it is positive, that means value will be 0.
  • Now we have to consider the Exponent. In this scenario our exponent value is 2^3 (2 to the power 3). So, the Exponent has value of 8 bits to represent itself and exponent should represent both positive and negative (-128 to 127). Before moving to the last part, there is another thing to consider. That is if our exponent is positive, we must add that into the 8-bit representation. So, our exponent is 3 (2^3) and we must add it into the 127, then it becomes 130. Finally convert 130 into binary and it will represent the “Exponent value” in IEEE 754 standard.
  • Now all we left to do is add the 9.1’s Binary Scientific Notation as the Mantissa. So, after all these calculations, 9.1’s IEEE 754 representation is look like this.
Image: IEEE 754 Representation of 9.1
Image: Binary Representation of 9.1 by using computer

Why Floating-Point Rounding Error occur in computer?

Using BigDecimal Class

Image: Calculations using Floating-Point numbers 02
Image: Answer 02
Image: Methods from BigDecimal Class

References

Associate Software Engineer at Virtusa

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

Recommended from Medium

Criminal Lawyer Utah

Nostalgia’s second governance NFT — w/ Gharliera

How to deploy a OpenStack DevStack @ KVM for development purposes in an automated way

The Power of Reduce()

Why Infrastructure as code?

Scrum and Agile methodology

{UPDATE} Baletnice, Gwiazdy Pop i Modelki : Gry dla Dzieci Hack Free Resources Generator

Cloud-Based Automation Using BrowserStack and Selenium in Python

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
Nisal Pubudu

Nisal Pubudu

Associate Software Engineer at Virtusa

More from Medium

Building Something From Scratch Phase at Encora Apprenticeship — Week 4

Managing WFH Team Chemistry

6 Tips to Prepare for a Coding Bootcamp

VMware vSAN team: An interview insight