Opened 11 years ago

# Significance arithmetic

Reported by: Owned by: eviatarbach AlexGhitza major sage-feature basic arithmetic N/A

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
```

### comment:1 Changed 11 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 11 years ago by eviatarbach

• Description modified (diff)

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

### comment:3 Changed 11 years ago by nbruin

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

### comment:4 follow-up: ↓ 5 Changed 11 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 11 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))
787.?
sage: RIF((2.95,3.05))
3.0?
sage:  RIF((786.5,787.5))*RIF((2.95,3.05))
2.4?e3
```
Note: See TracTickets for help on using tickets.