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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:54:32+00:00 2026-05-14T03:54:32+00:00

EDIT: I had made a mistake during the debugging session that lead me to

  • 0

EDIT: I had made a mistake during the debugging session that lead me to ask this question. The differences I was seeing were in fact in printing a double and in parsing a double (strtod). Stephen’s answer still covers my question very well even after this rectification, so I think I will leave the question alone in case it is useful to someone.

Some (most) C compilation platforms I have access to do not take the FPU rounding mode into account when

  • converting a 64-bit integer to double;
  • printing a double.

Nothing very exotic here: Mac OS X Leopard, various recent Linuxes and BSD variants, Windows.

On the other hand, Mac OS X Snow Leopard seems to take the rounding mode into account when doing these two things. Of course, having different behaviors annoys me no end.

Here are typical snippets for the two cases:

#if defined(__OpenBSD__) || defined(__NetBSD__) 
# include <ieeefp.h>
# define FE_UPWARD FP_RP
# define fesetround(RM) fpsetround(RM)
#else 
# include <fenv.h>
#endif

#include <float.h>
#include <math.h>

fesetround(FE_UPWARD);

...
double f;
long long b = 2000000001;
b = b*b;
f = b;

...
printf("%f\n", 0.1);

My questions are:

  1. Is there something non-ugly that I can do to normalize the behavior across all platforms? Some hidden setting to tell the platforms that take rounding mode into account not to or vice versa?
  2. Is one of the behaviors standard?
  3. What am I likely to encounter when the FPU rounding mode is not used? Round towards zero? Round to nearest? Please, tell me that there is only one alternative 🙂

Regarding 2. I found the place in the standard where it is said that floats converted to integers are always truncated (rounded towards zero) but I couldn’t find anything for the integer -> float direction.

  • 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-14T03:54:32+00:00Added an answer on May 14, 2026 at 3:54 am

    If you have not set the rounding mode, it should be the IEEE-754 default mode, which is round-to-nearest.

    For conversions from integer to float, the C standard says (§6.3.1.4):

    When a value of integer type is
    converted to a real floating type, if
    the value being converted can be
    represented exactly in the new type,
    it is unchanged. If the value being
    converted is in the range of values
    that can be represented but cannot be
    represented exactly, the result is
    either the nearest higher or nearest
    lower representable value, chosen in
    an implementation-defined manner.
    If
    the value being converted is outside
    the range of values that can be
    represented, the behavior is
    undefined.

    So both behaviors conform to the C standard.

    The C standard says (§F.5) that conversions between IEC60559 floating point formats and character sequences be correctly rounded as per the IEEE-754 standard. For non-IEC60559 formats, this is recommended, but not required. The 1985 IEEE-754 standard says (clause 5.4):

    Conversions shall be correctly rounded
    as specified in Section 4 for operands
    lying within the ranges specified in
    Table 3. Otherwise, for rounding to
    nearest, the error in the converted
    result shall not exceed by more than
    0.47 units in the destination’s least significant digit the error that is
    incurred by the rounding
    specifications of Section 4, provided
    that exponent over/underflow does not
    occur. In the directed rounding modes
    the error shall have the correct sign
    and shall not exceed 1.47 units in the
    last place.

    What section (4) actually says is that the operation shall occur according to the prevailing rounding mode. I.e. if you change the rounding mode, IEEE-754 says that the result of float->string conversion should change accordingly. Ditto for integer->float conversions.

    The 2008 revision of the IEEE-754 standard says (clause 4.3):

    The rounding-direction attribute
    affects all computational operations
    that might be inexact. Inexact numeric
    floating-point results always have the
    same sign as the unrounded result.

    Both conversions are defined to be computational operations in clause 5, so again they should be performed according to the prevailing rounding mode.

    I would argue that Snow Leopard has the correct behavior here (assuming that it is correctly rounding the results according to the prevailing rounding mode). If you want to force the old behavior, you can always wrap your printf calls in code that changes the rounding mode, I suppose, though that’s clearly not ideal.

    Alternatively, you could use the %a format specifier (hexadecimal floating point) on C99 compliant platforms. Since the result of this conversion is always exact, it will never be effected by the prevailing rounding mode. I don’t think that the Windows C library supports %a, but you could probably port the BSD or glibc implementation easily enough if you need it.

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

Sidebar

Ask A Question

Stats

  • Questions 342k
  • Answers 342k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer E8 is greater than the maximum usable character 7F in… May 14, 2026 at 5:11 am
  • Editorial Team
    Editorial Team added an answer I'm not sure what you are trying to achieve here,… May 14, 2026 at 5:11 am
  • Editorial Team
    Editorial Team added an answer The eval() function is not part of AS3, but apparently… May 14, 2026 at 5:11 am

Related Questions

I occasionally write code something like this: // file1.cpp #define DO_THIS 1 #if DO_THIS
Two weeks ago I needed a way to communicate a wcf service with a
I've been developing a very large LOB app using my flavor of M-V-VM which
I'm in a bit of a pickle at work. My department designs a number
I downloaded the source for Launchy and am trying to build it in Visual

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.