element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Personal Blogs
  • Community Hub
  • More
Personal Blogs
Legacy Personal Blogs The Unpredictability of float data type : Explained
  • Blog
  • Documents
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: deeps
  • Date Created: 31 May 2016 2:16 PM Date Created
  • Views 1014 views
  • Likes 2 likes
  • Comments 3 comments
  • c/c++
  • float
  • data types
  • algorithms
  • programing
  • c programming
  • basic coding
  • ieee
Related
Recommended

The Unpredictability of float data type : Explained

deeps
deeps
31 May 2016

Ever wondered why one of the C data types is called float ?

Because in some cases it literally floats nearby expected values!

For example :-when you do multiple operations between two relatively small and large values of float.

 

Proof

int main(){

float x=0.001f;

for(i=0;i<=40000;i++){

x=0.0001f+ x;

printf("%f\n",x);}}

 

So add 0.0001 to 0.001 and keep adding and printing it

 

O/P:-

0.001100

0.001200 (OK so far)

.

0.063400

0.063501 (where did 1 come from?)

.

0.122501

0.122602 (2??)

.

0.390185

0.390285

Woa X where are you going ?Why?

Before answering the why we need to understand what is float?How it works?How is it stored?

What is Float?

According to IEEE754 Float is a 4-bytes data type stored in 3 parts:-1)Sign 2)Mantissa 3)Exponent

i.e float = 2^(exp) x 1.(mantissa bits in binary)

Simple?Not so much !!!!!

Think about this.

For saving a float we have 4 bytes distributed as below

s e e e e e e e e m m m m m m m m m m m m m m m m m m m m m m m

31                                                                                                                       0

1 Sign is simple +ve / -ve

2 Exponent is binary number from which we subtract 127 to get exp and than 2^exp

3 Mantissa

This one is tricky.Basically if you have a number in binary form for example 1101.000101 you give 1.101000101 as Mantissa where 1 is always supposed to be there (for valid non zero floating point value) and thus not needed to be mentioned and you add 3 in exponent to get proper value.

Just like saying in decimal that 100 is 1.0 * (10^2) to shift decimal point

Now if we want to add two numbers which are comparatively very big / small to each other we mess things up!!!!

if we want to add 0.00000001 to 1.0 we get 1.0 !!!!

There is a lot more going on in how addition , multiplication etc is done behind the scenes in background but in short float is not precise in certain situations.

 

Reason : The exponent itself (i.e the scaling)

Each float consists of exponent which determines how large / small a number is.

This means we can either represent very large / very small numbers.

Adding two floats whose scale difference is very big will sometimes result in the smaller one vanishing as we can not represent that small of a value into the larger scale.

Let's see it like this

 

Simple Explanation!

Suppose we use a similar data type in decimal form and we can save only 12 digits in mantissa including before and after the decimal point

Take two decimals 100.0000000100 and 1.000000000011 and add them

that means 1.000000000100 * 10^2 +1.000000001100 * 10^0

                               9876543210                         9876543210

so result is 101.000000010011 to be saved as 1.01000000010011 * 10^2

                                                                                              9876543210

BUT We can save only 12 digits after 1st 1!!! We have to discard the last 2 digits (11) to fit it in 12 digits.

We added 2 12 digit numbers but we have to discard precision of smaller number due to difference
in exponent!
In short the precision / range of numbers in right side reduces as left side numbers get bigger!
That is how it is and that is how it fits into just 4 bytes which is good.
Next time you use float make sure you know how it gets stored in memory and how it can give unexplainable results in some scenarios.

  • Sign in to reply
  • DAB
    DAB over 9 years ago

    I always say a fool and their data are soon parted.

     

    You need to really analyze your application to make sure that float and conversion issues are not add issues into your algorithm.

     

    This was one of the first things they teach you in computer science classes.

     

    Don't just guess, test and verify.  Some compilers do aweful things when you aren't looking.

     

    DAB

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • D_Hersey
    D_Hersey over 9 years ago in reply to clem57

    Oh booshwah!  Floats were designed to be good for first-order models in most cases, and to use time effectively.  There are other ways to represent numbers.  They could be recorded as a sum of prime factors, then multiplication becomes an integer summation.  They can be represented as the ratio of two very long integers.  One could use arctan notation. Floats are a good way to represent  numbers in a data stream such as from a sensor.  I used to program graphtals 'til I passed out.  Often the enumeration scheme would have to be designed to tolerate the enormous amount of re-iteration.  Computers can only represent (different) transcendental numbers (in general) through real-time execution of algorithms.  Numbers that are roots of polynomials or the ratio of two whole numbers can be represented perfectly given a little forethought to the data structure

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • clem57
    clem57 over 9 years ago

    Take 1 float and divide by 3 and get .3333... a repeating number. Next multiply by 100 for 33.33333... Next multiply by 3 and see what happens.

    Problem is computers cannot handle numbers with accuracy needed for repeating numbers. Common with ALL computers.image

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube