Python 阻止 fractions.Fraction 减少分数
Python prevent fractions.Fraction from reducing fractions
所以我有一个简单的例子:
import fractions
f = fractions.Fraction(6, 12)
而且我不希望 f
变成 1/2
。我希望它保持 6/12
。有办法吗?
找到解决方案here:
No, not really. There's an internal _normalize
flag that can be set, allowing the creation of non-normalized fractions (and probably leading to much undefined behavior).
>>> f = fractions.Fraction(6, 12, _normalize=False)
>>> f
Fraction(6, 12)
But it wouldn't do anything for the results of calculations. Part of the use of the module is to allow equal fractions to be found equal, and that's done through normalization. So it looks to be a core part that can't be disabled.
>>> frac(1,2) == frac(6,12, _normalize=False)
False
所以我有一个简单的例子:
import fractions
f = fractions.Fraction(6, 12)
而且我不希望 f
变成 1/2
。我希望它保持 6/12
。有办法吗?
找到解决方案here:
No, not really. There's an internal
_normalize
flag that can be set, allowing the creation of non-normalized fractions (and probably leading to much undefined behavior).
>>> f = fractions.Fraction(6, 12, _normalize=False)
>>> f
Fraction(6, 12)
But it wouldn't do anything for the results of calculations. Part of the use of the module is to allow equal fractions to be found equal, and that's done through normalization. So it looks to be a core part that can't be disabled.
>>> frac(1,2) == frac(6,12, _normalize=False)
False