在Objective C中将十进制转换为分数(有理数)?

时间:2012-04-19 17:53:02

标签: objective-c math rational-numbers

作为计算器应用程序的一部分,我正在尝试使用sigma表示法。但是,它输出的结果总是小数,其余的并不重要。我只想将小数更改为分数。

我已经有了reduce函数,我遇到的问题是从这样的十进制:'0.96875'到它的小数值,'31 / 32'

谢谢!

PS:我已经研究了几乎所有事情,而对于我的生活,我无法弄清楚这一点。此时我需要的是如何从中取出小数,然后我可以减少它。

这是我的reduce方法:

    -(void)reduce {

    int u = numerator;
    int v = denominator;
    int temp;

    while (v != 0) {
        temp = u % v;
        u = v;
        v = temp;
    }

    numerator /= u;
    denominator /= u;

}

3 个答案:

答案 0 :(得分:5)

我自己发现了这个。我所做的是将分子和分母乘以1000000(回想小数看起来像.96875 / 1),使它看起来像96875/100000

然后,我使用这种reduce方法将其降为最低值:

    -(void)reduce {

    int u = numerator;
    int v = denominator;
    int temp;

    while (v != 0) {
        temp = u % v;
        u = v;
        v = temp;
    }

    numerator /= u;
    denominator /= u;

}

最后,我使用了一种打印方法将其转换为分数形式:

//In the .h
@property int numerator, denominator, mixed;
-(void)print;

//In the .m       
@synthesize numerator, denominator, mixed;

-(void)print {
    if (numerator > denominator) {
        //Turn fraction into mixed number
        mixed = numerator/denominator;
        numerator -= (mixed * denominator);
        NSLog(@"= %i %i/%i", mixed, numerator, denominator);
    } else if (denominator != 1) {
        //Print fraction normally
        NSLog(@"= %i/%i", numerator, denominator);
    } else {
        //Print as integer if it has a denominator of 1
        NSLog(@"= %i", numerator);
    }
}

得到了我想要的输出:

31/32

答案 1 :(得分:0)

我找到了一段相当好的方法,虽然我不记得从哪里来。无论如何,它像这样递归地工作(这是伪代码,而不是C):

function getRational(float n)
  let i = floor(n); (the integer component of n)
  let j = n - i;
  if j < 0.0001 (use abritrary precision threshold here), return i/1
  let m/n = getRational(1 / j)
  return ((i * m) + n) / m

例如,以3.142857为出发点。

i = 3
j = 0.142857
m/n = getRational(7)
  i = 7
  j = 0
  return 7/1
m/n = 7/1
return ((3*7)+1) / 7 = 22/7

或者更复杂的例子,1.55:

i = 1
j = 0.55
m/n = getRational(1.81818181)
  i = 1
  j = 0.81818181
  m/n = getRational(1.22222222)
    i = 1
    j = 0.22222222
    m/n = getRational(4.5)
      i = 4
      j = 0.5
      m/n = getRational(2)
        i = 2
        j = 0
        return 2/1
      m/n = 2/1
      return ((4*2)+1)/2 = 9/2
    m/n = 9/2
    return ((1*9)+2)/9 = 11/9
  m/n = 11/9
  return ((1*11)+9)/11) = 20/11
m/n = 20/11
return ((1*20)+11)/20 = 31/20

我用PI尝试了一次。它可能会持续一段时间,但如果你将你的阈值设置为0.01,它只会在返回355/113之前减少一些递归。

有一些问题,你可能会得到一个过大的整数,如果它返回的时候太深了;我没有真正考虑过这样做的好方法,除了将精度阈值设置为相当宽松的东西,例如0.01。

答案 2 :(得分:0)

试试这个:

-(NSString *)convertToFraction:(CGFloat)floatValue{
    double tolerance = 1.0E-6;
    CGFloat h1 = 1;
    CGFloat h2 = 0;
    CGFloat k1 = 0;
    CGFloat k2 = 1;
    CGFloat b = floatValue;
    do{
        CGFloat a = floor(b);
        CGFloat aux = h1;
        h1 = a*h1+h2;
        h2 = aux;
        aux = k1;
        k1 = a*k1+k2;
        k2 = aux;
        b = 1/(b-a);
    }while (ABS(floatValue-h1/k1) > floatValue*tolerance) ;

    return k1 > 1 ?  [NSString stringWithFormat:@"%.0f/%.0f",h1,k1] : [NSString stringWithFormat:@"%.0f",h1];
}