added samples/uint.cpp samples/int.cpp samples/Makefile
changed Int::Div -- a remainder is not returning now but taking as a parameter (a pointer or reference) changed order in parameters in UInt::AddTwoInts added Int::AddInt added Int::AddTwoInts added Int::SubInt changed Int::AddOne and Int::SubOne (much faster now) fixed a bug in UInt::operator--() and Int::operator--() (there was adding instead of subtracting) git-svn-id: svn://ttmath.org/publicrep/ttmath/trunk@13 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
30
samples/Makefile
Normal file
30
samples/Makefile
Normal file
@@ -0,0 +1,30 @@
|
||||
CC = g++
|
||||
CFLAGS = -Wall -pedantic -s -O2 -I..
|
||||
|
||||
|
||||
.SUFFIXES: .cpp .o
|
||||
|
||||
.cpp.o:
|
||||
$(CC) -c $(CFLAGS) $<
|
||||
|
||||
|
||||
all: uint int
|
||||
|
||||
|
||||
uint: uint.o
|
||||
$(CC) -o uint $(CFLAGS) uint.o
|
||||
|
||||
int: int.o
|
||||
$(CC) -o int $(CFLAGS) int.o
|
||||
|
||||
|
||||
uint.o: uint.cpp
|
||||
int.o: int.cpp
|
||||
|
||||
|
||||
clean:
|
||||
rm -f *.o
|
||||
rm -f *.s
|
||||
rm -f uint
|
||||
rm -f int
|
||||
rm -f *.exe
|
90
samples/int.cpp
Normal file
90
samples/int.cpp
Normal file
@@ -0,0 +1,90 @@
|
||||
#include <ttmath/ttmathint.h>
|
||||
#include <iostream>
|
||||
|
||||
|
||||
void SimpleCalculating(const ttmath::Int<2> & a, const ttmath::Int<2> & b)
|
||||
{
|
||||
std::cout << "Simple calculating" << std::endl;
|
||||
std::cout << "a = " << a << std::endl;
|
||||
std::cout << "b = " << b << std::endl;
|
||||
std::cout << "a + b = " << a+b << std::endl;
|
||||
std::cout << "a - b = " << a-b << std::endl;
|
||||
std::cout << "a * b = " << a*b << std::endl;
|
||||
std::cout << "a / b = " << a/b << std::endl;
|
||||
}
|
||||
|
||||
|
||||
void CalculatingWithCarry(const ttmath::Int<2> & a, const ttmath::Int<2> & b)
|
||||
{
|
||||
ttmath::Int<2> atemp;
|
||||
|
||||
std::cout << "Calculating with carry" << std::endl;
|
||||
std::cout << "a = " << a << std::endl;
|
||||
std::cout << "b = " << b << std::endl;
|
||||
|
||||
atemp = a;
|
||||
if( !atemp.Add(b) )
|
||||
std::cout << "a + b = " << atemp << std::endl;
|
||||
else
|
||||
std::cout << "a + b = (carry: the result is too big) " << atemp << std::endl;
|
||||
|
||||
atemp = a;
|
||||
if( !atemp.Sub(b) )
|
||||
std::cout << "a - b = " << atemp << std::endl;
|
||||
else
|
||||
std::cout << "a - b = (carry: 'a' was smaller than 'b') " << atemp << std::endl;
|
||||
|
||||
atemp = a;
|
||||
if( !atemp.Mul(b) )
|
||||
std::cout << "a * b = " << atemp << std::endl;
|
||||
else
|
||||
std::cout << "a * b = (carry: the result is too big) " << std::endl;
|
||||
// it have no sense to print 'atemp' (it's undefined)
|
||||
|
||||
atemp = a;
|
||||
if( !atemp.Div(b) )
|
||||
std::cout << "a / b = " << atemp << std::endl;
|
||||
else
|
||||
std::cout << "a / b = (division by zero) " << std::endl;
|
||||
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
// on 32bit platforms: 'a' and 'b' have 2-words (two 32bit words)
|
||||
// in other words a,b are from <-2^63, 2^63 - 1>
|
||||
ttmath::Int<2> a,b;
|
||||
|
||||
// conversion from 'const char *'
|
||||
a = "123456";
|
||||
b = "98767878";
|
||||
|
||||
SimpleCalculating(a,b);
|
||||
|
||||
// 'a' will have the max value which can be held in this type
|
||||
a.SetMaxValue();
|
||||
|
||||
// conversion from 'int'
|
||||
b = 10;
|
||||
|
||||
CalculatingWithCarry(a,b);
|
||||
}
|
||||
|
||||
/*
|
||||
the result:
|
||||
|
||||
Simple calculating
|
||||
a = 123456
|
||||
b = 98767878
|
||||
a + b = 98891334
|
||||
a - b = -98644422
|
||||
a * b = 12193487146368
|
||||
a / b = 0
|
||||
Calculating with carry
|
||||
a = 9223372036854775807
|
||||
b = 10
|
||||
a + b = (carry: the result is too big) -9223372036854775799
|
||||
a - b = 9223372036854775797
|
||||
a * b = (carry: the result is too big)
|
||||
a / b = 922337203685477580
|
||||
*/
|
91
samples/uint.cpp
Normal file
91
samples/uint.cpp
Normal file
@@ -0,0 +1,91 @@
|
||||
#include <ttmath/ttmathuint.h>
|
||||
#include <iostream>
|
||||
|
||||
|
||||
void SimpleCalculating(const ttmath::UInt<2> & a, const ttmath::UInt<2> & b)
|
||||
{
|
||||
std::cout << "Simple calculating" << std::endl;
|
||||
std::cout << "a = " << a << std::endl;
|
||||
std::cout << "b = " << b << std::endl;
|
||||
std::cout << "a + b = " << a+b << std::endl;
|
||||
std::cout << "a - b = " << a-b << std::endl;
|
||||
std::cout << "a * b = " << a*b << std::endl;
|
||||
std::cout << "a / b = " << a/b << std::endl;
|
||||
}
|
||||
|
||||
|
||||
void CalculatingWithCarry(const ttmath::UInt<2> & a, const ttmath::UInt<2> & b)
|
||||
{
|
||||
ttmath::UInt<2> atemp;
|
||||
|
||||
std::cout << "Calculating with carry" << std::endl;
|
||||
std::cout << "a = " << a << std::endl;
|
||||
std::cout << "b = " << b << std::endl;
|
||||
|
||||
atemp = a;
|
||||
if( !atemp.Add(b) )
|
||||
std::cout << "a + b = " << atemp << std::endl;
|
||||
else
|
||||
// if there was a carry then atemp.Add(...) would have returned 1
|
||||
std::cout << "a + b = (carry: the result is too big) " << atemp << std::endl;
|
||||
|
||||
atemp = a;
|
||||
if( !atemp.Sub(b) )
|
||||
std::cout << "a - b = " << atemp << std::endl;
|
||||
else
|
||||
std::cout << "a - b = (carry: 'a' was smaller than 'b') " << atemp << std::endl;
|
||||
|
||||
atemp = a;
|
||||
if( !atemp.Mul(b) )
|
||||
std::cout << "a * b = " << atemp << std::endl;
|
||||
else
|
||||
std::cout << "a * b = (carry: the result is too big) " << std::endl;
|
||||
// it have no sense to print 'atemp' (it's undefined)
|
||||
|
||||
atemp = a;
|
||||
if( !atemp.Div(b) )
|
||||
std::cout << "a / b = " << atemp << std::endl;
|
||||
else
|
||||
std::cout << "a / b = (division by zero) " << std::endl;
|
||||
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
// on 32bit platforms: 'a' and 'b' have 2-words (two 32bit words)
|
||||
// in other words a,b are from <0, 2^64 - 1>
|
||||
ttmath::UInt<2> a,b;
|
||||
|
||||
// conversion from 'const char *'
|
||||
a = "123456";
|
||||
b = "9876";
|
||||
|
||||
SimpleCalculating(a,b);
|
||||
|
||||
// 'a' will have the max value which can be held in this type
|
||||
a.SetMaxValue();
|
||||
|
||||
// conversion from 'int'
|
||||
b = 5;
|
||||
|
||||
CalculatingWithCarry(a,b);
|
||||
}
|
||||
|
||||
/*
|
||||
the result:
|
||||
|
||||
Simple calculating
|
||||
a = 123456
|
||||
b = 9876
|
||||
a + b = 133332
|
||||
a - b = 113580
|
||||
a * b = 1219251456
|
||||
a / b = 12
|
||||
Calculating with carry
|
||||
a = 18446744073709551615
|
||||
b = 5
|
||||
a + b = (carry: the result is too big) 4
|
||||
a - b = 18446744073709551610
|
||||
a * b = (carry: the result is too big)
|
||||
a / b = 3689348814741910323
|
||||
*/
|
Reference in New Issue
Block a user