Currency conversions
This commit is contained in:
46
3rd-party/ttmath-0.9.3/samples/Makefile
vendored
Normal file
46
3rd-party/ttmath-0.9.3/samples/Makefile
vendored
Normal file
@@ -0,0 +1,46 @@
|
||||
CC = g++
|
||||
CFLAGS = -Wall -pedantic -s -O2 -I.. -DTTMATH_DONT_USE_WCHAR
|
||||
|
||||
|
||||
.SUFFIXES: .cpp .o
|
||||
|
||||
.cpp.o:
|
||||
$(CC) -c $(CFLAGS) $<
|
||||
|
||||
|
||||
all: uint int big big2 parser
|
||||
|
||||
|
||||
uint: uint.o
|
||||
$(CC) -o uint $(CFLAGS) uint.o
|
||||
|
||||
int: int.o
|
||||
$(CC) -o int $(CFLAGS) int.o
|
||||
|
||||
big: big.o
|
||||
$(CC) -o big $(CFLAGS) big.o
|
||||
|
||||
big2: big2.o
|
||||
$(CC) -o big2 $(CFLAGS) big2.o
|
||||
|
||||
parser: parser.o
|
||||
$(CC) -o parser $(CFLAGS) parser.o
|
||||
|
||||
|
||||
uint.o: uint.cpp
|
||||
int.o: int.cpp
|
||||
big.o: big.cpp
|
||||
big2.o: big2.cpp
|
||||
parser.o: parser.cpp
|
||||
|
||||
|
||||
clean:
|
||||
rm -f *.o
|
||||
rm -f *.s
|
||||
rm -f uint
|
||||
rm -f int
|
||||
rm -f big
|
||||
rm -f big2
|
||||
rm -f parser
|
||||
# on MS Windows can automatically be added suffixes .exe to the names of output programs
|
||||
rm -f *.exe
|
100
3rd-party/ttmath-0.9.3/samples/big.cpp
vendored
Normal file
100
3rd-party/ttmath-0.9.3/samples/big.cpp
vendored
Normal file
@@ -0,0 +1,100 @@
|
||||
#include <ttmath/ttmath.h>
|
||||
#include <iostream>
|
||||
|
||||
// for convenience we're defining MyBig type
|
||||
// this type has 2 words for its mantissa and 1 word for its exponent
|
||||
// (on a 32bit platform one word means a word of 32 bits,
|
||||
// and on a 64bit platform one word means a word of 64 bits)
|
||||
|
||||
// Big<exponent, mantissa>
|
||||
typedef ttmath::Big<1,2> MyBig;
|
||||
|
||||
|
||||
void SimpleCalculating(const MyBig & a, const MyBig & 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 MyBig & a, const MyBig & b)
|
||||
{
|
||||
MyBig atemp;
|
||||
|
||||
std::cout << "Calculating with a 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)" << std::endl;
|
||||
// it have no sense to print 'atemp' (it's undefined)
|
||||
|
||||
atemp = a;
|
||||
if( !atemp.Sub(b) )
|
||||
std::cout << "a - b = " << atemp << std::endl;
|
||||
else
|
||||
std::cout << "a - b = (carry)" << std::endl;
|
||||
|
||||
atemp = a;
|
||||
if( !atemp.Mul(b) )
|
||||
std::cout << "a * b = " << atemp << std::endl;
|
||||
else
|
||||
std::cout << "a * b = (carry)" << std::endl;
|
||||
|
||||
|
||||
atemp = a;
|
||||
if( !atemp.Div(b) )
|
||||
std::cout << "a / b = " << atemp << std::endl;
|
||||
else
|
||||
std::cout << "a / b = (carry or division by zero) " << std::endl;
|
||||
|
||||
}
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
MyBig a,b;
|
||||
|
||||
// conversion from 'const char *'
|
||||
a = "123456.543456";
|
||||
b = "98767878.124322";
|
||||
|
||||
SimpleCalculating(a,b);
|
||||
|
||||
// 'a' will have the max value which can be held in this type
|
||||
a.SetMax();
|
||||
|
||||
// conversion from double
|
||||
b = 456.32;
|
||||
|
||||
// Look at the value 'a' and the product from a+b and a-b
|
||||
// Don't worry this is the nature of floating point numbers
|
||||
CalculatingWithCarry(a,b);
|
||||
}
|
||||
|
||||
/*
|
||||
the result (on 32 bit platform):
|
||||
|
||||
Simple calculating
|
||||
a = 123456.543456
|
||||
b = 98767878.124322
|
||||
a + b = 98891334.667778
|
||||
a - b = -98644421.580866
|
||||
a * b = 12193540837712.27076
|
||||
a / b = 0.00124996654580957646
|
||||
Calculating with a carry
|
||||
a = 1.6248012560666408782e+646457012
|
||||
b = 456.319999999999993
|
||||
a + b = 1.6248012560666408782e+646457012
|
||||
a - b = 1.6248012560666408782e+646457012
|
||||
a * b = (carry)
|
||||
a / b = 3.560661939136222174e+646457009
|
||||
*/
|
113
3rd-party/ttmath-0.9.3/samples/big2.cpp
vendored
Normal file
113
3rd-party/ttmath-0.9.3/samples/big2.cpp
vendored
Normal file
@@ -0,0 +1,113 @@
|
||||
#include <ttmath/ttmath.h>
|
||||
#include <iostream>
|
||||
|
||||
|
||||
// this is a similar example to big.cpp
|
||||
// but now we're using TTMATH_BITS() macro
|
||||
// this macro returns how many words we need to store
|
||||
// the given number of bits
|
||||
|
||||
// TTMATH_BITS(64)
|
||||
// on a 32bit platform the macro returns 2 (2*32=64)
|
||||
// on a 64bit platform the macro returns 1
|
||||
|
||||
// TTMATH_BITS(128)
|
||||
// on a 32bit platform the macro returns 4 (4*32=128)
|
||||
// on a 64bit platform the macro returns 2 (2*64=128)
|
||||
|
||||
// Big<exponent, mantissa>
|
||||
typedef ttmath::Big<TTMATH_BITS(64), TTMATH_BITS(128)> MyBig;
|
||||
|
||||
// consequently on a 32bit platform we define: Big<2, 4>
|
||||
// and on a 64bit platform: Big<1, 2>
|
||||
// and the calculations will be the same on both platforms
|
||||
|
||||
|
||||
void SimpleCalculating(const MyBig & a, const MyBig & 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 MyBig & a, const MyBig & b)
|
||||
{
|
||||
MyBig atemp;
|
||||
|
||||
std::cout << "Calculating with a 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)" << std::endl;
|
||||
// it have no sense to print 'atemp' (it's undefined)
|
||||
|
||||
atemp = a;
|
||||
if( !atemp.Sub(b) )
|
||||
std::cout << "a - b = " << atemp << std::endl;
|
||||
else
|
||||
std::cout << "a - b = (carry)" << std::endl;
|
||||
|
||||
atemp = a;
|
||||
if( !atemp.Mul(b) )
|
||||
std::cout << "a * b = " << atemp << std::endl;
|
||||
else
|
||||
std::cout << "a * b = (carry)" << std::endl;
|
||||
|
||||
|
||||
atemp = a;
|
||||
if( !atemp.Div(b) )
|
||||
std::cout << "a / b = " << atemp << std::endl;
|
||||
else
|
||||
std::cout << "a / b = (carry or division by zero) " << std::endl;
|
||||
|
||||
}
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
MyBig a,b;
|
||||
|
||||
// conversion from 'const char *'
|
||||
a = "123456.543456";
|
||||
b = "98767878.124322";
|
||||
|
||||
SimpleCalculating(a,b);
|
||||
|
||||
// 'a' will have the max value which can be held in this type
|
||||
a.SetMax();
|
||||
|
||||
// conversion from double
|
||||
b = 456.32;
|
||||
|
||||
// Look at the value 'a' and the product from a+b and a-b
|
||||
// Don't worry this is the nature of floating point numbers
|
||||
CalculatingWithCarry(a,b);
|
||||
}
|
||||
|
||||
/*
|
||||
the result (the same on a 32 or 64bit platform):
|
||||
|
||||
Simple calculating
|
||||
a = 123456.543456
|
||||
b = 98767878.124322
|
||||
a + b = 98891334.667778
|
||||
a - b = -98644421.580866
|
||||
a * b = 12193540837712.270763536832
|
||||
a / b = 0.0012499665458095764605964485261668609133
|
||||
Calculating with a carry
|
||||
a = 2.34953455457111777368832820909595050034e+2776511644261678604
|
||||
b = 456.3199999999999931787897367030382156
|
||||
a + b = 2.34953455457111777368832820909595050034e+2776511644261678604
|
||||
a - b = 2.34953455457111777368832820909595050034e+2776511644261678604
|
||||
a * b = (carry)
|
||||
a / b = 5.1488748127873374141170361292780486452e+2776511644261678601
|
||||
*/
|
92
3rd-party/ttmath-0.9.3/samples/int.cpp
vendored
Normal file
92
3rd-party/ttmath-0.9.3/samples/int.cpp
vendored
Normal file
@@ -0,0 +1,92 @@
|
||||
#include <ttmath/ttmath.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 a 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) " << atemp << std::endl;
|
||||
|
||||
atemp = a;
|
||||
if( !atemp.Sub(b) )
|
||||
std::cout << "a - b = " << atemp << std::endl;
|
||||
else
|
||||
std::cout << "a - b = (carry) " << 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)
|
||||
// it means a,b are from <-2^63, 2^63 - 1>
|
||||
ttmath::Int<2> a,b;
|
||||
|
||||
// conversion from int
|
||||
a = 123456;
|
||||
|
||||
// conversion from 'const char *'
|
||||
b = "98767878";
|
||||
|
||||
SimpleCalculating(a,b);
|
||||
|
||||
// 'a' will have the max value which can be held in this type
|
||||
a.SetMax();
|
||||
|
||||
// conversion from 'int'
|
||||
b = 10;
|
||||
|
||||
CalculatingWithCarry(a,b);
|
||||
}
|
||||
|
||||
/*
|
||||
the result (on 32 bit platform):
|
||||
|
||||
Simple calculating
|
||||
a = 123456
|
||||
b = 98767878
|
||||
a + b = 98891334
|
||||
a - b = -98644422
|
||||
a * b = 12193487146368
|
||||
a / b = 0
|
||||
Calculating with a carry
|
||||
a = 9223372036854775807
|
||||
b = 10
|
||||
a + b = (carry) -9223372036854775799
|
||||
a - b = 9223372036854775797
|
||||
a * b = (carry) the result is too big)
|
||||
a / b = 922337203685477580
|
||||
*/
|
39
3rd-party/ttmath-0.9.3/samples/parser.cpp
vendored
Normal file
39
3rd-party/ttmath-0.9.3/samples/parser.cpp
vendored
Normal file
@@ -0,0 +1,39 @@
|
||||
#include <ttmath/ttmath.h>
|
||||
#include <iostream>
|
||||
|
||||
|
||||
// for convenience we're defining MyBig type
|
||||
// this type has 2 words for its mantissa and 1 word for its exponent
|
||||
// (on a 32bit platform one word means a word of 32 bits,
|
||||
// and on a 64bit platform one word means a word of 64 bits)
|
||||
typedef ttmath::Big<1,2> MyBig;
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
ttmath::Parser<MyBig> parser;
|
||||
|
||||
// the sine function takes its parameter as being in radians,
|
||||
// the product from the arcus tangent will be in radians as well
|
||||
const char equation[] = " (34 + 24) * 123 - 34.32 ^ 6 * sin(2.56) - atan(10)";
|
||||
|
||||
ttmath::ErrorCode err = parser.Parse(equation);
|
||||
|
||||
if( err == ttmath::err_ok )
|
||||
std::cout << parser.stack[0].value << std::endl;
|
||||
else
|
||||
std::cout << "Error: "
|
||||
<< static_cast<int>(err)
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
/*
|
||||
the result (on 32 bit platform):
|
||||
-897705014.525731067
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
the result (on 64 bit platform):
|
||||
-897705014.5257310676097719585259773124
|
||||
*/
|
93
3rd-party/ttmath-0.9.3/samples/uint.cpp
vendored
Normal file
93
3rd-party/ttmath-0.9.3/samples/uint.cpp
vendored
Normal file
@@ -0,0 +1,93 @@
|
||||
#include <ttmath/ttmath.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 a 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)
|
||||
// it means a,b are from <0, 2^64 - 1>
|
||||
ttmath::UInt<2> a,b;
|
||||
|
||||
// conversion from 'const char *'
|
||||
a = "123456";
|
||||
|
||||
// conversion from int
|
||||
b = 9876;
|
||||
|
||||
SimpleCalculating(a,b);
|
||||
|
||||
// 'a' will have the max value which can be held in this type
|
||||
a.SetMax();
|
||||
|
||||
// conversion from 'int'
|
||||
b = 5;
|
||||
|
||||
CalculatingWithCarry(a,b);
|
||||
}
|
||||
|
||||
/*
|
||||
the result (on 32 bit platform):
|
||||
|
||||
Simple calculating
|
||||
a = 123456
|
||||
b = 9876
|
||||
a + b = 133332
|
||||
a - b = 113580
|
||||
a * b = 1219251456
|
||||
a / b = 12
|
||||
Calculating with a 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