src/Chippyash/Type/Number/Rational/RationalTypeFactory.php
public static function fromFloat($float, $tolerance = null)
    {
        if ($float instanceof FloatType) {
            $float = $float();
        }
        if ($float == 0.0) {
            return new RationalType(new IntType(0), new IntType(1));
        }
        if ($tolerance instanceof FloatType) {
            $tolerance = $tolerance();
        } elseif (is_null($tolerance)) {
            $tolerance = self::$defaultTolerance;
        }

        $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 self::createCorrectRational($num1, $den1);
    }
public static function fromFloat($float, $tolerance = null)
    {
        if ($float instanceof FloatType) {
            $float = $float();
        }
        if ($float == 0.0) {
            return new RationalType(new IntType(0), new IntType(1));
        }
        if ($tolerance instanceof FloatType) {
            $tolerance = $tolerance();
        } elseif (is_null($tolerance)) {
            $tolerance = self::$defaultTolerance;
        }

        $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 self::createCorrectRational($num1, $den1);
    }
public static function fromFloat ( $ float , $ tolerance = null ) { if ( $ float instanceof FloatType ) { $ float = $ float ( ) ; } if ( $ float == 0.0 ) { return new RationalType ( new IntType ( 0 ) , new IntType ( 1 ) ) ; } if ( $ tolerance instanceof FloatType ) { $ tolerance = $ tolerance ( ) ; } elseif ( is_null ( $ tolerance ) ) { $ tolerance = self :: $ defaultTolerance ; } $ 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 self :: createCorrectRational ( $ num1 , $ den1 ) ; }
Create a rational number from a float or FloatType
Use Continued Fractions method of determining the rational number

@param float|FloatType $float
@param float|FloatType $tolerance -
Default is whatever is currently set but normally self::CF_DEFAULT_TOLERANCE

@return \Chippyash\Type\Number\Rational\RationalType|\Chippyash\Type\Number\Rational\GMPRationalType

@throws \InvalidArgumentException
Create a rational number from a float or FloatType Use Continued Fractions method of determining the rational number