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

  • Home
  • SEARCH
  • 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 8902953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:44:43+00:00 2026-06-15T01:44:43+00:00

If I compile following c lines on windows and linux(ubuntu) I get different results.

  • 0

If I compile following c lines on windows and linux(ubuntu) I get different results. I would like to avoid. How can I do it?

 double a = DBL_EPSILON;
 double b = sqrt(a);
 printf("eps = %.20e\tsqrt(eps) = %.20e\n", a, b);

linux output:

eps = 2.22044604925031308085e-16        sqrt(eps) = 1.49011611938476562500e-08

windows output:

eps = 2.22044604925031310000e-016       sqrt(eps) = 1.49011611938476560000e-008

On linux tested with gcc and clang on 32-bit and 64-bit system same result.
On windows tested with gcc-mingw on 32-bit and visual-studio with 32-bit and 64-bit, also same results.

  • 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-15T01:44:44+00:00Added an answer on June 15, 2026 at 1:44 am

    In the example you give, it seems both programs have the same floating-point numbers. They just print them differently. The simplest solution around this particular issue is to write your own floating-point printing function. If you are not expecting too nice an output, you could use the function here as pseudocode for writing your own in C. It is not correctly rounded, but it works for what it is intended for (that is, reproducible and readable outputs).


    A deeper issue that your question hints you are encountering is floating-point computations giving different results on different platforms. This is a result of the C standard(s) not forcing compilers to implement the IEEE 754 floating-point standard exactly, specifically, allowing higher precision for intermediate results. And this relative leniency of the C standard(s) is caused at least in part by the historical x86 floating-point instructions making it expensive to implement the exact IEEE 754 semantics.

    On Linux, assuming you are using GCC, try the -msse2 compilation option. EDIT: the OP commented that -msse2 -mfpmath=sse worked for him.
    This makes GCC generate modern SSE2 instructions that give the exact IEEE 754 floating-point semantics. If on Windows you are using GCC too, use the same option there.

    If you are using Visual C: Visual C uses another trick to force the historical floating-point instructions to match IEEE 754 semantics: it tells the old 80-bit floating-point hardware to use only as many significand bits as IEEE 754 double-precision has. That gives an accurate simulation of double-precision numbers, except for a few corner cases that you will not be encountering. In this case it would help(*) if your program used only double-precision numbers (the C double type).

    (*) The Visual C compiler could theoretically generate code that computes exact single-precision arithmetics by rounding each intermediate result from double to single precision, but this would be expensive and I doubt it does this.

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

Sidebar

Related Questions

I'm basically asking why the following lines of codes do not compile: type IGenericType<'a>
I have made the following regexp for extracting robots links: re.compile(r/\S+(?:\/+)) And i get
I'm working on converting a Linux project of mine to compile on Windows using
I'm following this guide mostly to compile gcc on windows: http://www.aristeia.com/Misc/gcc4ForWindows.html The difference is
I get this solitary error while trying to compile the following code in code::blocks.
Can someone explain why the following code won't compile (formatted oddly to make it
When trying to compile a file that include winnt.h via windows.h, I get the
The following line of code generates the compile time error (PE19) There is no
When trying to compile the following code, I am getting a warning that line
The following code doesn't work (of course), because the marked line does not compile:

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.