%mathpiper,def="RationalizeNumber"

Function("RationalizeNumber",{x})
[
  Check(IsNumber(x), "Argument", "RationalizeNumber: Error: " : (PipeToString()Write(x)) :" is not a number");
  Local(n,i,bip,m);
  n   := 1;
  i   := 0;
  bip := BuiltinPrecisionGet();
  // We can not take for granted that the internal representation is rounded properly...
  While(i<=bip And Not(FloatIsInt(x)))
  [
    n := n*10; 
    x := x*10;
    i := i+1;
  ];
  m := Floor(x+0.5);
  (m/n);
];

%/mathpiper

