编程之美2.6——精确表达浮点数

来源:岁月联盟 编辑:exp 时间:2012-07-07

问题:
用分数形式来表示小数,以达到精确的计算结果。
 
解答一:
用于小整数,将无限循环小数0.a1a2...an(b1b2...bm)分为非循环部分和循环部分。
X=((a1a2...an)*(10^m-1)+b1b2...bm)/((10^m-1)*10^n)
[cpp] 
#include <iostream> 
 
using namespace std; 
 
long long gcd(long long a, long long b) 

    if (a < b) 
    { 
        long long tmp = a; 
        a = b; 
        b = tmp; 
    } 
    int i, k=0; 
    while (b!=0) 
    { 
        if ((a&1) == 0) 
        { 
            if ((b&1) == 0) 
            { 
                // a,b均是偶数,f(a,b)=2*f(a>>1,b>>1) 
                a >>= 1; 
                b >>= 1; 
                k++; 
            } 
            else 
                // a为偶数,b为奇数,f(a,b)=f(a>>1,b) 
                a >>= 1; 
        } 
        else 
        { 
            if ((b&1) == 0) 
                // a为奇数,b为偶数,f(a,b)=f(a,b>>1) 
                b >>= 1; 
            else 
                // a,b均是奇数,f(a,b)=f(a-b,b) 
                a = a-b; 
        } 
        if (a < b) 
        { 
            long long tmp = a; 
            a = b; 
            b = tmp; 
        } 
    } 
    return a << k; 

 
int main() 

    long long a=0, b=0, c=0; 
    // 整数部分c,非循环小数a,循环小数b 
    scanf("%d.%d(%d)",&c, &a, &b); 
    if (a==0 && b==0) 
        cout << c; 
    else 
    { 
        // 分子up,分母down 
        long long up = c; 
        long long down = 1; 
        long long ta = a; 
        while (ta) 
        { 
            down *= 10; 
            ta /= 10; 
        } 
        up = c*down+a; 
        if (b!=0) 
        { 
            long long wb = 1; 
            long long tb = b; 
            while (tb) 
            { 
                wb *= 10; 
                tb /= 10; 
            } 
            up = up*(wb-1)+b; 
            down = down*(wb-1); 
        } 
        long long fac = gcd(up, down); 
        cout << up/fac << "/" << down/fac << endl; 
    } 

 
解答二:
用于大整数,定义了大整数类型,以及对应的加减乘除、比较移位运算。
 
[cpp] 
#include <iostream> 
#include <cstring> 
#include <string> 
using namespace std; 
 
// 大整数类型 
#define MAXLEN 1000 
struct HP {int len, s[MAXLEN];}; 
 
void PrintHP(HP x)  

    for (int i=x.len; i>=1; i--) 
        cout << x.s[i]; 

 
// 字符串转大整数 
void Str2HP(const char *s, HP &x) 

    x.len = strlen(s); 
    for (int i=1; i<=x.len; i++) 
        x.s[i] = s[x.len-i] - '0'; 
    if (x.len == 0) 
    { 
        x.len = 1; 
        x.s[1] = 0; 
    } 

 
// 大整数的加法 
void Plus(const HP a, const HP b, HP &c) 

    int i; c.s[1] = 0; 
    // 大整数a,b的加法操作和结果c的进位操作 
    for (i=1; i<=a.len || i<=b.len || c.s[i]; i++) 
    { 
        if (i <= a.len) c.s[i] += a.s[i]; 
        if (i <= b.len) c.s[i] += b.s[i]; 
        c.s[i+1] = c.s[i]/10; c.s[i] %= 10; 
    } 
    // 退出循环到原因是c.s[i]==0,所以取前一位 
    c.len = i-1;  
    if (c.len == 0) c.len = 1; 

 
// 大整数的减法 
void Subtract(const HP a, const HP b, HP &c) 

    int i, j; 
    for (i=1,j=0; i<=a.len; i++) 
    { 
        // j表示是否要对高位进行借位 
        c.s[i] = a.s[i] - j; 
        if (i <= b.len) c.s[i] -= b.s[i]; 
        if (c.s[i] < 0)  
        { 
            // 向高位借位,补10 
            j = 1; 
            c.s[i] += 10; 
        } 
        else j = 0; 
    } 
    c.len = a.len; 
    while (c.len > 1 && !c.s[c.len]) c.len--; 

 
// 大整数的比较 
int HPCompare(const HP &x, const HP &y) 

    if (x.len > y.len) return 1; 
    if (x.len < y.len) return -1; 
    int i = x.len; 
    while (i>1 && (x.s[i]==y.s[i])) i--; 
    return x.s[i] - y.s[i]; 

 
// 大整数的乘法 
void Multi(const HP a, const HP b, HP &c) 

    int i, j; 
    // 对乘法结果赋初值,以方便之后的+=运算 
    c.len = a.len + b.len; 
    for (i=1; i<=c.len; i++) c.s[i] = 0; 
    for (i=1; i<=a.len; i++) 
        for (j=1; j<=b.len; j++) 
            c.s[i+j-1] += a.s[i]*b.s[j]; 
    // 运算结果进位 
    for (i=1; i<c.len; i++) {c.s[i+1] += c.s[i]/10; c.s[i] %= 10;} 
    // 最高位继续进位 
    while (c.s[i]) {c.s[i+1] = c.s[i]/10; c.s[i] %= 10; i++;} 
    // 确保最高位不为0 
    while (i>1 && !c.s[i]) i--; 
    c.len = i; 

 
// 大整数的除法 
void Divide(const HP a, const HP b, HP &c, HP &d) 

    int i, j; 
    // 用余数d存被除数a的前i位数据,用来多次减去除数b,以得到商c 
    d.len = 1; d.s[1] = 0; 
    for (i=a.len; i>0; i--) 
    { 
        if (!(d.len == 1 && d.s[1] == 0)) 
        { 
            // i没移一位,余数d也移位 
            for (j=d.len; j>0; j--) 
                d.s[j+1] = d.s[j]; 
            d.len++; 
        } 
        d.s[1] = a.s[i]; 
        c.s[i] = 0; 
        // 余数d大于除数b时,才可以进行减操作 
        while ((j=HPCompare(d,b)) >= 0) 
        { 
            Subtract(d, b, d); 
            c.s[i]++; 
            if (j == 0) break; 
        } 
    } 
    c.len = a.len; 
    while (c.len > 1 && c.s[c.len] == 0) 
        c.len--; 

// 十进位右移 
void RightShift(HP &x, int k) 

    for (int i=1; i<=x.len-k; i++) 
        x.s[i] = x.s[i+k]; 
    x.len -= k; 
    if(x.len <= 0) 
    { 
        x.len = 1; 
        x.s[1] = 0; 
    } 

// 十进位左移 
void LeftShift(HP &x, int k) 

    int i; 
    for (i=x.len; i>=1; i--) 
        x.s[i+k] = x.s[i]; 
    for (i=k; i>=1; i--) 
        x.s[i] = 0; 
    x.len += k; 

// 求大整数的最大公约数 
void GCD(HP a, HP b, HP &c) 

    if (b.len == 1 && b.s[1] == 0) 
    { 
        c.len = a.len; 
        memcpy(c.s, a.s, (a.len+1)*sizeof(int)); 
    } 
    else 
    { 
        HP m, n; 
        Divide(a, b, m, n); 
        GCD(b, n, c); 
    } 

 
int main() 

    string str; 
    string strc, stra, strb; 
    cin >> str; 
    int posc = str.find('.'); 
    int posa = str.find('('); 
    int posb = str.find(')'); 
    strc = str.substr(0, posc); 
    if (posc < 0) 
        cout << strc; 
    else 
    {    
        HP a, b, c; 
        HP tmp; tmp.len = 1; tmp.s[1] = 1; 
        // 整数部分 
        Str2HP(strc.c_str(), c); 
        stra = str.substr(posc+1, posa-posc-1); 
        // 非循环部分 
        Str2HP(stra.c_str(), a); 
        // up分子,down分母 
        HP up = c, down = tmp; 
        // 乘以10^|a| 
        LeftShift(down, stra.size()); 
        LeftShift(up, stra.size()); 
        Plus(up, a, up); 
        if (posa >= 0) 
        { 
            strb = str.substr(posa+1, posb-posa-1); 
            // 循环部分 
            Str2HP(strb.c_str(), b); 
            HP m = tmp; 
            LeftShift(m, strb.size()); 
            Subtract(m, tmp, m); 
            // 乘以10^(|b|-1) 
            Multi(up, m, up); 
            Plus(up, b, up); 
            Multi(down, m, down); 
        } 
        // 求分子分母的最大公约数 
        GCD(down, up, tmp); 
        HP h; 
        Divide(down, tmp, down, h); 
        Divide(up, tmp, up, h); 
        PrintHP(up); cout << "/"; 
        PrintHP(down); cout << endl; 
    } 

作者:linyunzju