11
$\begingroup$

The Python programming language has a float.as_integer_ratio(x) function which exactly converts an IEEE 754 floating-point number into a numerator/denominator pair of integers. For example:

float.as_integer_ratio(0.1)
 => (3602879701896397, 36028797018963968)

What is the Mathematica equivalent of this function for MachinePrecision numbers?

$\endgroup$
22
$\begingroup$

SetPrecision[] does this:

SetPrecision[0.1, ∞]
   3602879701896397/36028797018963968
$\endgroup$
  • $\begingroup$ Indeed it does. Thanks! $\endgroup$ – David Zhang May 24 '15 at 4:33
  • 4
    $\begingroup$ SetAccuracy appears to be equivalent. (+1) $\endgroup$ – Mr.Wizard May 24 '15 at 10:13
11
$\begingroup$

Not as clean as J.M.'s method but this seems to give the same result:

 0.1 ~RealDigits~ 2 ~FromDigits~ 2
3602879701896397/36028797018963968

Follow with Numerator and Denominator if needed.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.