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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:33:34+00:00 2026-06-13T17:33:34+00:00

Consider two following pieces of code – the only difference between them is a

  • 0

Consider two following pieces of code – the only difference between them is a single cout which prints the value eps:
http://ideone.com/0bEeHz – here the program enters and infinite loop since after the cout eps changes value to 0

#include <iostream>

int main()
{
    double tmp = 1.;
    double eps;
    while(tmp != 0) {
        eps = tmp;
        tmp /= 2.;
    }
    if(eps == 0) {
        std::cout << "(1)eps is zero!\n";
    }
    std::cout << "eps before: " << eps;
    if(eps == 0) {
        std::cout << "(2)eps is zero!\n";
    }

    while(eps < 1.) {
        tmp = eps;
        eps *= 2.;
        if(tmp == eps) {
            printf("wtf?\n");
        }
    }

    std::cout << "eps after: " << eps;
}

http://ideone.com/pI4d30 – here I’ve commented out the cout.

#include <iostream>

int main()
{
    double tmp = 1.;
    double eps;
    while(tmp != 0) {
        eps = tmp;
        tmp /= 2.;
    }
    if(eps == 0) {
        std::cout << "(1)eps is zero!\n";
    }
    //std::cout << "eps before: " << eps;
    if(eps == 0) {
        std::cout << "(2)eps is zero!\n";
    }

    while(eps < 1.) {
        tmp = eps;
        eps *= 2.;
        if(tmp == eps) {
            printf("wtf?\n");
        }
    }

    std::cout << "eps after: " << eps;
}

Hence, one single cout changes program logic dramatically and very surprisingly. Why is that?

  • 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-13T17:33:35+00:00Added an answer on June 13, 2026 at 5:33 pm

    I think it’s a case of Section 5 (Expressions), paragraph 11

    The values of the floating operands and the results of floating expressions may be represented in greater precision and range than that required by the type; the types are not changed thereby.

    at work, cf. this variation of the original code.

    while(tmp != 0) {
        eps = tmp;
        tmp /= 2.;
    }
    

    Calculations and comparisons performed at extended precision. The loop runs until eps is the smallest positive extended value (probably the 80-bit x87 extended type).

    if(eps == 0) {
        std::cout << "(1)eps is zero!\n";
    }
    

    Still at extended precision, eps != 0

    std::cout << "eps before: " << eps;
    

    For the conversion to a string to print, eps is stored and converted to double precision, resulting in 0.

    if(eps == 0) {
        std::cout << "(2)eps is zero!\n";
    }
    

    Yes, now it is.

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

Sidebar

Related Questions

consider the following two pieces of code: public static Time Parse(string value) { string
Consider the following two pieces of code: var adj=0>grip.y?0<grip.x?0:-180:0<grip.x?-360:-180; and var adj; if (grip.y
Consider the two following Python code examples, which achieves the same but with significant
Consider the following situation: I have two android projects named P1 and P2 which
Please consider the two following snippets of code: (function f() { var x; try
Consider the following two segments of code in Java, Integer x=new Integer(100); Integer y=x;
Consider the following two methods, written in pseudo code, that fetches a complex data
Consider the following two lines of code if (test ! = null) and if
Consider the following two code samples: NSData *imgData = UIImagePNGRepresentation(imgFull); NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
Today, I've stumbled over the following: Consider two classes NewClass and NewClass1, which have

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.