S
Somebody
Hi guys... quick question about floats...
I have a point "int x", and a width "int cx". In order to support scaling, I
store this as "double ratio = x / cx" which invariably led to round off
errors when I tried to re-calculate the new position from the ratio as in:
x = ratio * cx;
I tried to solve this by multiplying the numerator by 10,000 to gain more
precision... recent testing showed that I had not solved the problem in all
cases. Thinking about it, no matter how many times I multiply the numerator
of a fraction, I'll never be able to recover 100 from 1/3 (0.333333....) for
example.
I came up with this idea: instead of storing "double ratio = x / cx", I will
store int numerator=x, int denominator = cx;
That way, in my case off 1/3, I'll be able to fully recover the original
value. Ie...:
original pos = 33
original width = 100
num = 33;
denom = 100;
new width = num * 100 / denom = 33;
Does this sound like a reasonable solution (with ABSOLUTELY ZERO LOSS OF
PRECISION)? Or is there a better way?
Thanks!
I have a point "int x", and a width "int cx". In order to support scaling, I
store this as "double ratio = x / cx" which invariably led to round off
errors when I tried to re-calculate the new position from the ratio as in:
x = ratio * cx;
I tried to solve this by multiplying the numerator by 10,000 to gain more
precision... recent testing showed that I had not solved the problem in all
cases. Thinking about it, no matter how many times I multiply the numerator
of a fraction, I'll never be able to recover 100 from 1/3 (0.333333....) for
example.
I came up with this idea: instead of storing "double ratio = x / cx", I will
store int numerator=x, int denominator = cx;
That way, in my case off 1/3, I'll be able to fully recover the original
value. Ie...:
original pos = 33
original width = 100
num = 33;
denom = 100;
new width = num * 100 / denom = 33;
Does this sound like a reasonable solution (with ABSOLUTELY ZERO LOSS OF
PRECISION)? Or is there a better way?
Thanks!