0

Why does

cout << floor(pow(1000000000,1.0/3))<<nl; 

give 999? It should be 1000.

This

cout << floor(powf(1000000000,1.0/3))<<nl;

gives 1000, but I do not understand why the first gives weird answer.

I saw someone having same trouble in sql here: I have a question about sql function: floor() But I do not know SQL so I couldn't understand the response.

mch
  • 9,424
  • 2
  • 28
  • 42
주성우
  • 13
  • 4
  • In both cases, due to floating point not having infinite precision. Errors are introduced in both cases. The only difference, in the second case, is that - on your system - the errors happen to cancel out. – Peter Dec 24 '21 at 08:50
  • *It should be 1000* - Why should it? – Evg Dec 24 '21 at 08:51
  • If you remove the `floor` and print all the decimals, you will see `999.9999999999996589394868351519107818603515625` and `1000.00018310546875`. – molbdnilo Dec 24 '21 at 09:50

0 Answers0