Results 1 to 25 of 35
Thread: (22/7)
Hybrid View
-
8th May 2006 22:51 #1
Join Date: May:2005
Location: Varna
Posts: 290
-
8th May 2006 22:54 #2
-
8th May 2006 23:03 #3
-
8th May 2006 23:21 #4
-
8th May 2006 23:22 #5
A :
http://www.xtremesystems.com/pi/
64? Win?
-
8th May 2006 23:33 #6
Join Date: May:2005
Location: Varna
Posts: 290
-
8th May 2006 23:40 #7


Code:#include <stdio.h> #include <iostream> #include <iomanip> #include <string.h> #include <time.h> using namespace std; // the radix used in internal calculations (must be <= 10000 and a power of 10) #define RADIX 10000 // the precision (amount of fractional part digits) #define REALPREC 6 // the maximum amount of digits in a Real number (integer + fractional part) #define MAX 18 const int ipow10[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000}; class Real { private: int d; int a[MAX]; void stor(char *); void move_dec_point(int); // positive move = division. move_dec_point(1) divides by 10 public: Real(); Real(unsigned); Real(char *); Real(double); Real(int size, int *numbers); Real(const Real&); int length() const; void set_length(int new_len) // sets the length of the number (integer+frac part) { d = new_len; } void set_digit(int i,int value) // sets the i-th digit to the given value { if (i>=0 && i < d) a[i] = value; } void clear(); // zeroes the number void print() const; // prints the number void print_int() const; // prints the integral part void round(); // rounds to the nearest integer void trunc(); // truncates void increment(); // increase by 1 void norm(); // normalize length void shift(int); // positive = multiplication. shift(1) multiplies by RADIX void muldig(unsigned); // multiplies by a number less than RADIX void div2(); // divides by 2 void incbyeps(); // increases by epsilon //operators friend Real operator+(const Real&, const Real&); friend Real operator-(const Real&, const Real&); friend Real operator+=(Real &, const Real&); friend Real operator-=(Real &, const Real&); friend Real operator*(const Real&, const Real&); friend Real operator*=(Real &, const Real&); friend Real operator /(const Real &, const Real &); friend Real operator/=(Real &, const Real &); // comparison operators friend bool operator<(const Real&, const Real&); friend bool operator==(const Real&, const Real&); friend bool operator<=(const Real&, const Real&); friend bool operator>=(const Real&, const Real&); friend bool operator>(const Real&, const Real&); friend bool operator!=(const Real&, const Real&); }; // returns the integral part of the base 10 logarithm of x int ilog10(unsigned x) { int r = -1; while (x) { r++; x/=10; } return r; } // private functions: void Real::stor(char *s) { int r, i, j; d = REALPREC-1; r = ilog10(RADIX); for (i=strlen(s)-1;i>=0;i--) { if ((strlen(s)-i-1)%r == 0) { ++d; j = 1; a[d]=0; } a[d] += j * ((unsigned) s[i]-'0'); j *= 10; } ++d; } void Real::move_dec_point(int amount) { int r, i, x, y; if (!amount) return; r = ilog10(RADIX); x = amount / r; y = amount % r; if (y<0) { x--; y+=r; } int b[MAX] = {0}; for (i=0;i<MAX;i++) { if (i-x-1>=0) b[i-x-1] += (a[i] % ipow10[y])*ipow10[r-y]; if (i-x>=0) b[i-x] += a[i] / ipow10[y]; } memcpy(a, b, sizeof(b)); d -= x; if (a[d-1]==0) d--; if (d<REALPREC) d = REALPREC; } // public functions: Real::Real() { int i; d = REALPREC; for (i=0;i<MAX;i++) a[i] = 0; } Real::Real(unsigned x) { clear(); d = REALPREC; do { a[d++] = x % RADIX; x /= RADIX; }while (x); } void Real::clear() { for (int i = 0;i < MAX; i++) a[i] = 0; } Real::Real(char *s) { int i; clear(); for (i=0;i<strlen(s);i++) if (s[i]<'0' || s[i] > '9') { cout << "Invalid real number: " << s << endl; return; } stor(s); } Real::Real(double x) { int dec/*, sign*/; int i; char s[100]; char q[20]; //o = _fcvt(x, ilog10(RADIX)*REALPREC, &dec, &sign); sprintf(q, "%%.%dlf", ilog10(RADIX)*REALPREC); sprintf(s, q, x); dec = 0; while (s[dec]!='.') dec++; for (i=dec;i<strlen(s);i++) s[i] = s[i+1]; stor(s); move_dec_point(strlen(s)-dec); } Real::Real(int size, int *numbers) { d = size; memcpy(a, numbers, size*sizeof(int)); } Real::Real(const Real&x) { int i; d=x.d; for (i=0;i<d;i++) a[i]=x.a[i]; } int Real::length() const { return d; } void Real::print_int() const { int i, j, p, f; char s[12] = {0}; for (i=d-1;i>=REALPREC;i--) { f = a[i]; p = 10; for (j=0;j<ilog10(RADIX);j++) { s[--p] = '0' + f % 10; f/=10; } if (i==d-1) {while(s[p]=='0') p++;} cout << s+p; } } void Real::print() const { int i, j, p, f; char s[12] = {0}; print_int(); cout << "." ; for (i=REALPREC-1;i>=0;i--) { f = a[i]; p = 10; for (j=0;j<ilog10(RADIX);j++) { s[--p] = '0' + f % 10; f/=10; } cout << s+p; } } void Real::trunc() { int i; for (i=0;i<REALPREC;i++) a[i] = 0; } void Real::round() { if (a[REALPREC-1] >= RADIX / 2) { increment(); } trunc(); } void Real::increment() { int i = REALPREC, carry = 1; while (carry) { a[i]++; carry = a[i]/RADIX; a[i]%=RADIX; i++; if (i>=d && carry!=0) { d++; a[i] = 0; } } } void Real::shift(int amount) { int b[MAX] = {0}; int i; for (i=0;i<d;i++) if (i+amount >= 0 && i+amount < MAX) b[i+amount] = a[i]; memcpy(a, b, MAX*sizeof(int)); d+=amount; if (d<REALPREC) d=REALPREC; } void Real::muldig(unsigned dig) { int i, carry; for (i=0,carry=0; i < d; i++) { a[i] = a[i] * dig + carry; carry = a[i] / RADIX; a[i] %= RADIX; } if (carry) { a[d++] = carry; } } void Real::norm() { if (d < REALPREC) d = REALPREC; while (d>REALPREC+1 && a[d-1]==0) d--; } void Real::div2() { int i, carry; for (carry=0,i=d-1;i>=0;i--) { a[i] += carry; carry = RADIX*(a[i]%2); a[i]/=2; } if (a[d-1]==0) d--; if (d<REALPREC) d = REALPREC; } void Real::incbyeps() { int i, carry = 1; for (i=0;i<d;i++) { a[i]+=carry; carry = a[i]/RADIX; a[i]%=RADIX; } if (carry) a[d++] = carry; } Real operator+(const Real& a, const Real& b) { int m[MAX], d, i, fa, fb, fc; d = (a.d>b.d)?a.d:b.d; for (fc=0,i=0;i<=d;i++) { fa = i<a.d?a.a[i]:0; fb = i<b.d?b.a[i]:0; m[i] = fa+fb+fc; fc = m[i] / RADIX; m[i] %= RADIX; } if (m[d]) d++; Real r(d, m); return r; } Real operator-(const Real & a, const Real & b) { int m[MAX], d, i, fa, fb, fc; d = (a.d>b.d)?a.d:b.d; for (fc=0,i=0;i<d;i++) { fa = i<a.d?a.a[i]:0; fb = i<b.d?b.a[i]:0; m[i] = fa - fb + fc; fc = (m[i]-RADIX+1) / RADIX; m[i] -= RADIX*fc; } if (fc!=0) { cout << "error: result of a-b is negative!\n"; Real r; return r; } if (m[d-1]==0) d--; if (d<REALPREC) d = REALPREC; Real r(d, m); return r; } Real operator+=(Real & a, const Real& b) { //cout << a.d << endl; int i, fa, fb, fc; if (b.d>a.d) a.d = b.d; for (fc=0,i=0;i<=a.d;i++) { fa = i < a.d ? a.a[i] : 0; fb = i < b.d ? b.a[i] : 0; a.a[i] = fa + fb + fc; fc = a.a[i] / RADIX; a.a[i] %= RADIX; } if (a.a[a.d]!=0) a.d++; return a; } Real operator-=(Real & a, const Real& b) { int i, fa, fb, fc; for (fc=0,i=0;i<a.d;i++) { fa = i < a.d?a.a[i] : 0; fb = i < b.d?b.a[i] : 0; a.a[i] = fa - fb + fc; fc = (a.a[i]-RADIX+1) / RADIX; a.a[i] -= RADIX*fc; } if (fc!=0) { cout << "error: result of a-b is negative!\n"; Real r; return r; } if (a.a[a.d-1]==0) a.d--; if (a.d<REALPREC) a.d = REALPREC; return a; } Real operator*(const Real& a, const Real& b) { int i; Real d, c; d = 0u; for (i=0;i<b.d;i++) if (b.a[i]!=0){ c = a; c.muldig((unsigned)b.a[i]); c.shift(i); d += c; } d.shift(-REALPREC); return d; } Real operator*=(Real &a, const Real &b) { a = a * b; return a; } Real operator/(const Real &a, const Real &b) { Real c = a; Real d; int i, l, r, m; //c.shift(+REALPREC); d.set_length(c.length() - b.length() + REALPREC+1); for (i=d.length()-1;i>=0;i--) { l = 0; r = RADIX - 1; while (r-l>0) { m = (r+l+1)/2; d.set_digit(i , m); // e = d*b; if ((d*b)<=c) l = m; else r = m-1; } d.set_digit(i, l); //c -= d*b; } d.norm(); return d; } Real operator/=(Real &a, const Real &b) { a = a/b; return a; } Real Real_sqrt(const Real &x) { Real l, r, m, z, ll; l = 0u; if (x < 1.0) r = 1.0; else r = x; ll = l; while (ll<r) { m = l + r; m.div2(); z = m*m; if (z>x) r = m; else l = m; ll = l; ll.incbyeps(); } return l; } bool operator<(const Real & a, const Real & b) { int i; if (a.d > b.d) return false; if (a.d < b.d) return true; for (i=a.d-1;i>=0;i--) { if (a.a[i]<b.a[i]) return true; if (a.a[i]>b.a[i]) return false; } return false; } bool operator==(const Real & a, const Real & b) { int i; if (a.d!=b.d) return false; for (i=0;i<a.d;i++) if (a.a[i] != b.a[i]) return false; return true; } bool operator<=(const Real & a, const Real & b) { return ((a<b)||(a==b)); } bool operator>(const Real & a, const Real & b) { return (!(a<=b)); } bool operator>=(const Real & a, const Real & b) { return (!(a<b)); } bool operator!=(const Real & a, const Real & b) { return (!(a==b)); } Real calculate_e(double x) { Real s=1.0; Real term=1.0; unsigned i; for (i=1;i<150&&term!=0u;i++) { term *= x; term /= i; s += term; } return s; } #define MAX_PI_ITERATIONS 1048576 Real calculate_pi() { unsigned i; Real a, b, c=1.0, d; for (i=1;i<MAX_PI_ITERATIONS;i++) { if (i%8192==0) { cout << setw(4) << i / 1024 << "K iterations completed. b="; b.print(); cout << endl; } d = i; d *= d; d *= d; //get i^4 b = c/d; if (b == 0u) { cout << "Calculated Pi in " << i << " iterations." << endl; break; } a += b; } if (b!=0u) { cout << "Pi calculation cancelled due to reaching the maximum iteration count\n"; } a *= 90u; cout << "Sum = Pi^4 = "; a.print(); cout << endl; return Real_sqrt(Real_sqrt(a)); } int main(void) { /* double x; cout << "x= "; cin >> x; Real a = calculate_e(x); cout << "e^x = ";*/ time_t clk, clk0; clk = clock(); Real a = calculate_pi(); clk0 = clock(); cout << "Pi calculation done in " << clk0-clk << " milliseconds.\n"; cout << "Pi = "; a.print(); cout << endl; }, . .
"640K ught to be enough for anybody" - Bill Gates, 1981
::Machine specs::Fract::AGG::::Baileys::blog::YouTube channel




Reply With Quote

Lenovo ThinkPad 15 IdeaPad 15
5th May 2023, 22:16 in