IE:
$x = 0.125
But I want to know what fraction $x is the result of!
In this case: the numerator should be: 1 and the denominator should be 8
another example:
$x = 0.333333333333
the numerator should be 1
the denominator should be 3
This is approximately off couse and that's the trick!
Any ideas?!
I have a JavaScript which works but when I code it in PHP it won't! Please help!
this is the JavaScript:
<script language="JavaScript">
<!--
function RatFrac(x, tol)
{
var tol0 = 1e-10;
var maxIt = 50;
var xi=x;
var d=[];
var n,ni,t;
var i,j;
if (typeof(tol)=='undefined')
{
tol = tol0;
}
for (i=0; i<maxIt; i++)
{
d[i] = Math.round(xi);
xi = 1/(xi-d[i]);
t = d[i];
n = 1;
for (j=i-1; j>=0; j--)
{
ni = n;
n = t;
t = ni+t*d[j];
}
if (n<0)
{
n = -n;
t = -t;
}
if ((Math.abs(t/n-x)<tol))
{
break;
}
}
return [n,t];
}
x = 0.0947;
f = RatFrac(x,1e-10);
n = f[0];
t = f[1];
alert("numerator: " + t + "\n\rdenominator: " + n);
//-->
</script>