简体   繁体   中英

Rounding to Integer with MPFR

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?

您可以先使用mpfr_get_z再使用 GMP 函数,或者直接使用mpfr_get_uimpfr_get_si

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM