1 | initial version |
Recommended reading:
Some ideas:
RealField(k)
for reals with k
bits of precisionRealBallField
or ComplexBallField
Chopping (also know as truncating) might mean different things:
base^-N
or does it depend on the size of the number?For example, truncating the following numbers to 2 decimal places:
$$ a = 23456.789 $$ $$ b = 2.3456789 $$ $$ c = 0.00023456789 $$
do you expect the result to be
$$ a' = 23456.78 $$ $$ b' = 2.34 $$ $$ c' = 0.0 $$
or
$$ a' = 23000.00 $$ $$ b' = 2.3 $$ $$ c' = 0.0023 $$
?
The following function could be a starting point:
def chop(x, precision=3, base=10, absolute=True):
bp = base^precision
if absolute:
return RDF(floor(bp*x)/bp)
# otherwise truncate to relative precision
size = floor(log(x, base)) + 1
bs = base^size
return chop(x/bs, precision=precision, base=base, absolute=True)*bs
Define a printing and formatting function for illustration:
def chop_list(aa):
format = "%20s %20s %20s"
print()
print(format % ('number', 'chop_absolute', 'chop_relative'))
for a in aa:
b = chop(a, precision=2, base=10, absolute=True)
c = chop(a, precision=2, base=10, absolute=False)
print("%20s %20s %20s" % (a, b, c))
Check out the results:
sage: aa = [23456.789, 2.3456789, 0.0023456789]
sage: chop_list(aa)
number chop_absolute chop_relative
23456.7890000000 23456.78 23000.0
2.34567890000000 2.34 2.3000000000000003
0.00234567890000000 0.0 0.0023
Now for rounding instead of truncating, use round
instead of floor
.