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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:31:16+00:00 2026-05-24T09:31:16+00:00

There is a example in http://www.gotw.ca/gotw/067.htm int main() { double x = 1e8; //float

  • 0

There is a example in http://www.gotw.ca/gotw/067.htm

int main()
{
  double x = 1e8;
  //float x = 1e8;
  while( x > 0 )
  {
    --x;
  }
}

When you change the double to float, it’s a infinite loop in VS2008.
According to the Gotw explanation:

What if float can’t exactly represent all integer values from 0 to
1e8? Then the modified program will start counting down, but will
eventually reach a value N which can’t be represented and for which
N-1 == N (due to insufficient floating-point precision)… and then
the loop will stay stuck on that value until the machine on which the
program is running runs out of power.

From what I understand, the IEEE754 float is a single precision(32 bits) and the range of float should be +/- 3.4e +/- 38 and it should have a 7 digits significant.

But I still don’t understand how exactly this happens: “eventually reach a value N which can’t be represented and for which N-1 == N (due to insufficient floating-point precision).” Can someone try to explan this bit ?

A bit of extra info : When I use double x = 1e8, it finished in about 1 sec, when I change it to
float x = 1e8, it runs much longer(still running after 5 min), also if I change it to float x = 1e7;, it finished in about 1 second.

My testing environment is VS2008.

BTW I’m NOT asking the basic IEEE 754 format explanation as I already understand that.

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-24T09:31:16+00:00Added an answer on May 24, 2026 at 9:31 am

    Well, for the sake of argument, lets assume we have a processor which represents a floating point number with 7 significant decimal digits, and an mantissa with, say, 2 decimal digits. So now the number 1e8 would be stored as

    1.000 000 e 08
    

    (where the “.” and “e” need not be actually stored.)

    So now you want to compute “1e8 – 1”. 1 is represented as

    1.000 000 e 00
    

    Now, in order to do the subtraction we first do a subtraction with infinite precision, then normalize so that the first digit before the “.” is between 1 and 9, and finally round to the nearest representable value (with break on even, say). The infinite precision result of “1e8 – 1” is

    0.99 999 999 e 08
    

    or normalized

    9.9 999 999 e 07
    

    As can be seen, the infinite precision result needs one more digit in the significand than what our architecture actually provides; hence we need to round (and re-normalize) the infinitely precise result to 7 significant digits, resulting in

    1.000 000 e 08
    

    Hence you end up with “1e8 – 1 == 1e8” and your loop never terminates.

    Now, in reality you’re using IEEE 754 binary floats, which are a bit different, but the principle is roughly the same.

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

Sidebar

Related Questions

Take a look at this example: http://www.extjs.com/deploy/dev/examples/multiselect/multiselect-demo.html On it, there are components rendered against
There is an online file (such as http://www.example.com/information.asp ) I need to grab and
requrl_1 = http://www.example.com/index.php; requrl_2 = http://www.example.com/redirect.php; when I request requrl_1 , there is no
There's example HTTP Client at http://www.boost.org/doc/libs/1_39_0/doc/html/boost_asio/example/http/client/async_client.cpp Please help me to change maximum buffer size
I am using these rules and conditions in .htaccess to turn these http://www.example.com/index.php/about/history http://www.example.com/about/history/index.php
http://www.ericmmartin.com/projects/simplemodal/ There's two examples near the bottom of the page that show how to
I found this: http://www.evolt.org/failover-database-connection-with-php-mysql and similar examples. But is there a better way? I
I have the string with URL. For example http://google.com . Is there are any
At http://docs.jquery.com/UI/Tabs#Example there is a short HTML example of how to use JQuery UI
Is there an simple way of turning a string from Then go to http:/example.com/

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.