1

How is it that when I uncomment "//cout << current << endl;" in the code below, it works?

#include <iostream>
#include <float.h>

using namespace std;

int main()
{
  char divider = 2;
  float power = 1;
  float number = 1;
  float current = number + power;
  cout.precision(1024);
  // Divide until rounded off
  while(current != number)
  {
    power /= divider;
    current = number + power;
    //cout << current << endl;
  }

  cout << power * divider << endl;
  cout << FLT_EPSILON << endl;
}

The code above prints:

1.40129846432481707092372958328991613128026194187651577175706828388979108268586060148663818836212158203125e-45
1.1920928955078125e-07

But after uncommenting the line "//cout << current << endl;" it prints:

1.5
1.25
1.125
1.0625
1.03125
1.015625
1.0078125
1.00390625
1.001953125
1.0009765625
1.00048828125
1.000244140625
1.0001220703125
1.00006103515625
1.000030517578125
1.0000152587890625
1.00000762939453125
1.000003814697265625
1.0000019073486328125
1.00000095367431640625
1.000000476837158203125
1.0000002384185791015625
1.00000011920928955078125
1
1.1920928955078125e-07
1.1920928955078125e-07

Which is correct, but why doesn't it work without that line?

I use the Android app with ID "com.duy.c.cpp.compiler" version "1.2.4-armeabi-v7a".

Martin
  • 29
  • 5
  • 2
    `cout.precision(1024)` <-- That's just silly. No built-in data type in C++ has that much precision. And in most applications you cannot expect/get more than 7-8 digits of base-10 accuracy anyway (which is in most cases also *plenty* and more than good enough). – Jesper Juhl May 21 '20 at 15:42
  • Are you sure it prints the output you have given when commented? because I tried running it and got a different output – Aditya Kurkure May 21 '20 at 15:48
  • 2
    I ran your code and it gave a different output 1.1920928955078125e-07 1.1920928955078125e-07 – RNGesus.exe May 21 '20 at 16:01
  • 1
    The commented version seems to work as well: https://onlinegdb.com/SkbiFmVo8 . –  May 21 '20 at 16:17
  • Seems OP uses `-ffast-math` flags [Demo](https://coliru.stacked-crooked.com/a/9b8bea8426ba7190) (gcc gives 0 whereas clang gives espilon). – Jarod42 May 21 '20 at 16:19

2 Answers2

1

This is just my guess but from what I see when you comment cout << current << endl your program does the calculus but the only thing that will print on your screen are the 2 count at the end of your program so the value of power * diviser and the value of flt-epsilon.

On the other hand, if you uncomment it your program prints every result of the calculus done in the while loop so that's why it changes a lot what you see (because in the while loop every time you will divide power and add it to current it will print the result and so on until the condition is reached).

I hope that my explanations were clear enough ^^

Jarod42
  • 203,559
  • 14
  • 181
  • 302
Godeta
  • 21
  • 2
  • 1
    I believe OP is referring to the 2nd-to-last line which should be unaffected by the commented line - in practice it is not reproducible however. –  May 21 '20 at 16:25
0

Thanks Jarod42, it seems you're right. It's because of -ffast-math.

Solved by adding

#pragma GCC optimize ("no-fast-math")

to the code.

Anyway, may someone explain me how does optimizing math result in the condition being true?

Martin
  • 29
  • 5