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.

Also, to optimize operations on floating-point values, 127 is added to exponent as bias when encoding it to the binary representation format (i.e. the value with exponent -126 is stored with exponent +1), so we need to subtract 127 from the values in exponent bits when computing the number from the encoded value.

programming software software-development software-engineering computer-science 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

Software Developer vs Software Engineer — Differences: Bogus or Real?

In this article, see if there are any differences between software developers and software engineers. What you’re about to read mostly revolves around my personal thoughts, deductions, and offbeat imagination. If you have different sentiments, add them in the comment section, and let’s dispute! So, today’s topic…

Offshore Software Development - Best Practices

To make the most out of the benefits of offshore software development, you should understand the crucial factors that affect offshore development.

Best Free Courses For Computer Science, Software Engineering, and Data Science

Best Free Courses For Computer Science, Software Engineering, and Data Science. Become an Expert for Free! Learning Programming, Software Engineering, and Data Science Has Never Been Cheaper

Building a Career in Software Development Without a Computer Science Degree

Do you want to get a job as a Software Engineer at FAANG Companies but do not have a computer science degree? Could you not get CS in your dream college because you ended up screwing JEE but want to build a career in tech?

Is Software Engineering a Prerequisite for Data Science?

Find out here. Although data science job descriptions require a range of various skillsets, there are concrete prerequisites that can help you to become a successful data scientist. Some of those skills include, but are not limited to: communication, statistics, organization, and lastly, programming. Programming can be quite vague, for example, some companies in an interview could ask for a data scientist to code in Python a common pandas’ functions, while other companies can require a complete take on software engineering with classes.