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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:59:18+00:00 2026-05-16T23:59:18+00:00

I have some simple code that is comparing two float values to illustrate a

  • 0

I have some simple code that is comparing two float values to illustrate a problem I see with GCC’s optimization and am hoping someone can help me figure out why the output it produces is different under some repeatable circumstances.

First, I know it’s bad to compare float values with == because you can be off by some very small amount in the mantissa, however that is not the case in my example. The problem I have is the output changes based on 2 factors. 1) the optimization flag I pass in, and 2) if I uncomment the std::cout line.

Why does the code GCC produce run differently under -O2?
Why does the code compiled under -O2 work if I uncomment the print?

Here is the code I am testing:

#include <iostream>

const float ft_to_m          =  (float)0.3048; 
const float m_to_ft          =  (float)3.28083989501;


float FeetToKilometers( float & Feet ) {
  float Kilometers;
  Kilometers = (ft_to_m * Feet) / 1000.;
  return Kilometers;
}

int main(void)
{
    float feet = 20000.;
    float old_val = 0;
    float new_val = FeetToKilometers(feet );
    float diff_val = 0;

    int *old_int = reinterpret_cast<int*>(&old_val);
    int *new_int = reinterpret_cast<int*>(&new_val);

    for (int i=0; i<2; i++)
    {

    new_val = FeetToKilometers(feet );
    diff_val = old_val-new_val;

    //std::cout << "Random COUT that makes this work" << std::endl;

        if(old_val==new_val)
    {
             std::cout << "old_val==new_val" << std::endl;
         std::cout << std::hex << *old_int << "," << std::hex << *new_int << std::endl;
             std::cout << "diff_val = " << diff_val <<std::endl;
    }
        else
        {
            std::cout << "old_val!=new_val" <<std::endl;
        std::cout << std::hex << *old_int << "," << std::hex << *new_int << std::endl;
            std::cout << "diff_val = " << diff_val <<std::endl;
            old_val=FeetToKilometers(feet);
        }
    }

    return 0;
}

When compiled on linux/cygwin with -O0, -O1, and -O3 ( g++ -O test.cpp ), I get the following output:


$ ./a.exe
old_val!=new_val
0,40c3126f
diff_val = -6.096
old_val==new_val
40c3126f,40c3126f
diff_val = 0


That output is correct, you can see the bits for the floats (new_val and old_val) are identical. When I compile with the -O2 flag ( g++ -O2 test.cpp ) I get the following:


$ ./a.exe
old_val!=new_val
0,40c3126f
diff_val = -6.096
old_val!=new_val
40c3126f,40c3126f
diff_val = 1.19209e-07


I would consider this output wrong. Even though the two values are the same bit wise, subtracting them and the == check indicate they are different. If I then uncomment the std::cout line, and rebuild with the -O2 flag ( g++ -O2 test.cpp ) I get the following:


$ ./a.exe
Random COUT that makes this work
old_val!=new_val
0,40c3126f
diff_val = -6.096
Random COUT that makes this work
old_val==new_val
40c3126f,40c3126f
diff_val = 1.19209e-07


This is correct in that old_val == new_val, even though the subtraction still shows a slight difference.

This code also works under -O2 if feet is 2000, instead of 20000.

Can anyone explain why the compiled code is behaving like this? I want to know why 2 bit identical float values cannot be compared with ==.

gcc version 3.4.4

  • 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-16T23:59:18+00:00Added an answer on May 16, 2026 at 11:59 pm

    The optimization level and surrounding code may affect whether the values used in the diff_val calculation are being fetched from memory, or from registers. The processor
    may be using 80-bit internal floating point registers in one case, and 32-bit floating
    point values from memory in the other case, giving unexpected results.

    Yet another reason to avoid using == for floating point comparisons!

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Decompose the table into three different tables. One holds the… May 17, 2026 at 2:29 am
  • Editorial Team
    Editorial Team added an answer One patch per issue posted to the projects issue tracker.… May 17, 2026 at 2:29 am
  • Editorial Team
    Editorial Team added an answer You can wrap the result of url.openStream() in a GZIPInputStream.… May 17, 2026 at 2:29 am

Trending Tags

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

Top Members

Related Questions

I have been working for two years in software industry. Some things that have
I have some simple example WinForms code which I am trying to translate into
I have some older AS2-style Haxe code which uses flash.Lib.Current.CreateEmptyMovieClip() to create a slideshow
Is there a simple line of code that would allow only loading the code
We have a .NET application that needs to pass some data over to a
CVS and Subversion both have a handy merge feature so that when you update
I'm writing a very simple CRUD app that takes user stories and stores them
I've seen second one in another's code and I suppose this length comparison have
I am trying to tidy up some legacy code. The following is a cut
I have many textfields that show instruction text within the textbox (how the default

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.