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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:47:14+00:00 2026-05-31T11:47:14+00:00

While trying to figure out how to round a float like 1.255 to the

  • 0

While trying to figure out how to round a float like 1.255 to the nearest hundredth, I found something interesting. I’m using gcc 4.4.5 on Debian 6.

int   x = (1.255 * 100) + 0.5;   //  gives me back 125 instead of 126. 
float y = (1.255 * 100) + 0.5;   //  gives me back 126.000000. 

Why is is that when I save to an int I get back 125 and not 126 ? In fedora when I save the above expression to an int I get back 126. Is this a gcc bug in debian ? Any help would be greatly appreciated.
Thanks.

  • 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-31T11:47:15+00:00Added an answer on May 31, 2026 at 11:47 am

    Although this looks like a “typical” floating-point question, it’s more complicated than that.

    This one involves a combination of 3 things:

    • The “usual” floating-point representation stuff.
    • Implicit casting.
    • Implicit type-promotion.

    Let’s break this down:

    Floating-point literals are of type double by default. Hence 1.255 is of type double.

    Thus the expression:

    (1.255 * 100) + 0.5
    

    is done using type double.

    But because binary floating-point can’t represent 1.255 exactly, the expression evaluates to:

    (1.255 * 100) + 0.5 = 125.99999999999999000000
    

    and is of type double.

    Since this is less than 126, storing it to an integer will result in 125.
    Storing it to float will round it to the nearest float, which results in 126.


    int    x = (1.255 * 100.) + 0.5;
    float  y = (1.255 * 100.) + 0.5;
    double z = (1.255 * 100.) + 0.5;
    
    cout << fixed;
    cout << x << endl;
    cout << setprecision(20) << y << endl;
    cout << setprecision(20) << z << endl;
    

    Output:

    125
    126.00000000000000000000
    125.99999999999999000000
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While trying to figure out the best method to ping (ICMP) something from python,
I learned something new while trying to figure out why my readwrite property declared
I was hanging out in my profiler for a while trying to figure out
A while ago I was trying to figure out a way of doing this
While trying to figure out terminology for rendering, I came down to this problem
I am working on my first NASM program, and while trying to figure out
I've been struggling for a while now trying to figure this out and I'm
Okay I've been trying to figure this out for a while now. I have
I'm trying to figure out the best solution to create a game-round timer in
Was just thinking about this question while trying to figure out some code written

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.