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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:55:15+00:00 2026-05-13T08:55:15+00:00

#define MAXBUF 1000 int buf[MAXBUF]; int buffered = 0; int bufp = 0; int

  • 0
#define MAXBUF 1000
int buf[MAXBUF];
int buffered = 0;
int bufp = 0;

int getch()
{
    if(bufp > 0) {
       if(!--bufp)
          buffered = 0;

       return buf[bufp];
    }
    else {
       buffered = 0;
       return getchar();
    }

}

void ungetch(int c)
{
     buf[bufp++] = c;
     buffered = 1;
}

int getfloat(float *pn)
{
    int c, sign, sawsign;
    float power = 1.0;

    while(isspace(c=getch()))
         ;

    if(!isdigit(c) && c!= '+' && c!= '-' && c != '.') {
          ungetch(c);
          return 0;
    }

    sign = (c == '-') ? -1 : 1;

    if(sawsign = (c == '-' || c == '+'))
       c = getch();

    if(c != '.' && !isdigit(c)) {
         ungetch(c);

         if(sawsign)
            ungetch((sign == -1) ? '-' : '+');

         return 0;
    }

    for(*pn = 0.0; isdigit(c); c = getch())
        *pn = 10.0 * *pn + (float)(c - '0');

    if(c == '.')
       while(isdigit(c = getch())) {
         *pn = 10.0 * *pn + (float)(c - '0');
          power *= 10.0;
       }

    *pn *= sign;
    *pn /= power;

    ungetch(c);
    return c;
}

It always returns 23.7999 when i enter 23.8, and i have no idea why. Can anybody tell me 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-05-13T08:55:15+00:00Added an answer on May 13, 2026 at 8:55 am

    Numbers are represented in base 2, and base-2 floating-point values cannot represent every base-10 decimal value exactly. What you enter as 23.8 gets converted into its closest equivalent base-2 value, which is not exactly 23.8. When you print this approximate value out, it gets printed as 23.7999.

    You are also using float, which is the smallest floating-point type, and has only 24 bits of precision (roughly 7 decimal digits). If you switch to double, the amount of bits of precision more than doubles from float, so the difference between a decimal value such as 23.8 and its double representation is much smaller. This may allow a printing routine to perform the rounding better so that you see 23.8 with double. However, the actual value in the variable is still not exactly 23.8.

    As general advice, unless you have a huge number of floating-point values (making memory usage your primary concern), it is best to use double whenever you need a floating-point type. You don’t get rid of all odd behavior but you’re going to see less of it than with float.

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

Sidebar

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.