0

When I run the code it outputs 1 even though temp is 10 but it doesn't output 1.1 right after. Then it does what it's supposed to from 1.2 to 4.9 but then it outputs 5 and but not 5.1, which it shouldn't. So what the hell is going on here?

#include <iostream>

using namespace std;

int main()
{
    cout << "Input how many you want to calculate:\n";

    int last;
    cin >> last;
    double power = 1.0;
    double real = 0.0;
    //cout << 0 << ". " << real << "\n";

    for (int pos = 0; pos <= last; ++pos)
    {
        if (power * 10.0 <= real)
        {
            power = power * 10.0;
            real = 0.0;
            real = real + 1.0 / power;
        }

        double temp = real * power;
        double next = real + (1.0 / power);
        cout << "temp is: " << temp << "\n";

        if (next < power && static_cast<int>(temp) % 10 == 0)
            cout << "hello\n";
        else
            cout << pos << ". " << real << "\n";

        real = real + 1.0 / power;
    }

    return 0;
}
Chris
  • 26,361
  • 5
  • 21
  • 42
  • Maybe its this: [https://stackoverflow.com/questions/588004/is-floating-point-math-broken](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – drescherjm Mar 29 '22 at 14:41
  • 2
    *So what the hell is going on here?* -- Expecting binary floating point math to give you the same results as schoolbook, decimal-fraction math. All of your assumptions in your question is based on doing the calculations in straight-laced base 10. Computers (most of them at least) use base 2, not base 10. Try to convert `0.1` to base 2, and you will see the issue. – PaulMcKenzie Mar 29 '22 at 14:47
  • This cant be it because the temp output is correct. It enters the if even though temp is divisible by 10. – Kaldarin Mar 29 '22 at 14:51
  • @Kaldarin -- You say "it can't be", so what do you think it is? There are no gremlins inside of the program. Second, don't go by what you see as output -- that output could be there to make the number "look nice:" to you, but internally, the number is different in various decimal positions. That `10.0` you see as output could be `10.0000000234` internally. – PaulMcKenzie Mar 29 '22 at 14:53
  • @PaulMckenzie because i static_cast it to be an integer, so even if it was 10.9 instead of actually 10 it should enter the if and not the else. – Kaldarin Mar 29 '22 at 14:58
  • @Kaldarin -- You still didn't give a reason why you think it doesn't work. The idea is so that you realize what others are saying is what happened. As soon as you did this: `real = real + 1.0 / power;` -- you've entered "floating point approximation" land. – PaulMcKenzie Mar 29 '22 at 14:59
  • @Kaldarin `static_cast(temp)` and what if `temp` is `9.999999977`? If you need to see it for yourself, [look at this program](http://coliru.stacked-crooked.com/a/ff8f85d2f19b1f7d). You see that the output is 10, but using `static_cast` would give you 9. So regardless of how you want to look at it, your program is broken because of assumptions of how floating point math works. – PaulMcKenzie Mar 29 '22 at 15:04
  • @PaulMckenzie because i obviously dont know why it doesnt work. what i do know is that its not a floating point problem, wich i believe because of what i said before. Also it does work sometimes, but not at others. – Kaldarin Mar 29 '22 at 15:05
  • *i obviously dont know why it doesnt work.* -- Nowhere did you mention in your original post that you were aware of floating point being an approximation, and then justify why your code somehow skirts around this issue. We had to tell you this, and you're surprised, maybe not even aware of this aspect of programming. Debug your code, look at the values in the debugger, don't rely on output statements, as my previous comment shows. – PaulMcKenzie Mar 29 '22 at 15:08
  • 1
    I think once you understand that in floating point math that 0.2 + 0.1 is not equal to exactly 0.3 you can understand the reason for the differce. – drescherjm Mar 29 '22 at 15:09
  • @Kaldarin *Also it does work sometimes, but not at others.* -- And this is the reason why companies, mostly financial-based ones, ban the usage of `double` to calculate monetary values. Instead, data types that can represent the value exactly are used, or some companies are old-school and stick with COBOL, which can do exact math. – PaulMcKenzie Mar 29 '22 at 15:11
  • @PaulMcKenzie Aah what i didnt know is that floating point approximation can be lower than the actuall number to be calculated. Thank you for your help. – Kaldarin Mar 29 '22 at 15:15

0 Answers0