"how numbers are stored in computers"
When subtracting two floating-point numbers x - y
, we usually lose some precision, especially if x
and y
are very close. However, under certain conditions where x
and y
are close but not too close, a guard digit allows us to perform an exact subtraction when
We can create a rudimentary mechanism for detecting precision loss during subtraction by scaling up the values before we subtract them, then scaling down the difference by the same factor.
code.ts1function checkSubtraction(x: number, y: number, factor = 1000) { 2 const diff = x - y 3 const scaledDiff = (x * factor - y * factor) / factor 4 5 console.log(`normal: ${ x } - ${ y } = ${ diff }`) 6 console.log(`scaled: ${ x } - ${ y } = ${ scaledDiff }`) 7} 8 9// Try values satisfying y/2 <= x <= 2y 10checkSubtraction(0.3, 0.15) 11checkSubtraction(0.29, 0.15) 12checkSubtraction(0.15, 0.08) 13checkSubtraction(0.15, 0.075)
Intuition suggests that this is true - now let's examine how we would construct a rigorous proof of this.
If
When
References
1. What Every Computer Scientist Should Know About Floating-Point Arithmetic David Goldberg