• Skip to main content
  • Skip to primary sidebar
  • Home
  • About
  • Subscribe BMA
  • Contact Us!

Be My Aficionado

Inspire Affection

You are here: Home / Programming / What Every Programmer Must know about Floating Point Numbers

What Every Programmer Must know about Floating Point Numbers3 min read

August 23, 2017 by Varun Shrivastava 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

Share this:

  • Click to share on Facebook (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • Click to share on Pinterest (Opens in new window)
  • Click to share on Reddit (Opens in new window)
  • More
  • Click to print (Opens in new window)
  • Click to share on Tumblr (Opens in new window)
  • Click to share on Pocket (Opens in new window)
  • Click to share on Telegram (Opens in new window)
  • Click to share on WhatsApp (Opens in new window)
  • Click to share on Skype (Opens in new window)
  • Click to email this to a friend (Opens in new window)

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

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Featured Posts

Big Query: Everything You Need to Jump Start Your Development

July 23, 2018 By Varun Shrivastava Leave a Comment

OPPO A57 32GB Royal Gold Best Review

April 28, 2017 By Varun Shrivastava 3 Comments

History of Indian Food Heritage

August 30, 2017 By Varun Shrivastava Leave a Comment

Life of a Frustrated Engineer

December 24, 2016 By Varun Shrivastava Leave a Comment

Work Smarter: 5 Tips to Use Your Time Better

March 25, 2017 By Priyanka Yadav 2 Comments

Latest Posts

  • 3 Best Laptops (Mid-Range) For Every Use Case In India
  • Distributed System Architectural Patterns
  • The Power of being in the Present
  • Basic Calculator Leetcode Problem Using Object-Oriented Programming In Java
  • Study Abroad Destinations : Research and Review

Categories

  • Blogging (103)
  • Cooking (11)
  • Fashion (7)
  • Finance & Money (12)
  • Programming (51)
  • Reviews (4)
  • Technology (22)
  • Travelling (4)
  • Tutorials (12)
  • Web Hosting (8)
  • Wordpress N SEO (19)

Follow us on facebook

Follow us on facebook

Grab the Deal Now!

Hostgator Starting @$3.95/mo

DigitalOcean Free Credits

Trending

Affordable Hosting amazon aoc-2020 bad luck believe in yourself best database earn money blogging education experience fashion finance Financial Freedom food friends goals google india indian cuisine indian education system java javascript life life changing love make money microservices motivation oops poor education system principles of microservices problem-solving programmer programming reality search engines seo SSD Hosting success technology tips top 5 web web developer wordpress

Copyright © 2021 · BeMyAficionado by Varun Shrivastava · WordPress

loading Cancel
Post was not sent - check your email addresses!
Email check failed, please try again
Sorry, your blog cannot share posts by email.