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

Harry :

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
Solomon Ucko :

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.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=31818&siteId=1