Say you have a situation where you divide and then multiply a float, and you need to guarantee that it survives macheps (ie multiplication output equals division input). What are known strategies for guaranteeing this? Rounding would work, but anything else?
- Anybody can ask a question
- Anybody can answer
- The best answers are voted up and rise to the top
Traditionally, you multiply FIRST, and THEN you divide.
If your hardware and compiler are smart enough, the instructions emitted will preserve the full double-width result of the multiplication, for processing by the division.
Dividing first and then multiplying, you can't really do that. (Your compiler MIGHT at that point be smart enough to reorder the operations, but you are usually safer doing it yourself.)
Read Hamming's Numerical Methods for Scientists and Engineers. Early in the book, he goes into considerable detail on reorganizing your computations to reduce this kind of trouble. It's available from Dover. (If technical literature is your fetish, like it is mine, Dover is your FRIEND!)
Actually this error happens not only because of multiplication, it can be reproduced by a simplier way:
There is a special website for this issue: http://floating-point-gui.de/
You can't guarantee that. Not while using floats, anyway. As the comment says, if you need a guarantee, you can use arbitrary-precision fractions.
In general, rounding doesn't offer such a guarantee, either -- you need to do some actual math before you can be confident it works within the range of your particular application.
The real question is: why do you think you need a guarantee? Your question suggests that you need to actually, seriously devote some effort to considering this, because its answer is likely the most relevant and important part of actually solving your problem, and you have not provided enough information to enable any more detailed constructive advice.