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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T10:59:37+00:00 2026-05-12T10:59:37+00:00

I can only assume this is a bug. The first assert passes while the

  • 0

I can only assume this is a bug. The first assert passes while the second fails:

double sum_1 =  4.0 + 6.3;
assert(sum_1 == 4.0 + 6.3);

double t1 = 4.0, t2 = 6.3;

double sum_2 =  t1 + t2;
assert(sum_2 == t1 + t2);

If not a bug, 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-12T10:59:37+00:00Added an answer on May 12, 2026 at 10:59 am

    This is something that has bitten me, too.

    Yes, floating point numbers should never be compared for equality because of rounding error, and you probably knew that.

    But in this case, you’re computing t1+t2, then computing it again. Surely that has to produce an identical result?

    Here’s what’s probably going on. I’ll bet you’re running this on an x86 CPU, correct? The x86 FPU uses 80 bits for its internal registers, but values in memory are stored as 64-bit doubles.

    So t1+t2 is first computed with 80 bits of precision, then — I presume — stored out to memory in sum_2 with 64 bits of precision — and some rounding occurs. For the assert, it’s loaded back into a floating point register, and t1+t2 is computed again, again with 80 bits of precision. So now you’re comparing sum_2, which was previously rounded to a 64-bit floating point value, with t1+t2, which was computed with higher precision (80 bits) — and that’s why the values aren’t exactly identical.

    Edit So why does the first test pass? In this case, the compiler probably evaluates 4.0+6.3 at compile time and stores it as a 64-bit quantity — both for the assignment and for the assert. So identical values are being compared, and the assert passes.

    Second Edit Here’s the assembly code generated for the second part of the code (gcc, x86), with comments — pretty much follows the scenario outlined above:

    // t1 = 4.0
    fldl    LC3
    fstpl   -16(%ebp)
    
    // t2 = 6.3
    fldl    LC4
    fstpl   -24(%ebp)
    
    // sum_2 =  t1+t2
    fldl    -16(%ebp)
    faddl   -24(%ebp)
    fstpl   -32(%ebp)
    
    // Compute t1+t2 again
    fldl    -16(%ebp)
    faddl   -24(%ebp)
    
    // Load sum_2 from memory and compare
    fldl    -32(%ebp)
    fxch    %st(1)
    fucompp
    

    Interesting side note: This was compiled without optimization. When it’s compiled with -O3, the compiler optimizes all of the code away.

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

Sidebar

Ask A Question

Stats

  • Questions 171k
  • Answers 171k
  • 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 You need to pass --export-dynamic to the linker. So from… May 12, 2026 at 2:23 pm
  • Editorial Team
    Editorial Team added an answer There is a fantastic package in Python called itertools. But… May 12, 2026 at 2:23 pm
  • Editorial Team
    Editorial Team added an answer It's an unsigned right shift -- they're basically do that… May 12, 2026 at 2:23 pm

Related Questions

I just installed the FindBugs plugin for Eclipse, with the hope that it will
I need to step through the Qt code to help solve a bug in
I've just built an SVG map of New Zealand for use with the excellent
QTextEdit t; t.setHtml( ); displays a small 'a' with acute accent instead of a non-breaking

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.