16

我正在尝试将用户键入的具有小数结果的计算转换为分数。例如;66.6666666667 成 66 2/3。任何指针?提前感谢

4

7 回答 7

32

连分数可用于找到严格意义上的“最佳”实数的有理逼近。这是一个 PHP 函数,它找到给定(正)浮点数的有理逼近,其相对误差小于$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

我已经写了更多关于这个算法及其工作原理的内容,甚至还有一个 JavaScript 演示:https ://web.archive.org/web/20180731235708/http://jonisalonen.com/2012/converting-decimal-numbers-to -比率/

于 2013-01-16T11:19:46.000 回答
7

在这种情况下,法雷分数可能非常有用。

它们可用于将任何小数转换为分母最小的分数。

抱歉 - 我没有 PHP 原型,所以这里有一个 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
于 2013-01-15T03:40:27.067 回答
7

根据@Joni 的回答,这是我用来提取整数的内容。

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";
}
于 2014-10-31T23:44:41.703 回答
7

将答案中的 Python 代码从 @APerson241 转换为 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
于 2013-02-18T16:22:33.383 回答
1

基于@APerson 和@Jeff Monteiro 的回答,我创建了Farey 分数的PHP 版本,它将被简化为具有最低分母的分数的整数:

<?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;
            }
        }
    }
}

然后你像这样使用它:

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
于 2021-04-14T08:43:52.200 回答
0

这是我解决这个问题的方法。适用于有理数。

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;
}
于 2014-03-26T04:29:10.950 回答
0

有时有必要只处理浮点数的小数。所以我创建了一个代码,它使用@Joni 创建的函数来呈现一种在烹饪食谱中很常见的格式,至少在巴西是这样。

因此,不是使用 3/2(即 1.5 的结果),而是使用我创建的函数,可以显示值 1 1/2,如果需要,还可以添加一个字符串来连接这些值,创建类似“1 和 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;
    }
  }
}

用法例如:

echo float2fraction(1.5);
will return "1 1/2"
于 2020-07-15T14:53:00.953 回答