Originally Posted by
wysota
Another workaround is not to operate on floating point values as long as you can:
(1/6)*3 = 3*(1/6) = 3/6 = 0.5
Sticking with the above advise. I would like to maintain 2 floating variables.
float numerator = 1, denominator = 1;
float numerator = 1, denominator = 1;
To copy to clipboard, switch view to plain text mode
Whenever you have to multiply fractions, keep multiplying numerators to the numerator, as well as keep multiplying denominators to the denominator.
//multiply (1/6)
numerator *= 1;
denominator *=6;
//multiply (3)
numerator *= 3;
denominator *=1; // since 3 can also be written as (3/1)
//multiply (1/6)
numerator *= 1;
denominator *=6;
//multiply (3)
numerator *= 3;
denominator *=1; // since 3 can also be written as (3/1)
To copy to clipboard, switch view to plain text mode
In the end calculate
result = numerator/denominator
result = numerator/denominator
To copy to clipboard, switch view to plain text mode
Bookmarks