0

My code should calculate with maximum precision different powers of different ints. Sometimes I have problem digits after nought point.

Example code:

#include <iostream>
#include <math.h>
#include <iomanip>

using namespace std;

int main()
{
    cout << setprecision(10000);
    int length = 5;
    long double Maxi = pow(10, length);
    cout << Maxi << endl;
    return 0;
}

prints

100000.00000000000000710542735760100185871124267578125

instead of

100000

Screen: http://postimg.org/image/epdsrinkx/

Is it something to worry about or should I just round down and forget about it?

Adam Bieńkowski
  • 585
  • 4
  • 21
  • 4
    You realize that you told it to print out 1000 digits of precision after the decimal point, right? – Edward Strange Jan 27 '16 at 00:58
  • What compiler are you using? – Anton Savin Jan 27 '16 at 01:00
  • Probably a dupe of: http://stackoverflow.com/questions/21895756/why-are-floating-point-numbers-inaccurate – vsoftco Jan 27 '16 at 01:02
  • @CrazyEddie Yes. I did it because I don't want to display large numbers in scientific notation. – Adam Bieńkowski Jan 27 '16 at 01:02
  • @AntonSavin I'm using GNU GCC Compiler @ Code::Blocks 13.12 – Adam Bieńkowski Jan 27 '16 at 01:04
  • 1
    @vsoftco `100000` can be represented exactly, so this is not the case. – Anton Savin Jan 27 '16 at 01:08
  • @AdamBieńkowski Which version of GCC? And what compiler flags are set? I [can't reproduce](http://coliru.stacked-crooked.com/a/c4b9f8743f6980e7) – Anton Savin Jan 27 '16 at 01:09
  • @AntonSavin I have no flags enabled, I don't know the version but my Code::Blocks was installed last Sunday. http://postimg.org/image/epdsrinkx/ – Adam Bieńkowski Jan 27 '16 at 01:17
  • @AntonSavin:`pow` calculates with doubles or long doubles, so `pow(10, 5)` is effectively the same as `pow(10.0, 5.0)`. you get the rounding errors in the calculation itself. – Mooing Duck Jan 27 '16 at 01:22
  • @MooingDuck but `pow(4,5)` works perfectly – Adam Bieńkowski Jan 27 '16 at 01:31
  • Cannot reproduce using CB 13.12 + MinGW-w64 4.9.2 – M.M Jan 27 '16 at 01:36
  • BTW to do the power in max precision use `pow(10.0L, length);` – M.M Jan 27 '16 at 01:37
  • Are you using compiler switch `-std=c++11` or `-std=c++14`? Your code is actually ill-formed in C++03 (the default mode for g++ installations) although when I tested it, older g++ versions erroneously accepted the code. Possibly your version isc alling the wrong version of `pow`. – M.M Jan 27 '16 at 01:41
  • @M.M I don't know why there is "L" letter, but result is the same. I have installed package named 'codeblocks-13.12mingw-setup-TDM-GCC-481.exe' – Adam Bieńkowski Jan 27 '16 at 01:42
  • 1
    `10.0L` means the number `10` as a `long double`, so that the version of `pow` which operates in long double precision is selected. Try again with `-std=c++11` compiler switch. If you still get the problem then perhaps it is a bug in TDM-GCC 4.8.1. In any case you should update to a more recent compiler. – M.M Jan 27 '16 at 01:50
  • @M.M Unfortunately no change with that flag. What do You think about using `floor()` or multiplying with `for()` loop? – Adam Bieńkowski Jan 27 '16 at 02:10
  • `floor` probably isn't a good idea. If your compiler allows a positive error then it might also allow a negative error in which case floor would give the wrong result. Doing the calculation in integer precision is obviously an option, although that will be limited by the size of the integer. – M.M Jan 27 '16 at 02:16
  • @M.M I will have only positive Integers, but ints have to little space for me :( So the solution probably will be to multiple over the loop. – Adam Bieńkowski Jan 27 '16 at 02:26

0 Answers0