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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:01:11+00:00 2026-05-23T13:01:11+00:00

In a book I am currently reading, there is this excerpt: You can also

  • 0

In a book I am currently reading, there is this excerpt:

You can also use a floating-point
value as a loop counter. Here’s an
example of a for loop with this kind
of counter:

double a(0.3), b(2.5);
for(double x = 0.0; x <= 2.0; x += 0.25)
    cout << "\n\tx = " << x << "\ta*x + b = " << a*x + b;

This code fragment calculates the
value of a*x+b for values of x
from 0.0 to 2.0, in steps of
0.25; however, you need to take care
when using a floating-point counter in
a loop. Many decimal values cannot be
represented exactly in binary
floating-point form, so discrepancies
can build up with cumulative values.
This means that you should not code a
for loop such that ending the loop
depends on a floating-point loop
counter reaching a precise value. For
example, the following poorly-designed
loop never ends:

for(double x = 0.0 ; x != 1.0 ; x += 0.2)
    cout << x;

The intention with this loop is to
output the value of x as it varies
from 0.0 to 1.0; however, 0.2
has no exact representation as a
binary floating-point value, so the
value of x is never exactly 1.
Thus, the second loop control
expression is always false, and the
loop continues indefinitely.

Can someone please explain how the first code block runs while the second doesn’t?

  • 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-23T13:01:12+00:00Added an answer on May 23, 2026 at 1:01 pm

    The first one will eventually terminate, even if x doesn’t reach exactly 2.0… because it’ll end up being greater than 2.0, and thus break out.

    The second one would have to make x hit exactly 1.0 in order to break.

    It’s unfortunate that the first example uses a step of 0.25, which is exactly representable in binary floating point – it would have been smarter to make both examples use 0.2 as the step size. (0.2 isn’t exactly representable in binary floating point.)

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

Sidebar

Related Questions

im currently reading a book about programming Android and there is a nice little
I'm currently reading the book Professional Enterprise .NET and I've noticed this warning in
Currently I'm reading a book (Pro ASP.Net Framework). In this book, the author suggests
I'm currently reading a book about ASP.NET MVC3 to learn working with this framework.
I am reading a great book called Swing: A Beginner's guide. There is this
I'm currently reading the book Pro Asp.Net MVC Framework. In the book, the author
Hi, guys! Currently I'm reading a book Programming in Objective-C by Stephen Kochan. Actually
I am currently building an Book-App (sort of). So there are a lot of
Info: I'm currently trying to learn template metaprogramming (by following this book ). One
I'm currently reading Practical Django Projects and in the Django admin interface there is

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.