Opened 10 years ago
Last modified 10 years ago
#11646 new enhancement
Significance arithmetic
Reported by: | eviatarbach | Owned by: | AlexGhitza |
---|---|---|---|
Priority: | major | Milestone: | sage-feature |
Component: | basic arithmetic | Keywords: | |
Cc: | Merged in: | ||
Authors: | Reviewers: | ||
Report Upstream: | N/A | Work issues: | |
Branch: | Commit: | ||
Dependencies: | Stopgaps: |
Description (last modified by )
I am working on implementing significance arithmetic in Sage (i.e., maintaining significant digits under arithmetic operations). This can be accomplished through a special structure which, upon instantiation, determines the amount of sig. figs and coerces into a RealField.
For example,
sage: SF('787') * SF('3.0') 2.4e3
Change History (5)
comment:1 Changed 10 years ago by
- Description modified (diff)
comment:3 Changed 10 years ago by
Check out python's decimal
module. It might already provide what you are looking for.
comment:4 follow-up: ↓ 5 Changed 10 years ago by
Not quite. It does not preserve significant figures in multiplication; 1.30 * 1.20 gives 1.5600.
comment:5 in reply to: ↑ 4 Changed 10 years ago by
That is a surprising implementation choice. It looks like the precision is again just part of the context, not of a particular number. But apparently they do allow a number to be "underspecified" (have fewer digits than the precision of the context in force). Indeed, if you want the precision to be an attribute of each individual number, this module does not do what you want. You could build on top of it (or hack it) if you want to faithfully mimic pen-and-paper float arithmetic.
If you are mainly interested in keeping track of how inaccuracies propagate through a computation, you should look at the interval arithmetic already in place in Sage, though. It should track loss of precision more accurately than keeping track of the number of "significant digits".
sage: RIF((786.5,787.5)) 787.? sage: RIF((2.95,3.05)) 3.0? sage: RIF((786.5,787.5))*RIF((2.95,3.05)) 2.4?e3
Shouldn't the result display as something like
2.4 * 10^3
(2.4e3
) or24 * 10^2
(24e2
)?