A single-precision floating-point number is represented by 32 bits and hides various wonderful encoding techniques. However, some of those tricks migh

Three Myths About Floating-Point Numbers - C++ Stories

submited by
Style Pass
2021-06-07 07:30:04

A single-precision floating-point number is represented by 32 bits and hides various wonderful encoding techniques. However, some of those tricks might cause some imprecise calculations so it’s crucial to know how to work with those numbers.

Adam is a software engineer interested in graphics, media, and game development. He has broad experience, low level to high level, from developing graphics drivers, software for digital satellite television, making video games and 3D game engines, to photography, video recording and editing. You can find Adam on LinkedIn, Twitter, and his personal blog asawicki.info.

Floating-point numbers are a great invention in computer science, but they can also be tricky and troublesome to use correctly. I’ve written about them already by publishing Floating-Point Formats Cheatsheet and presentation “Pitfalls of floating-point numbers” (“Pułapki liczb zmiennoprzecinkowych” – the slides are in Polish). Last year I was preparing for a more extensive talk about this topic, but it got cancelled, like pretty much everything in these hard times of the COVID-19 pandemic. So in this post, I would like to approach this topic from a different angle.

A programmer can use floating-point numbers on different levels of understanding. A beginner would use them, trusting they are infinitely capable and precise, leading to problems. An intermediate programmer knows that they have some limitations, and so by using some good practices, the problems can be avoided. An advanced programmer understands what is really going on inside these numbers and can use them with full awareness of what to expect from them. This post may help you jump from step 2 to step 3. Commonly adopted good practices are called “myths” here, but they are actually just generalizations and simplifications. They can be useful for avoiding errors unless you understand what is true and what is false about them on a deeper level.

Leave a Comment