11

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?

Alexey Popkov
  • 61,809
  • 7
  • 149
  • 368
David Zhang
  • 2,316
  • 15
  • 25

2 Answers2

22

SetPrecision[] does this:

SetPrecision[0.1, ∞]
   3602879701896397/36028797018963968
J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
11

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.

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371