Why Floating-Point Numbers are not Always Accurate

Why Floating-Point Numbers are not Always Accurate

In this post, I will explain why Floating-Point numbers are not always precise. First, let me remind us of the basics of Floating-Point representation quickly.

If you are a programmer and handling numbers with fractions (for example to represent an elapsed time[secs] since a certain event or transaction money[dollars]), you might have heard that you should not use Floating-Point types because they are not always accurate/precise.

In this post, I will explain why Floating-Point numbers are not always precise.

First, let me remind us of the basics of Floating-Point representation quickly.

Floating-Point representation

IEEE 754 floating point standard specifies binary representation format of floating point as below,

Image for post

By Codekaizen — Own work, CC BY 3.0, https://commons.wikimedia.org/w/index.php?curid=3651274

In this case, the floating-point value representation has 32 bits size(called single-precision, for the rest of the post, I will focus on the single-precision floating-point). In C or C++ programming language, this corresponds to _float _type on most platforms(C++ standard does not specify the size).

  • sign: the sign of the floating-point number (1 bit).
  • exponent: the value that is placed in the exponent field on base 2 (8 bits).
  • fraction: the value generally between 0 and 1, placed in the fraction field(23 bits).

To compute a number from the encoded value, the following form is used.

Image for post

IEEE 754 adds an invisible leading bit(that is not actually encoded) with value 1 to save more bits in the fraction bits.

programming software data science

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

What Are The Advantages and Disadvantages of Data Science?

Online Data Science Training in Noida at CETPA, best institute in India for Data Science Online Course and Certification. Call now at 9911417779 to avail 50% discount.

Data Science Course in Dallas

Become a data analysis expert using the R programming language in this [data science](https://360digitmg.com/usa/data-science-using-python-and-r-programming-in-dallas "data science") certification training in Dallas, TX. You will master data...

50 Data Science Jobs That Opened Just Last Week

Data Science and Analytics market evolves to adapt to the constantly changing economic and business environments. Our latest survey report suggests that as the overall Data Science and Analytics market evolves to adapt to the constantly changing economic and business environments, data scientists and AI practitioners should be aware of the skills and tools that the broader community is working on. A good grip in these skills will further help data science enthusiasts to get the best jobs that various industries in their data science functions are offering.

Data Science With Python Training | Python Data Science Course | Intellipaat

🔵 Intellipaat Data Science with Python course: https://intellipaat.com/python-for-data-science-training/In this Data Science With Python Training video, you...

Here’s how I Learned Just Enough Programming for Data Science

How to approach learning programming and best books I recommend. There’s no doubt that data science requires decent programming skills, but how much is enough?