• Skip to main content
  • Skip to primary sidebar
BMA

BeMyAficionado

Inspire Affection

What Every Programmer Must know about Floating Point Numbers

August 23, 2017 by varunshrivastava Leave a Comment

Hey Friends!?Today I’ll talk about the concepts of floating point numbers. This is one of the trickiest part in the computer world. It is really very complicated to understand the concept of floating point numbers especially for a newbie. So, I thought of writing this article to help you understand floating point numbers better. If you are a programmer, and you think you know about the floating point numbers, well give it another thought.

Floating Point Numbers

People expect computers to be accurate. And they must expect it because we rely too much on computers for our day-to-day activity. But if you are a programmer, then one way or another you will face a situation where your computers won’t give you a correct answer for an obvious simple arithmetic operation. You don’t believe me, Right? Well, go on and run this piece of code on Java.

class SimpleFloatAddition{
  public static void main (String [] args){
    System.out.println((0.1+0.2));
  }
}

This is the output for the program,

floating-point-addition-anomaly

Now, imagine if you are creating a software which has to add up simple prices of two products, then it’s not accurate. The difference is so minute that we can ignore it but it’s there and that can cause you problem. What if an angry customer finds that out :D. If you know why this happens, then this article is probably not for you, but if you are curious to know the reason just to impress your friends or interviewer then go on and read the complete article. I have tried my best to explain it in the easiest way.

What are Floating Point numbers?

These are actually scientific notations. Let’s understand this concept with a real-world example. Suppose, you are an astrophysicist and you have to multiply the speed of light with some very small value.

Say,

300000000 * 0.00000015

We, humans, know a trick with the help of which we can solve these in less than 20sec. If we ask a human to solve this thing, he will probably go for it like this,

300000000 => 3*108

0.00000015 => 1.5*10-7

Which is, 4.5*10 = 45

This is the concept of Significant Numbers. Speed of light is approx. 300000000ms but in actual it’s something like 2.99977?? so we round it up and write it as 300000000. Floating point numbers are basically a mathematics of significant number.

Let’s talk about Base 10,
We follow base 10 rules for calculation,

100, 10, 1, -, 1/10, 1/100, 1/1000

100, 10, 1, -, 0.1, 0.01, 0.001

For computer, it will be like this,

4, 2, 1, -, ?, ? , 1/8, 1/16

4, 2, 0, -, 0 0011?. (Infinity)

A typical 32-bit machine can store only up to 23 digits and after that, it just cuts all the access digits right off. Computers also store the position of the decimal where it is in some scientific notation in base 2 and that’s what floating point actually is.
Now, let’s get back to the problem.

When we added 0.1+0.2, we got 3.00000000000004, to understand this let’s simply take the above concept that floating points numbers work on significant numbers. Which means that they don’t understand the concept of recursion as we humans do. So, for us,

1/10 + 2/10 = 3/10,

Or,

0.1+0.2 = 0.3

For computers, they don’t understand the concept of recursion so they would do something like this,

0.00011001100110011001100110011001100110011001100110011001100110… + 0. 0.00110011001100110011001100110011001100110011001100110011001100… = 0.3000 0000000000004

Yeah, it is that simple. I hope you got the concept here. If not, then no problem just go through the concept one more time, or simply enjoy Tom Scott’s Numberphile video here?

Conclusion

In nearly all cases floating point numbers are very close to the actual calculation and at top of that we do not need precision of more than 23 decimal place in most of the case and if we do we have 64 bit machines for that. Floating point numbers has really made this easy for us. These are very efficient and provides a great accuracy with less space requirements.

If you liked the article then please do share and comment below. It will really help others to increase their awareness on the concept of floating point numbers.

If you are a programmer then you would love to read these articles as well:

  • Simple Shopping Cart using Collections in Java
  • Thinking in OOPs
  • A peek inside the MNC’s development models
  • Understanding Collections in Java by Real World Object Modelling
  • Basic Authentication System with Php and MySql

Related

Filed Under: Programming Tagged With: floating point, floating point concept, floating point numbers, floating point numbers concept, programmer, programming, significant numbers

Primary Sidebar

Subscribe to Blog via Email

Do you enjoy the content? Feel free to leave your email with me to receive new content straight to your inbox. I'm an engineer, you can trust me :)

Join 874 other subscribers

Latest Podcasts

Recent Posts

  • Is The Cosmos a Vast Computation?
  • Building Semantic Search for E-commerce Using Product Embeddings and OpenSearch
  • Leader Election with ZooKeeper: Simplifying Distributed Systems Management
  • AWS Serverless Event Driven Data Ingestion from Multiple and Diverse Sources
  • A Step-by-Step Guide to Deploy a Static Website with CloudFront and S3 Using CDK Behind A Custom Domain

Recent Comments

  • Varun Shrivastava on Deploy Lambda Function and API Gateway With Terraform
  • Vaibhav Shrivastava on Deploy Lambda Function and API Gateway With Terraform
  • Varun Shrivastava on Should Girls Wear Short Clothes?
  • D on Should Girls Wear Short Clothes?
  • disqus_X5PikVsRAg on Basic Calculator Leetcode Problem Using Object-Oriented Programming In Java

Categories

  • Blogging
  • Cooking
  • Fashion
  • Finance & Money
  • Programming
  • Reviews
  • Software Quality Assurance
  • Technology
  • Travelling
  • Tutorials
  • Web Hosting
  • Wordpress N SEO

Archives

  • November 2024
  • September 2024
  • July 2024
  • April 2024
  • February 2024
  • November 2023
  • June 2023
  • May 2023
  • April 2023
  • August 2022
  • May 2022
  • April 2022
  • February 2022
  • January 2022
  • November 2021
  • September 2021
  • August 2021
  • June 2021
  • May 2021
  • April 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
  • April 2020
  • February 2020
  • December 2019
  • November 2019
  • October 2019
  • August 2019
  • July 2019
  • June 2019
  • May 2019
  • April 2019
  • March 2019
  • January 2019
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • June 2018
  • May 2018
  • March 2018
  • February 2018
  • January 2018
  • December 2017
  • November 2017
  • October 2017
  • September 2017
  • August 2017
  • July 2017
  • June 2017
  • May 2017
  • April 2017
  • March 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
  • October 2016
  • September 2016
  • August 2016
  • July 2016
  • June 2016
  • May 2016

Tags

Affordable Hosting (4) algorithms (4) amazon (3) aoc-2020 (7) believe in yourself (4) best (4) database (4) earn money blogging (5) education (4) elementary sorting algorithms (4) experience (3) fashion (4) finance (6) Financial Freedom (7) food (7) friends (3) goals (5) google (5) india (10) indian cuisine (5) indian education system (4) java (16) life (16) life changing (4) love (4) make money (3) microservices (9) motivation (4) oops (4) podcast (6) poor education system (4) principles of microservices (5) problem-solving (7) programmer (5) programming (28) python (5) reality (3) seo (6) spring (3) success (10) success factor (4) technology (4) top 5 (7) typescript (3) wordpress (7)

Copyright © 2025 · Be My Aficionado · WordPress · Log in

Go to mobile version