Maxima Function
rationalize (expr)
Convert all double floats and big floats in the Maxima expression
expr to their exact rational equivalents. If you are not familiar with
the binary representation of floating point numbers, you might
be surprised that rationalize (0.1)
does not equal 1/10. This behavior
isn't special to Maxima -- the number 1/10 has a repeating, not a terminating,
binary representation.
(%i1) rationalize (0.5); 1 (%o1) - 2 (%i2) rationalize (0.1); 1 (%o2) -- 10 (%i3) fpprec : 5$ (%i4) rationalize (0.1b0); 209715 (%o4) ------- 2097152 (%i5) fpprec : 20$ (%i6) rationalize (0.1b0); 236118324143482260685 (%o6) ---------------------- 2361183241434822606848 (%i7) rationalize (sin (0.1*x + 5.6)); x 28 (%o7) sin(-- + --) 10 5
Example use:
(%i1) unitfrac(r) := block([uf : [], q], if not(ratnump(r)) then error("The input to 'unitfrac' must be a rational number"), while r # 0 do ( uf : cons(q : 1/ceiling(1/r), uf), r : r - q), reverse(uf)); (%o1) unitfrac(r) := block([uf : [], q], if not ratnump(r) then error("The input to 'unitfrac' must be a rational number"), 1 while r # 0 do (uf : cons(q : ----------, uf), r : r - q), 1 ceiling(-) r reverse(uf)) (%i2) unitfrac (9/10); 1 1 1 (%o2) [-, -, --] 2 3 15 (%i3) apply ("+", %); 9 (%o3) -- 10 (%i4) unitfrac (-9/10); 1 (%o4) [- 1, --] 10 (%i5) apply ("+", %); 9 (%o5) - -- 10 (%i6) unitfrac (36/37); 1 1 1 1 1 (%o6) [-, -, -, --, ----] 2 3 8 69 6808 (%i7) apply ("+", %); 36 (%o7) -- 37