Having just succeeded in compiling a program using MPFR C++ (1st time), I need to print out some fairly large numbers, but they only come out as engineering notations instead of the whole numbers. From what I read, the library is not optimized for (arbitrary) integers, so it's fine to use floating point numbers as integers, since they won't be having any decimals. I am only a beginner in C++, so I don't know my way through all the tools available, but is there a way to print out the (big) floating point numbers as if they were integers? As an example, instead of (say) 1.12276e+44
, print 112275575285571389562324404930670903477890625
. If I try std::cout.precision(44)
, I get 1.12275575285571389562324404930670903477890625e+44
, which doesn't look any better.
Asked
Active
Viewed 1,040 times
0

a concerned citizen
- 787
- 2
- 9
- 25
-
Have you tried [using any of the other floating point output formats](http://en.cppreference.com/w/cpp/io/manip/fixed)? – Some programmer dude Sep 16 '16 at 09:18
-
What happens if you add `std::fixed`? source: http://en.cppreference.com/w/cpp/io/manip/fixed – Richard Critten Sep 16 '16 at 09:25
-
@JoachimPileborg No, I haven't. Thank you for posting the link. `std::fixed` seem to get close to what I want, which is not printing the decimals. Is that the only way do do it? – a concerned citizen Sep 16 '16 at 09:28
-
You could always roll your own print. Note that after approx 9 digits for float and 18 digits for double you are not printing accurate (meaningful might be better) values due to the inherent precision of the above data types. – Richard Critten Sep 16 '16 at 09:38
-
@RichardCritten I am using MPFR C++, shouldn't that give me the precision I need? – a concerned citizen Sep 16 '16 at 09:40
-
Sorry missed that – Richard Critten Sep 16 '16 at 09:41
1 Answers
3
MPFR C++ allows precise tuning of output format a la printf
style (if standard C++ capabilities is not enough). Example:
std::cout<<x.toString("%34.0RNf")
Please refer to MPFR manual for format specification in brackets. Also you might check this question: https://stackoverflow.com/a/9627660/479995

Community
- 1
- 1

Pavel Holoborodko
- 528
- 2
- 11
-
Thank you for the answer. I don't suppose there is a way to actually work with big integers, rather than floating points? My need is generating polynomials and the order can get up to 50, but I'm still considering whether I should cap it, or let the users have fun... – a concerned citizen Sep 16 '16 at 09:46
-
Actually MPFR is not the library for big integers and it is not good idea to simulate them with floating point types. GMP/MPIR are specifically focused on this (see mpz_t type), there are some C++ wrappers exist for the libraries. – Pavel Holoborodko Sep 16 '16 at 09:49
-
Floating point type has fixed number of bits to store number - so that really big integer will be rounded to this number of bits. Native big-int library grows number storage to keep number exactly. – Pavel Holoborodko Sep 16 '16 at 09:54
-
GMP was the first one suggested, but it lacked even sin/cos, then MPFR was suggested, but it was too clunky and confusing, until I saw the wrappers. The thing is, I also need to find out the roots of those polynomials, so, if I do find the way to use actual integers, I think the next wall to hit will be how to use an integer as a float argument in the root finding function. Using floats instead of integers seems to already give me headaches, as I see decimals where there shouldn't be, but that may be my generating of the polynimials. – a concerned citizen Sep 16 '16 at 09:56