Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8598043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:03:55+00:00 2026-06-12T01:03:55+00:00

Possible Duplicate: Floating point comparison I have a problem about the accuracy of float

  • 0

Possible Duplicate:
Floating point comparison

I have a problem about the accuracy of float in C/C++. When I execute the program below:

#include <stdio.h>

int main (void) {
    float a = 101.1;
    double b = 101.1;
    printf ("a: %f\n", a);
    printf ("b: %lf\n", b);
    return 0;
}

Result:

a: 101.099998
b: 101.100000

I believe float should have 32-bit so should be enough to store 101.1 Why?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T01:03:56+00:00Added an answer on June 12, 2026 at 1:03 am

    You can only represent numbers exactly in IEEE754 (at least for the single and double precision binary formats) if they can be constructed from adding together inverted powers of two (i.e., 2-n like 1, 1/2, 1/4, 1/65536 and so on) subject to the number of bits available for precision.

    There is no combination of inverted powers of two that will get you exactly to 101.1, within the scaling provided by floats (23 bits of precision) or doubles (52 bits of precision).

    If you want a quick tutorial on how this inverted-power-of-two stuff works, see this answer.

    Applying the knowledge from that answer to your 101.1 number (as a single precision float):

    s eeeeeeee mmmmmmmmmmmmmmmmmmmmmmm    1/n
    0 10000101 10010100011001100110011
               |  | |   ||  ||  ||  |+- 8388608
               |  | |   ||  ||  ||  +-- 4194304
               |  | |   ||  ||  |+-----  524288
               |  | |   ||  ||  +------  262144
               |  | |   ||  |+---------   32768
               |  | |   ||  +----------   16384
               |  | |   |+-------------    2048
               |  | |   +--------------    1024
               |  | +------------------      64
               |  +--------------------      16
               +-----------------------       2
    

    The mantissa part of that actually continues forever for 101.1:

    mmmmmmmmm mmmm mmmm mmmm mm
    100101000 1100 1100 1100 11|00 1100 (and so on).
    

    hence it’s not a matter of precision, no amount of finite bits will represent that number exactly in IEEE754 format.

    Using the bits to calculate the actual number (closest approximation), the sign is positive. The exponent is 128+4+1 = 133 – 127 bias = 6, so the multiplier is 26 or 64.

    The mantissa consists of 1 (the implicit base) plus (for all those bits with each being worth 1/(2n) as n starts at 1 and increases to the right), {1/2, 1/16, 1/64, 1/1024, 1/2048, 1/16384, 1/32768, 1/262144, 1/524288, 1/4194304, 1/8388608}.

    When you add all these up, you get 1.57968747615814208984375.

    When you multiply that by the multiplier previously calculated, 64, you get 101.09999847412109375.

    All numbers were calculated with bc using a scale of 100 decimal digits, resulting in a lot of trailing zeros, so the numbers should be very accurate. Doubly so, since I checked the result with:

    #include <stdio.h>
    int main (void) {
        float f = 101.1f;
        printf ("%.50f\n", f);
        return 0;
    }
    

    which also gave me 101.09999847412109375000....

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Floating point issue in C #include<stdio.h> main() { int a,b; float f;
Possible Duplicate: Floating point equality in python I have a small issue about my
Possible Duplicate: Floating point comparison When I run the code below I get the
Possible Duplicate: Dealing with accuracy problems in floating-point numbers Code: float flt = 78983636;
Possible Duplicate: Floating point comparison I have some simple C code and the output
Possible Duplicate: Dealing with accuracy problems in floating-point numbers I was quite surprised why
Possible Duplicate: Strange floating-point behaviour in a Java program Why does JSP/JSTL division by
Possible Duplicate: What is a simple example of floating point/rounding error? When I execute
Possible Duplicate: Strange floating-point behaviour in a Java program I came across this weird
Possible Duplicate: Elegant workaround for JavaScript floating point number problem var sum = 0;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.