Infinite Precision Arithmetic Class [Incomplete]
I'll update this as I go, but for now I just want to show off my awesome math skills with this infinite-precision arithmetic class I made.
It can only preform addition and subtraction, but soon I'll add multiplication, division and modulo operations. I invite you to test this class and see if the calculations are working as they should. I think I got them right but you never know so please report any bugs you might find.
With this class you can use numbers who's size is only limited by the amount of memory on your system and the speed of your processor. Why would anyone need such large numbers? I have no idea about others but I know that I need them to hold ridiculously large prime numbers for my implementation of the RSA algorithm which I'm going to post here later
There are a few inconsistencies in how strict I am checking for size differences, this is because I started working on this project while I was awake, now several hours later I feel like sleeping for a day or two
.
ExtendedArithmetic.h
ExtendedArithmetic.cpp
Usage/main.cpp
It can only preform addition and subtraction, but soon I'll add multiplication, division and modulo operations. I invite you to test this class and see if the calculations are working as they should. I think I got them right but you never know so please report any bugs you might find.
With this class you can use numbers who's size is only limited by the amount of memory on your system and the speed of your processor. Why would anyone need such large numbers? I have no idea about others but I know that I need them to hold ridiculously large prime numbers for my implementation of the RSA algorithm which I'm going to post here later
There are a few inconsistencies in how strict I am checking for size differences, this is because I started working on this project while I was awake, now several hours later I feel like sleeping for a day or two
.ExtendedArithmetic.h
Code:
#ifndef EXT_AR_H
#define EXT_AR_H
/*
infinite-precision arithmetic class
by SCHiM
*/
class ArbitraryArithmetic{
long MyLength;
unsigned char* Number;
public:
//constructor
ArbitraryArithmetic(long Lenght );
//debug/verification
void print();
//information
long getsize() const;
unsigned char* getnumber() const;
//operators
void operator = (unsigned __int64 n);
void operator = (const ArbitraryArithmetic &);
void operator -= (unsigned __int64 n);
void operator += (unsigned __int64 n);
};
#endif
Code:
#include "ExtendedArithmetic.h"
#include <iostream>
ArbitraryArithmetic::ArbitraryArithmetic(long Length){
MyLength = Length;
Number = new unsigned char[ Length ];
for(int i = 0; i <= MyLength; i++){
Number[i] = 0;
}
return;
}
void ArbitraryArithmetic::print(){
for( int i = MyLength; i >= 0 ; i--)
printf("%.2x", Number[i]);
printf("\n");
}
long ArbitraryArithmetic::getsize() const{
return MyLength;
}
unsigned char* ArbitraryArithmetic::getnumber() const{
return Number;
}
void ArbitraryArithmetic::operator =(unsigned __int64 n){
memcpy( (void*)Number, (void*)&n, sizeof( unsigned __int64) );
for(int i = sizeof( unsigned __int64); i <= MyLength; i++)
Number[i] = 0;
}
void ArbitraryArithmetic::operator =(const ArbitraryArithmetic &n){
//if I am myself :p
if(&n == this) return;
int i = 0;
long nLength = n.getsize();
unsigned char* ptr = n.getnumber();
long Lesser = nLength < MyLength ? nLength : MyLength;
for( i = 0 ; i <= Lesser; i++)
Number[i] = ptr[i];
if( Lesser == MyLength )
return;
for( ; i <= MyLength; i++)
Number[i] = 0;
return;
}
void ArbitraryArithmetic::operator -=(unsigned __int64 n){
unsigned char* nPtr = (unsigned char*) &n;
int Lesser = sizeof( unsigned __int64 ) < MyLength ? sizeof( unsigned __int64 ) : MyLength;
for( int i = 0; i < Lesser; i++){
if( Number[i] < nPtr[i] ){
for(int y = 1; y+i < Lesser; y++){
if( Number[i+y] != 0 ){
Number[i+y]--;
break;
} else {
Number[i+y] = 0xFF;
}
}
Number[i] = ( 0x100 - (nPtr[i] - Number[i]) );
nPtr[i] = 0;
}
Number[i] -= nPtr[i];
}
}
void ArbitraryArithmetic::operator +=(unsigned __int64 n){
unsigned short r = 0;
unsigned char* nPtr = (unsigned char*) &n;
unsigned char* rPtr = (unsigned char*) &r;
int Lesser = sizeof( unsigned __int64 ) < MyLength ? sizeof( unsigned __int64 ) : MyLength;
for(int i = 0; i < Lesser; i++){
r = Number[i] + nPtr[i];
if( r > 0xff ){
Number[i] = rPtr[0];
for(int y = 1; y+i < MyLength; i++){
if( rPtr[1] != 0){
if( y+i < Lesser )
r = Number[i+y] + nPtr[i+y] + rPtr[1];
else
r = Number[i+y] + rPtr[1];
Number[i+y] = rPtr[0];
} else {
break;
}
}
} else {
Number[i] += nPtr[i];
}
}
}
Code:
int main(){
uint64 Prime1, Prime2, N;
/*
GeneratePrimes( &Prime1, &Prime2);
N = Prime1 * Prime2;
printf("Found N [%lld]\n", N);
*/
ArbitraryArithmetic LargeNumber( 32 );
ArbitraryArithmetic SmallerNumber( 32 );
uint64 A = 0x100;
uint64 b = 0xFffffAc33Defff;
LargeNumber = A;
SmallerNumber = LargeNumber;
LargeNumber -= 0x1;
//printf("Small number is: ");
SmallerNumber.print();
//printf("Large number is: ");
LargeNumber.print();
//printf(" %d ", phi( 32231 ) );
system("pause");
return 0;
}