In Julia you can use Rational
types to represent rational numbers without losing precision. See the docs here.
However, you will still need to be very careful with your computations not to promote Rational
to Real
.
Your example of 1.6
could be represented as follows:
> x = 16//10
8//5
Julia would then remember the denominator and numerator and store the value as such. You could use these rational numbers to compare your derived value to the true value.
If at the end you need to convert back to floating point numbers (because for example your result was irrational) than you can use the BigFloat
type to get higher precision. Rational
numbers would give you precision improvement even when using BigFloats
:
> @printf "%.100f" BigFloat(1.6 - 1.5)
0.1000000000000000888178419700125232338905334472656250000000000000000000000000000000000000000000000000
> @printf "%.100f" BigFloat(16//10 - 15//10)
0.1000000000000000000000000000000000000000000000000000000000000000000000000000002159042138773611156347