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

The Archive Base Latest Questions

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

While writing a function which will perform some operation with each number in a

  • 0

While writing a function which will perform some operation with each number in a range I ran into some problems with floating point inaccuracies. The problem can be seen in the code below:

#include <iostream>

using namespace std;

int main()
{
    double start = .99999, end = 1.00001, inc = .000001;
    int steps = (end - start) / inc;

    for(int i = 0; i <= steps; ++i)
    {
        cout << (start + (inc * i)) << endl;
    }
}

The problem is that the numbers the above program outputs look like this:

0.99999
0.999991
0.999992
0.999993
0.999994
0.999995
0.999996
0.999997
0.999998
0.999999
1
1
1
1
1
1.00001
1.00001
1.00001
1.00001
1.00001
1.00001

They only appear to be correct up to the first 1. What is the proper way to solve this problem?

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

    Your sample output only looks wrong because of how ostream rounds by default. You can set precision to get some other output.

    cout.precision(10);
    cout << (start + (inc * i)) << endl;
    

    Here’s what I get now:

    0.99999
    0.999991
    0.999992
    0.999993
    0.999994
    0.999995
    0.999996
    0.999997
    0.999998
    0.999999
    1
    1.000001
    1.000002
    1.000003
    1.000004
    1.000005
    1.000006
    1.000007
    1.000008
    1.000009
    1.00001
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Many times while writing functions that accept enumerable types I face this confusion. Which
I am writing a function that will take in the head of a linked
I'm writing a program that will watch a particular directory for new files containing
I am writing some code to generate call graphs for a particular intermediate representation
I'm a C# beginner facing problems regarding sending an image from a client to
I'm watching this lecture: http://www.youtube.com/watch?v=Kq4FpMe6cRs // the speaker states that 'bar' is just some
Hey all, I am writing a logviewer application. These logs are between 100-200 megs
I'm working on a C++ class assignment and I'd like some input. I'm not
I got a strange problem here: i have a potential large (as in up
I am currently developing a prototype for a game and I need a simple

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.