Last active
August 29, 2015 14:04
-
-
Save film42/ab9d0076e9ac335dc5ef to your computer and use it in GitHub Desktop.
RxCPP PI Stream
This file contains 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
auto pi_series = []( long limit ){ | |
return rxcpp::observable<>::range(1, limit) | |
.map([](int k) { | |
return ( k % 2 == 0 ? -4 : 4 ) / ( long double )( ( 2 * k ) - 1 ); | |
}) | |
.sum() | |
.as_dynamic(); | |
}; | |
pi_series( 1000000 ).subscribe([]( long double total ) { | |
std::cout << "Pi: " << total << std::endl; | |
}); |
This file contains 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
auto pi = [](int k) { | |
return ( k % 2 == 0 ? -4 : 4 ) / ( long double )( ( 2 * k ) - 1 ); | |
}; | |
long double total = 0; | |
for( int i = 1; i <= 1000000; ++i ) { | |
long double res = pi( i ); | |
total += res; | |
} | |
std::cout << "Pi: " << total << std::endl; |
This file contains 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
Pi: 3.14159 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment