Skip to content

Instantly share code, notes, and snippets.

@film42
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save film42/ab9d0076e9ac335dc5ef to your computer and use it in GitHub Desktop.
Save film42/ab9d0076e9ac335dc5ef to your computer and use it in GitHub Desktop.
RxCPP PI Stream
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;
});
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;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment