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:

Status badges

Description (last modified by eviatarbach)

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')

Change History (5)

comment:1 Changed 10 years ago by leif

  • Description modified (diff)

Shouldn't the result display as something like 2.4 * 10^3 (2.4e3) or 24 * 10^2 (24e2)?

comment:2 Changed 10 years ago by eviatarbach

  • Description modified (diff)

Yes, that's correct. I will fix it.

comment:3 Changed 10 years ago by nbruin

Check out python's decimal module. It might already provide what you are looking for.

comment:4 follow-up: Changed 10 years ago by eviatarbach

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 nbruin

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))
sage: RIF((2.95,3.05))
sage:  RIF((786.5,787.5))*RIF((2.95,3.05))
Note: See TracTickets for help on using tickets.