Reputation: 196
I've written a small program meant to count the digits of a Fibonacci number, but I've had to use MPFR and GMP because of how large the numbers get. I am getting the correct values, but I need to be able to round them up to the nearest integer, and the rounding modes included with MPFR round, predictably, to float values. Is there a simple way to perform mpfr_t rounding to an int?
Upvotes: 1
Views: 1000
Reputation: 5636
MPFR has integer related functions; round, floor, ceil, truncate, and nearest integer to convert integers
int mpfr_rint (mpfr t rop, mpfr t op, mp rnd t rnd)
int mpfr_ceil (mpfr t rop, mpfr t op)
int mpfr_floor (mpfr t rop, mpfr t op)
int mpfr_round (mpfr t rop, mpfr t op)
int mpfr_trunc (mpfr t rop, mpfr t op)
Set rop to op rounded to an integer. mpfr_rint rounds to the nearest representable integer in the given rounding mode, mpfr_ceil rounds to the next higher or equal representable integer, mpfr_floor to the next lower or equal representable integer, mpfr_round to the nearest representable integer, rounding halfway cases away from zero (as in the roundTiesToAway mode of IEEE 754-2008), and mpfr_trunc to the next representable integer toward zero.
Then use the conversions like mpfr_get_z
to get mpz_t
.
Upvotes: 0
Reputation: 7925
You can first use mpfr_get_z
and then GMP functions, or directly mpfr_get_ui
or mpfr_get_si
.
Upvotes: 1