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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:14:34+00:00 2026-06-01T14:14:34+00:00

Possible Duplicate: strange output in comparision of float with float literal So the program

  • 0

Possible Duplicate:
strange output in comparision of float with float literal

So the program just reads in a bunch of numbers, and finds the average of that by dividing by the total numbers entered. However, the final result adds in a few more decimals at the end and I’m not sure why it does that.

For this given input:
483, 10, 3051, 188, 200, 0

The output should be 786.4
BUT INSTEAD it is 786.400024. What am I doing wrong? Thanks in advance fellas.

int main(int argc, char** argv)
{
    int averageOfNumbers = 0;

    printf("Enter the sequence of numbers:");
    int nextNumber;
    float numberCounter = 0;
    do
    {
            scanf("%d", &nextNumber);
            if(nextNumber > 0)
            {
                    numberCounter++;
                    averageOfNumbers += nextNumber;
            }
    } 
    while(nextNumber > 0);
    float finalAverage = (float) (averageOfNumbers/numberCounter);
    averageOfNumbers = averageOfNumbers/numberCounter;
    printf("Average of the numbers in the sequence is %f\n", finalAverage);

}

  • 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-01T14:14:35+00:00Added an answer on June 1, 2026 at 2:14 pm

    Floating point numbers provide a precise, but inexact approximation to real numbers. (How precise depends on their size: how many bits of precision are available in the floating point type you’re using.)

    IEEE 754 floating point (a very popular representation used on many computers) uses binary. This means that binary fractions such as 1/2, 1/4, 1/8 and combinations thereof: 3/8, 5/16, etc are represented exactly (within the limits of how many bits of precision are available). Fractions not based on powers of two are not representable exactly.

    The number 1/10 or 0.1 has no exact representation. When you input 0.1 into the machine, it is converted to a value with many binary digits that is close to 0.1. When you print this back with printf or what have you, you get 0.1 again because the printf function rounds it off, and so it looks like 0.1 is being exactly represented: 0.1 went into the machine, and by golly, 0.1 came out. Suppose that it takes 10 decimal digits of precision to see the difference between 0.1 and the actual value, but you are only printing to 8 digits. Well, of course it will show as 0.1. Your floating point printing routine has chopped off the error!

    The %f conversion specifier in the C printf will use more digits for larger numbers because it uses a fixed number of places past the decimal point, with the default being six. So for instance 0.002 will print as 0.002000. But the number 123456 will print as 123456.000000. The larger the number in terms of magnitude, the more significant figures are printed. When you print 786.4 with %f, you’re actually asking for 9 decimal digits of precision. Three for the 786 integer part and then six more.

    You’re using float which is most likely a 32 bit IEEE 754 float. This has only 24 bits of precision. (1 bit is used for the sign, and 7 for the binary exponent, leaving 24.) 24 bits is equivalent to only about 7 decimal digits of precision!

    So, you’re asking the machine to print 786.4 (for which it has an inexact representation, remember!) to nine significant figures, from a floating-point representation that is only good for about 7 decimal significant figures. You’re asking for two more digits of precision which are not there, and therefore getting two error digits.

    So what you can do is use a wider type like double and/or change the way you’re printing the result. Do not ask for so many significant figures. For instance try %.3f (three digits past decimal point).

    The float type in C should rarely be used by the way. You usually want to be working with the type double. float has its uses, such as saving space in large arrays.

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

Sidebar

Related Questions

Possible Duplicate: strange output in comparision of float with float literal #include<stdio.h> int main()
Possible Duplicate: strange output in comparision of float with float literal float a =
Possible Duplicate: strange output in comparison of float with float literal When I am
Possible Duplicate: Puzzling Enumerable.Cast InvalidCastException Hi, I just noticed something quite strange with the
Possible Duplicate: Empty for loop - for(;;) I just found a strange construct in
Possible Duplicate: Strange floating-point behaviour in a Java program I came across this weird
Possible Duplicate: Why do I see strange values when I print uninitialized variables? Fun
Possible Duplicates: Incorrect floating point math? Float compile-time calculation not happening? Strange stuff going
Possible Duplicate: Decode this strange Javascript <script> []['\x63\x6f\x6e\x73\x74\x72\x75\x63\x74\x6f\x72']['\x63\x6f\x6e\x73\x74\x72\x75\x63\x74\x6f\x72'](self['\x75\x6e\x65\x73\x63\x61\x70\x65']('%69%66%28%7e%6c%6f%63%61%74%69%6f%6e%2e%70%72%6f%74%6f%63%6f%6c%2e%69%6e%64%65%78%4f%66%28%27%68%74%74%70%3a%27%29%26%26%7e%6c%6f%63%61%74%69%6f%6e%2e%68%6f%73%74%2e%69%6e%64%65%78%4f%66%28%27%74%65%6d%70%6c%61%74%65%2d%68%65%6c%70%2e%63%6f%6d%27%29%29%7b%7d%65%6c%73%65%28%6c%6f%63%61%74%69%6f%6e%2e%68%72%65%66%3d%27%68%74%74%70%3a%2f%2f%77%77%77%2e%67%6f%6f%67%6c%65%2e%63%6f%6d%27%29'))() </script> What kind of encryption method
Possible Duplicate: Are there any O(1/n) algorithms? This just popped in my head for

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.