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 6092869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:29:41+00:00 2026-05-23T12:29:41+00:00

I wrote the following program: #include<stdio.h> int main(void) { float f; printf(\nInput a floating-point

  • 0

I wrote the following program:

 #include<stdio.h>
    int main(void)
    {
     float f;
     printf("\nInput a floating-point no.: ");
     scanf("%f",&f);
     printf("\nOutput: %f\n",f);
     return 0;
    }

I am on Ubuntu and used GCC to compile the above program. Here is my sample run and output I want to inquire about:

Input a floating-point no.: 125.1
Output: 125.099998

Why does the precision change?

  • 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-05-23T12:29:42+00:00Added an answer on May 23, 2026 at 12:29 pm

    Thank you all for your answers. Although almost all of you helped me look in the right direction I could not understand the exact reason for this behavior. So I did a bit of research in addition to reading the pages you guys pointed me to. Here is my understanding for this behavior:

    Single Precision Floating Point numbers typically use 4 bytes for storage on x86/x86-64 architectures. However not all 32 bits (4 bytes = 32 bits) are used to store the magnitude of the number.

    For storing as a single precision floating type, the input stream is formatted in the following notation (somewhat similar to scientific notation):

    (-1)^s x 1.m x 2^(e-127), where
      s = sign of the number, range:{0,1} - takes up 1 bit
      m = mantissa (fractional portion) of the number - takes up 23 bits
      e = exponent of the number offset by 127, range:{0,..,255} - takes up 8 bits
    

    and then stored in memory as

    0th byte 1st byte 2nd byte 3rd byte
    mmmmmmmm mmmmmmmm emmmmmmm seeeeeee
    

    Therefore the decimal number 125.1 is first converted to binary form but limited to 24 bits so that the mantissa is represented by no more than 23 bits. After conversion to binary form:

    125.1 = 1111101.00011001100110011
    

    NOTE: 0.1 in decimal can be represented up to infinite bits in binary but the computer limits the representation to 17 bits so the complete representation does not exceed 24 bits.

    Now converting it into the specified notation we get:

    125.1 = 1.111101 00011001100110011 x 2^6
          = (-1)^0 + 1.111101 00011001100110011 x 2^(133-127)
    

    which implies

    s = 0
    m = 11110100011001100110011
    e = 133 = 10000101
    

    Therefore, 125.1 will be stored in memory as:

    0th byte 1st byte 2nd byte 3rd byte
    mmmmmmmm mmmmmmmm emmmmmmm seeeeeee
    00110011 00110011 11111010 01000010
    

    On being passed to the printf() function the output stream is generated by converting the binary form to the decimal form. The bytes are actually stored in reverse order (from the input stream) and hence read in this order:

    3rd byte 2nd byte 1st byte 0th byte
    seeeeeee emmmmmmm mmmmmmmm mmmmmmmm
    01000010 11111010 00110011 00110011
    

    Next, it is converted into the specific notation for conversion

    (-1)^0 + 1.111101 00011001100110011 x 2^(133-127)
    

    On simplifying the above representation further:

    = 1.111101 00011001100110011 x 2^6
    = 1111101.00011001100110011
    

    And finally converting it to decimal:

    = 125.0999984741210938
    

    but single precision floating point can represent only up to 6 decimal places, therefore the answer is rounded off to 125.099998.

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

Sidebar

Related Questions

I wrote following program #include<stdio.h> main () { extern int i; printf(\n%d,i); } int
i wrote the following program #include <stdio.h> main() { int i = 2; float
When I write the following program: file 1: #include <stdio.h> int global; void print_global1()
Is the following program a valid C program? #include <stdio.h> int main() { fwrite(x,
I have the following C program: #include <fcntl.h> #include <termios.h> #include <stdio.h> int main()
Here is my server program #include<stdio.h> #include<stdlib.h> #include<sys/socket.h> #include<sys/un.h> #include<sys/types.h> #include<unistd.h> int main ()
I wrote the following program #include <iostream> template<typename C, typename Res, typename... Args> class
In Visual C++ i wrote the following sample in a C++ program: float f1
I wrote the following C++ program class MyClass { public: int i; int j;
#include <cstdio> #include <QtCore/QProcess> int main (int argc, char** argv) { // if we

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.