1. Suggested implementation of mod()

Computing mod(x,y)=x-y*floor(x/y) is notionally correct, but numerically wrong:
?1e20-1.7*floor(1e20/1.7)
?remainder(1e20,1.7)
?machine_func(26,0)

The two displayed real numbers should agree, since both operands are positive.
Yet, one gets:
0
1.689469706

Since 10 is not divisible by 17, which is a prime number, the first result
cannot ever be correct. The issue is not so much that 1.7 isn't exactly
represented in hardware. The roundoff error which results from substracting two
large numbers from one another is much bigger, as the results show.
mod() must be built-in, like remainder(). Adjusting mod() from remainder() is
slower, but at least the results will be numerically correct.

Btw, the correct answer is 0.9. The Windows calulator displays the correct
result, using the Mod function.

CChris

new topic     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu