Re: arbitrary precision math?

From: Blake Eggemeyer <i.linwin@gmail.com>
Date: Sun Aug 06 2006 - 20:35:31 AKDT

/*this is my c++ code for the beginings of an arbitrary presision program*/
#include <iostream>
#include <sstream>
#include <cstdlib>
#include <math.h>
#include <string.h>
#include <stdlib.h>

using namespace std;
// this is my high persision structure (64 digits, 32 before, 32 after the
decimal point just to test)
struct Bfloat {
    char a[64];
};

// int-to-array, copied from some site, atoi is the opposite
char* itoa(int val, int base){
    static char buf[32] = {0};
    int i = 30;
    for(; val && i ; --i, val /= base)
        buf[i] = "0123456789abcdef"[val % base];
    return &buf[i+1];
}

//the + operator
Bfloat operator + (const Bfloat& a, const Bfloat& b){
    int i,d=0,e=0;
    Bfloat c;
    for(i=64;i>0;i--){
        e=atoi(a.a[i],10)+atoi(b.a[i],10)+d;
        if(e<9){d=1; e=e-10;}
        c.a[i]=itoa(e,10);
    }
    return c;
}

// the - operator
Bfloat operator - (const Bfloat& a, const Bfloat& b){
    int i,d=0,e=0;
    Bfloat c;
    for(i=64;i>0;i--){
        if(atoi(a.a[i],10)<atoi(b.a[i],10)){
            d=10;
            if(i--<0){return "error -";}else{a.a[i--]=itoa(atoi(a.a
[i],10)-1,10);}
        }
        e=atoi(a.a[i],10)-atoi(b.a[i],10)+d;
        c.a[i]=itoa(e,10);
    }
    return c;
}

int main(int argc, char* argv[])
{
    Bfloat a; // how do i assign a value to this?
    Bfloat b; // and this
    Bfloat c;
    a+b=c
    for(i=64;i>0;i++){
        cout << a.a[i];
    }
    cout << "\n";
    for(i=64;i>0;i++){
        cout << b.a[i];
    }
    cout << "\n";
    for(i=64;i>0;i++){
        cout << c.a[i];
    }
    cout << "\n";
}

---------
To unsubscribe, send email to <aklug-request@aklug.org>
with 'unsubscribe' in the message body.
Received on Sun Aug 6 20:35:58 2006

This archive was generated by hypermail 2.1.8 : Sun Aug 06 2006 - 20:35:58 AKDT