简体   繁体   中英

Python/Mpmath: Why don't I get any decimal points for large number division, but do for smaller numbers

Why does the large number give me an integer (or at least no decimal points), but the smaller number give me a bunch of decimal points? Is the way I set the precision or declare the variables wrong?

import math
from mpmath import *
mp.prec=1000

x = 5431526412865007456
print mpf((x)/6)

ACTUAL OUTPUT: 905254402144167909.0
WANTED OUTPUT: 905254402144167909.3333333333333333333333(…)

x = 5431526413
print mpf((x)/6.)

OUTPUT: 905254402.16666662693023681640625

Try using mpf(x)/6 or mpf(x)/6.0 . The reason your code didn't work is that it did the division using Python's normal rules, then converted it to a arbitrary-precision number, whereas this converts it first so the division is done using arbitrary-precision math.

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