Created
August 18, 2011 03:05
-
-
Save aont/1153200 to your computer and use it in GitHub Desktop.
Calculation before compile using template
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
| #include <iostream> | |
| #include <vector> | |
| using std::size_t; | |
| template<size_t index> | |
| struct factorial | |
| { | |
| const static size_t value = index * factorial<index-1>::value; | |
| }; | |
| template<> | |
| struct factorial<0> | |
| { | |
| const static size_t value = 1; | |
| }; | |
| template<size_t index> | |
| struct napier | |
| { | |
| const static double value = napier<index-1>::value + 1.0 / factorial<index>::value; | |
| }; | |
| template<> | |
| struct napier<0> | |
| { | |
| const static double value = 1.0 / factorial<0>::value; | |
| }; | |
| template<size_t index> | |
| struct power | |
| { | |
| // const static double value = power<index-1>::value * 10; | |
| const static double value = power<index >> 1 >::value * power<index >> 1 >::value * power<index & 1 >::value; | |
| }; | |
| template<> | |
| struct power<1> | |
| { | |
| const static double value = 10; | |
| }; | |
| template<> | |
| struct power<0> | |
| { | |
| const static double value = 1; | |
| }; | |
| int main() | |
| { | |
| std::cerr.precision(17); | |
| std::cerr << power<5>::value << std::endl; | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment