public static function fromFloat($float, $tolerance=1e-7)
    {
        if (is_string($float) && preg_match('~^\-?\d+([,|.]\d+)?$~', $float)) {
            $float = floatval(str_replace(',','.',$float));
        }

        if ($float == 0.0) {
            return new Rational(0,1);
        }
        $negative = ($float < 0);
        if ($negative) {
            $float = abs($float);
        }
        $num1 = 1;
        $num2 = 0;
        $den1 = 0;
        $den2 = 1;
        $oneOver = 1 / $float;
        do {
            $oneOver = 1 / $oneOver;
            $floor = floor($oneOver);
            $aux = $num1;
            $num1 = $floor * $num1 + $num2;
            $num2 = $aux;
            $aux = $den1;
            $den1 = $floor * $den1 + $den2;
            $den2 = $aux;
            $oneOver = $oneOver - $floor;
        } while (abs($float - $num1 / $den1) > $float * $tolerance);
        if ($negative) {
            $num1 *= -1;
        }

        return new Rational(intval($num1), intval($den1));
    }
public static function fromFloat($float, $tolerance=1e-7)
    {
        if (is_string($float) && preg_match('~^\-?\d+([,|.]\d+)?$~', $float)) {
            $float = floatval(str_replace(',','.',$float));
        }

        if ($float == 0.0) {
            return new Rational(0,1);
        }
        $negative = ($float < 0);
        if ($negative) {
            $float = abs($float);
        }
        $num1 = 1;
        $num2 = 0;
        $den1 = 0;
        $den2 = 1;
        $oneOver = 1 / $float;
        do {
            $oneOver = 1 / $oneOver;
            $floor = floor($oneOver);
            $aux = $num1;
            $num1 = $floor * $num1 + $num2;
            $num2 = $aux;
            $aux = $den1;
            $den1 = $floor * $den1 + $den2;
            $den2 = $aux;
            $oneOver = $oneOver - $floor;
        } while (abs($float - $num1 / $den1) > $float * $tolerance);
        if ($negative) {
            $num1 *= -1;
        }

        return new Rational(intval($num1), intval($den1));
    }
public static function fromFloat ( $ float , $ tolerance = 1e-7 ) { if ( is_string ( $ float ) && preg_match ( '~^\-?\d+([,|.]\d+)?$~' , $ float ) ) { $ float = floatval ( str_replace ( ',' , '.' , $ float ) ) ; } if ( $ float == 0.0 ) { return new Rational ( 0 , 1 ) ; } $ negative = ( $ float < 0 ) ; if ( $ negative ) { $ float = abs ( $ float ) ; } $ num1 = 1 ; $ num2 = 0 ; $ den1 = 0 ; $ den2 = 1 ; $ oneOver = 1 / $ float ; do { $ oneOver = 1 / $ oneOver ; $ floor = floor ( $ oneOver ) ; $ aux = $ num1 ; $ num1 = $ floor * $ num1 + $ num2 ; $ num2 = $ aux ; $ aux = $ den1 ; $ den1 = $ floor * $ den1 + $ den2 ; $ den2 = $ aux ; $ oneOver = $ oneOver - $ floor ; } while ( abs ( $ float - $ num1 / $ den1 ) > $ float * $ tolerance ) ; if ( $ negative ) { $ num1 *= - 1 ; } return new Rational ( intval ( $ num1 ) , intval ( $ den1 ) ) ; }
convert float to Rational

Convert float to a continued fraction, with prescribed accuracy

@param string|float $float
@param float $tolerance
@retval Rational
convert float to Rational