17

I am trying to convert calculations keyed in by users with decimal results into fractions. For e.g.; 66.6666666667 into 66 2/3. Any pointers? Thanx in advance

Joni
  • 108,737
  • 14
  • 143
  • 193
Joe Shamuraq
  • 1,245
  • 3
  • 18
  • 32
  • 1
    66 2/3 != 66.6666666667, so you will have to make guesses. – NullUserException Jan 15 '13 at 03:32
  • 2
    But that's not 66 2/3! It's 666666666667/10000000000. How will you tell the difference? What's a "reasonable" rounding? – Ry- Jan 15 '13 at 03:32
  • The output is 66.66666667 when it should be endless of 6s. So how do i change the output to 2/3? Cannot do rounding else the finalised answer would not be 2/3... – Joe Shamuraq Jan 15 '13 at 03:33
  • Possibly relevant: http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html – Lusitanian Jan 15 '13 at 03:34
  • You didn't answer the question... what's a reasonable rounding? Will someone ever really mean 66.66666667? If not, why not? (Re: edit: Yes, 666666666667/10000000000 can be rounded to 200/3, it's just not the usual kind of rounding.) – Ry- Jan 15 '13 at 03:34
  • 2
    Obviously you cannot have "endless" 6s without endless memory. – NullUserException Jan 15 '13 at 03:34
  • You either have to keep the original operands (200 and 3 in this case), or guess/assume that the 6s should repeat infinitely. These are the limitations of [floating point arithmetic](http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems). – Gordon Bailey Jan 15 '13 at 03:36
  • @minitech - Cannot do premature rounding. There's more calculations following that. – Joe Shamuraq Jan 15 '13 at 03:36
  • @TeamStar: If you can't round it, then there is no way you can tell the difference. – Ry- Jan 15 '13 at 03:36
  • 1
    Here's my [C++ implementation](http://stackoverflow.com/a/7563694/922184) of a [continued fractions](http://en.wikipedia.org/wiki/Continued_fraction) approach to exacting a fraction from a float. Not sure how hard it would be to translate to PHP, but it might be worth taking a look at. – Mysticial Jan 15 '13 at 03:52

7 Answers7

35

Continued fractions can be used to find rational approximations to real numbers that are "best" in a strict sense. Here's a PHP function that finds a rational approximation to a given (positive) floating point number with a relative error less than $tolerance:

<?php
function float2rat($n, $tolerance = 1.e-6) {
    $h1=1; $h2=0;
    $k1=0; $k2=1;
    $b = 1/$n;
    do {
        $b = 1/$b;
        $a = floor($b);
        $aux = $h1; $h1 = $a*$h1+$h2; $h2 = $aux;
        $aux = $k1; $k1 = $a*$k1+$k2; $k2 = $aux;
        $b = $b-$a;
    } while (abs($n-$h1/$k1) > $n*$tolerance);

    return "$h1/$k1";
}

printf("%s\n", float2rat(66.66667)); # 200/3
printf("%s\n", float2rat(sqrt(2)));  # 1393/985
printf("%s\n", float2rat(0.43212));  # 748/1731

I have written more about this algorithm and why it works, and even a JavaScript demo here: https://web.archive.org/web/20180731235708/http://jonisalonen.com/2012/converting-decimal-numbers-to-ratios/

Kenny
  • 24
  • 6
Joni
  • 108,737
  • 14
  • 143
  • 193
  • 2
    There's an small bug in the function, in case `$b == $a` will throw a warning, "Division by 0." – adrian7 Apr 11 '13 at 15:00
  • Well spotted @adrian7, I didn't know this aspect of PHP -- in other languages floating point division by 0 is well behaved. In case `$b == $a` the loop is in its last iteration, so moving the division from the end of the loop to the beginning fixes the problem. I've updated the code. – Joni May 24 '13 at 09:35
  • Since it was trivial, I took the freedom to add the handling of negative numbers. I hope it's something that can be done per SO rules :| – ZioBit Jul 03 '18 at 12:58
8

Farey fractions can be quite useful in this case.

They can be used to convert any decimal into a fraction with the lowest possible denominator.

Sorry - I don't have a prototype in PHP, so here's one in Python:

def farey(v, lim):
    """No error checking on args.  lim = maximum denominator.
        Results are (numerator, denominator); (1, 0) is 'infinity'."""
    if v < 0:
        n, d = farey(-v, lim)
        return (-n, d)
    z = lim - lim   # Get a "zero of the right type" for the denominator
    lower, upper = (z, z+1), (z+1, z)
    while True:
        mediant = (lower[0] + upper[0]), (lower[1] + upper[1])
        if v * mediant[1] > mediant[0]:
            if lim < mediant[1]:
                return upper
            lower = mediant
        elif v * mediant[1] == mediant[0]:
            if lim >= mediant[1]:
                return mediant
            if lower[1] < upper[1]:
                return lower
            return upper
        else:
            if lim < mediant[1]:
                return lower
            upper = mediant
APerson
  • 8,140
  • 8
  • 35
  • 49
  • Thanx for the kind gesture but am really lost trying to convert that to PHP... Thanx again tho... If anyone can assist this python structure into PHP? – Joe Shamuraq Jan 15 '13 at 04:24
  • The Python code uses tuples as a way to shorten the code greatly. `lower` and `upper` in the code can be treated as just arrays with size 2, which both can be "unpacked" into 2 variables. Most of the code is comparing the items in `lower` and `upper` and setting them equal to one another, so that should be relatively easy to convert into PHP. – APerson Jan 15 '13 at 14:46
  • Thank you @APerson for this, I was able to improve measures in our recipes using this approach. I've added my answer here → https://stackoverflow.com/a/67088369/842480 – Wirone Apr 15 '21 at 08:28
  • There is a logical error in assuming which is closer to v when the limit for the denominator is exceeded. It selects, for example, 17/59 instead of 19/66 for 0.288 when the maximum denominator is specified as 113. – smichr Jun 21 '21 at 17:23
7

Converted Python code in answer from @APerson241 to PHP

<?php
function farey($v, $lim) {
    // No error checking on args.  lim = maximum denominator.
    // Results are array(numerator, denominator); array(1, 0) is 'infinity'.
    if($v < 0) {
        list($n, $d) = farey(-$v, $lim);
        return array(-$n, $d);
    }
    $z = $lim - $lim;   // Get a "zero of the right type" for the denominator
    list($lower, $upper) = array(array($z, $z+1), array($z+1, $z));
    while(true) {
        $mediant = array(($lower[0] + $upper[0]), ($lower[1] + $upper[1]));
        if($v * $mediant[1] > $mediant[0]) {
            if($lim < $mediant[1]) 
                return $upper;
            $lower = $mediant;
        }
        else if($v * $mediant[1] == $mediant[0]) {
            if($lim >= $mediant[1])
                return $mediant;
            if($lower[1] < $upper[1])
                return $lower;
            return $upper;
        }
        else {
            if($lim < $mediant[1])
                return $lower;
            $upper = $mediant;
        }
    }
}

// Example use:
$f = farey(66.66667, 10);
echo $f[0], '/', $f[1], "\n"; # 200/3
$f = farey(sqrt(2), 1000);
echo $f[0], '/', $f[1], "\n";  # 1393/985
$f = farey(0.43212, 2000);
echo $f[0], '/', $f[1], "\n";  # 748/1731
Ole Helgesen
  • 3,191
  • 1
  • 18
  • 8
7

Based upon @Joni's answer, here is what I used to pull out the whole number.

function convert_decimal_to_fraction($decimal){

    $big_fraction = float2rat($decimal);
    $num_array = explode('/', $big_fraction);
    $numerator = $num_array[0];
    $denominator = $num_array[1];
    $whole_number = floor( $numerator / $denominator );
    $numerator = $numerator % $denominator;

    if($numerator == 0){
        return $whole_number;
    }else if ($whole_number == 0){
        return $numerator . '/' . $denominator;
    }else{
        return $whole_number . ' ' . $numerator . '/' . $denominator;
    }
}

function float2rat($n, $tolerance = 1.e-6) {
    $h1=1; $h2=0;
    $k1=0; $k2=1;
    $b = 1/$n;
    do {
        $b = 1/$b;
        $a = floor($b);
        $aux = $h1; $h1 = $a*$h1+$h2; $h2 = $aux;
        $aux = $k1; $k1 = $a*$k1+$k2; $k2 = $aux;
        $b = $b-$a;
    } while (abs($n-$h1/$k1) > $n*$tolerance);

    return "$h1/$k1";
}
Bryan
  • 17,201
  • 24
  • 97
  • 123
5

Based on @APerson's and @Jeff Monteiro's answers I've created PHP version of Farey fractions that will be simplified to whole values with fractions with lowest possible denominator:

<?php

class QuantityTransform
{
    /**
     * @see https://stackoverflow.com/questions/14330713/converting-float-decimal-to-fraction
     */
    public static function decimalToFraction(float $decimal, $glue = ' ', int $limes = 10): string
    {
        if (null === $decimal || $decimal < 0.001) {
            return '';
        }

        $wholeNumber = (int) floor($decimal);
        $remainingDecimal = $decimal - $wholeNumber;

        [$numerator, $denominator] = self::fareyFraction($remainingDecimal, $limes);

        // Values rounded to 1 should be added to base value and returned without fraction part
        if (is_int($simplifiedFraction = $numerator / $denominator)) {
            $wholeNumber += $simplifiedFraction;
            $numerator = 0;
        }

        return (0 === $wholeNumber && 0 === $numerator)
            // Too small values will be returned in original format
            ? (string) $decimal
            // Otherwise let's format value - only non-0 whole value / fractions will be returned
            : trim(sprintf(
                '%s%s%s',
                (string) $wholeNumber ?: '',
                $wholeNumber > 0 ? $glue : '',
                0 === $numerator ? '' : ($numerator . '/' . $denominator)
            ));
    }

    /**
     * @see https://stackoverflow.com/a/14330799/842480
     *
     * @return int[] Numerator and Denominator values
     */
    private static function fareyFraction(float $value, int $limes): array
    {
        if ($value < 0) {
            [$numerator, $denominator] = self::fareyFraction(-$value, $limes);

            return [-$numerator, $denominator];
        }

        $zero = $limes - $limes;
        $lower = [$zero, $zero + 1];
        $upper = [$zero + 1, $zero];

        while (true) {
            $mediant = [$lower[0] + $upper[0], $lower[1] + $upper[1]];

            if ($value * $mediant[1] > $mediant[0]) {
                if ($limes < $mediant[1]) {
                    return $upper;
                }
                $lower = $mediant;
            } elseif ($value * $mediant[1] === $mediant[0]) {
                if ($limes >= $mediant[1]) {
                    return $mediant;
                }
                if ($lower[1] < $upper[1]) {
                    return $lower;
                }

                return $upper;
            } else {
                if ($limes < $mediant[1]) {
                    return $lower;
                }

                $upper = $mediant;
            }
        }
    }
}

Then you san use it like:

QuantityTransform::decimalToFraction(0.06); // 0.06
QuantityTransform::decimalToFraction(0.75); // 3/4
QuantityTransform::decimalToFraction(1.75, ' and '); // 1 and 3/4
QuantityTransform::decimalToFraction(2.33, ' and '); // 2 and 1/3
QuantityTransform::decimalToFraction(2.58, ' ', 5); // 2 3/5
QuantityTransform::decimalToFraction(2.58, ' & ', 10); // 2 & 4/7
QuantityTransform::decimalToFraction(1.97); // 2
Wirone
  • 3,304
  • 1
  • 29
  • 48
0

Here is my approach to this problem. Works fine with rational numbers.

function dec2fracso($dec){
    //Negative number flag.
    $num=$dec;
    if($num<0){
        $neg=true;
    }else{
        $neg=false;
    }

    //Extracts 2 strings from input number
    $decarr=explode('.',(string)$dec);

    //Checks for divided by zero input.
    if($decarr[1]==0){
        $decarr[1]=1;
        $fraccion[0]=$decarr[0];
        $fraccion[1]=$decarr[1];
        return $fraccion;
    }

    //Calculates the divisor before simplification.
    $long=strlen($decarr[1]);
    $div="1";
    for($x=0;$x<$long;$x++){
        $div.="0";
    }

    //Gets the greatest common divisor.
    $x=(int)$decarr[1];
    $y=(int)$div;
    $gcd=gmp_strval(gmp_gcd($x,$y));

    //Calculates the result and fills the array with the correct sign.
    if($neg){
        $fraccion[0]=((abs($decarr[0])*($y/$gcd))+($x/$gcd))*(-1);
    }else{
        $fraccion[0]=(abs($decarr[0])*($y/$gcd))+($x/$gcd);
    }
    $fraccion[1]=($y/$gcd);
    return $fraccion;
}
Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
0

Sometimes it is necessary to treat only the decimals of a float. So I created a code that uses the function created by @Joni to present a format that is quite common in culinary recipes, at least in Brazil.

So instead of using 3/2 which is the result for 1.5, using the function I created it is possible to present the value 1 1/2, and if you want, you can also add a string to concatenate the values, creating something like "1 and 1/2 ".

function float2rat($n, $tolerance = 1.e-6) {
  $h1=1; $h2=0;
  $k1=0; $k2=1;
  $b = 1/$n;
  do {
      $b = 1/$b;
      $a = floor($b);
      $aux = $h1; $h1 = $a*$h1+$h2; $h2 = $aux;
      $aux = $k1; $k1 = $a*$k1+$k2; $k2 = $aux;
      $b = $b-$a;
  } while (abs($n-$h1/$k1) > $n*$tolerance);

  return "$h1/$k1";
}

function float2fraction($float, $concat = ' '){
  
  // ensures that the number is float, 
  // even when the parameter is a string
  $float = (float)$float;

  if($float == 0 ){
    return $float;
  }
  
  // when float between -1 and 1
  if( $float > -1 && $float < 0  || $float < 1 && $float > 0 ){
    $fraction = float2rat($float);
    return $fraction;
  }
  else{

    // get the minor integer
    if( $float < 0 ){
      $integer = ceil($float);
    }
    else{
      $integer = floor($float);
    }

    // get the decimal
    $decimal = $float - $integer;

    if( $decimal != 0 ){

      $fraction = float2rat(abs($decimal));
      $fraction = $integer . $concat . $fraction;
      return $fraction;
    }
    else{
      return $float;
    }
  }
}

Usage e.g:

echo float2fraction(1.5);
will return "1 1/2"
Jeff Monteiro
  • 721
  • 1
  • 7
  • 17