Last active
October 22, 2016 20:25
-
-
Save Luiz-Monad/678e23deafb33156d93f55423243b02c to your computer and use it in GitHub Desktop.
peano_complex.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Lets create an arithmetic by using templates, why not? | |
//We're not allowed to use builtin compiler math operations | |
//Yes, this uses sifinae | |
struct Z { | |
enum { ToInt = 0 }; | |
}; | |
template<typename T> | |
struct S { | |
enum { ToInt = sizeof(T) }; | |
T t; | |
char c; | |
}; | |
//Sum definition | |
//1. P(a,0) = a | |
//2. P(a,S(b)) = S(P(a,b)) | |
template<typename A, typename B> | |
struct Sum { | |
}; | |
template<> | |
template<typename A> | |
struct Sum<A, Z> { | |
typedef A value; | |
}; | |
template<> | |
template<typename A, typename B> | |
struct Sum<A, S<B> > { | |
typedef S<typename Sum<A, B>::value> value; | |
}; | |
//Multiplication definition | |
//1. M(a,0) = 0 | |
//2. M(a,S(b)) = P(a,M(a,b)) | |
template<typename A, typename B> | |
struct Mult { | |
}; | |
template<> | |
template<typename A> | |
struct Mult<A, Z> { | |
typedef Z value; | |
}; | |
template<> | |
template<typename A, typename B> | |
struct Mult<A, S<B> > { | |
typedef typename Sum<A, typename Mult<A, B>::value>::value value; | |
}; | |
typedef Z D0; | |
typedef S < D0 > D1; | |
typedef S < D1 > D2; | |
typedef S < D2 > D3; | |
typedef S < D3 > D4; | |
typedef S < D4 > D5; | |
typedef S < D5 > D6; | |
typedef S < D6 > D7; | |
typedef S < D7 > D8; | |
typedef S < D8 > D9; | |
typedef S < D9 > DA; | |
template<typename base, typename power, typename leastsignf, typename... digits> | |
struct AlKhwarizmi { | |
typedef typename Sum< | |
typename Mult<power, leastsignf>::value, | |
typename AlKhwarizmi< | |
base, | |
typename Mult<base, power>::value, | |
digits... | |
>::value | |
>::value value; | |
}; | |
template<> | |
template<typename base, typename power, typename leastsignf> | |
struct AlKhwarizmi<base, power, leastsignf> { | |
typedef typename Mult<power, leastsignf>::value value; | |
}; | |
template<typename... digits> | |
struct Decimal { | |
typedef typename AlKhwarizmi<DA, D1, digits...>::value value; | |
}; | |
//(120 + 301) * 2 = 842 | |
typedef Decimal<D0, D2, D1>::value A; | |
typedef Decimal<D1, D0, D3>::value B; | |
typedef Sum<A, B>::value C; | |
typedef Mult<C, D2>::value D; | |
int k = D::ToInt; | |
#include <iostream> | |
int main(int argc, char** argv) { | |
std::cout << k; | |
return k; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment