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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T07:41:34+00:00 2026-05-30T07:41:34+00:00

Let’s, float dt; I read dt from a text file as inputFile >> dt;

  • 0

Let’s,

float dt;

I read dt from a text file as

inputFile >> dt;

Then I have a for loop as,

for (float time=dt; time<=maxTime; time+=dt)
{
    // some stuff
}

When dt=0.05 and I output std::cout << time << std::endl; I got,

0.05
0.10
...
7.00001
7.05001
...

So, why number of digits is increasing after a while?

  • 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-30T07:41:35+00:00Added an answer on May 30, 2026 at 7:41 am

    Because not every number can be represented by IEEE754 floating point values. At some point, you’ll get a number that isn’t quite representable and the computer will have to choose the nearest one.

    If you enter 0.05 into Harald Schmidt's excellent online converter and reference the Wikipedia entry on IEEE754-1985, you’ll end up with the following bits (my explanation of that follows):

       s eeeeeeee mmmmmmmmmmmmmmmmmmmmmmm
       0 01111010 10011001100110011001101
         |||||||| |||||||||||||||||||||||
    128 -+||||||| ||||||||||||||||||||||+- 1 / 8388608
     64 --+|||||| |||||||||||||||||||||+-- 1 / 4194304
     32 ---+||||| ||||||||||||||||||||+--- 1 / 2097152
     16 ----+|||| |||||||||||||||||||+---- 1 / 1048576
      8 -----+||| ||||||||||||||||||+----- 1 /  524288
      4 ------+|| |||||||||||||||||+------ 1 /  262144
      2 -------+| ||||||||||||||||+------- 1 /  131072
      1 --------+ |||||||||||||||+-------- 1 /   65536
                  ||||||||||||||+--------- 1 /   32768
                  |||||||||||||+---------- 1 /   16384
                  ||||||||||||+----------- 1 /    8192
                  |||||||||||+------------ 1 /    4096
                  ||||||||||+------------- 1 /    2048
                  |||||||||+-------------- 1 /    1024
                  ||||||||+--------------- 1 /     512
                  |||||||+---------------- 1 /     256
                  ||||||+----------------- 1 /     128
                  |||||+------------------ 1 /      64
                  ||||+------------------- 1 /      32
                  |||+-------------------- 1 /      16
                  ||+--------------------- 1 /       8
                  |+---------------------- 1 /       4
                  +----------------------- 1 /       2
    

    The sign, being 0, is positive. The exponent is indicated by the one-bits mapping to the numbers on the left: 64+32+16+8+2 = 122 - 127 bias = -5, so the multiplier is 2-5 or 1/32. The 127 bias is to allow representation of very small numbers (as in close to zero rather that negative numbers with a large magnitude).

    The mantissa is a little more complicated. For each one-bit, you accumulate the numbers down the right hand side (after adding an implicit 1). Hence you can calculate the number as the sum of {1, 1/2, 1/16, 1/32, 1/256, 1/512, 1/4096, 1/8192, 1/65536, 1/131072, 1/1048576, 1/2097152, 1/8388608}.

    When you add all these up, you get 1.60000002384185791015625.

    When you multiply that by the multiplier 1/32 (calculated previously from the exponent bits), you get 0.0500000001, so you can see that 0.05 is already not represented exactly. This bit pattern for the mantissa is actually the same as 0.1 but, with that, the exponent is -4 rather than -5, and it’s why 0.1 + 0.1 + 0.1 is rarely equal to 0.3 (this appears to be a favourite interview question).

    When you start adding them up, that small error will accumulate since, not only will you see an error in the 0.05 itself, errors may also be introduced at each stage of the accumulation – not all the the numbers 0.1, 0.15, 0.2 and so on can be represented exactly either.

    Eventually, the errors will get large enough that they’ll start showing up in the number if you use the default precision. You can put this off for a bit by choosing your own precision with something like:

    #include <iostream>
    #include <iomanip>
    :
    std::cout << std::setprecison (2) << time << '\n';
    

    It won’t fix the variable value, but it will give you some more breathing space before the errors become visible.

    As an aside, some people recommend avoiding std::endl since it forces a flush of the buffers. If your implementation is behaving itself, this will happen for terminal devices when you send a newline anyway. And if you’ve redirected standard output to a non-terminal, you probably don’t want flushing on every line. Not really relevant to your question and it probably won’t make a real difference in the vast majority of cases, just a point I thought I’d bring up.

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

Sidebar

Related Questions

Let's say I have a text file composed like this ##### typeofthread1 ##### typeofthread2
Let's say I have the string: hello world; some random text; foo; How could
Let say I have some code HTML code: <ul> <li> <h1>Title 1</h1> <p>Text 1</p>
Let's say i have this block of code, <div id=id1> This is some text
Let's say I have the following text: (example) <table> <tr> <td> <span>col1</span> </td> <td>col2</td>
Let's say I have a javascript array with a bunch of elements (anywhere from
Let's say, I've got a XmlNode: <A>1</A> How to remove a text from it,
Let's say i have input 22365.2588 in my Edit Text. I want to change
Let's say I'm building a data access layer for an application. Typically I have
Let's say you have a class called Customer, which contains the following fields: UserName

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.