Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <kickstart/all.hpp> // Available at GitHub.
- using namespace kickstart::all;
- #include <math.h> // pow, llround
- #include <fenv.h> // fetestexcept, FE_INVALID, feclearexcept, FE_ALL_EXCEPT
- auto to_longlong( const double value )
- -> long long
- {
- const long long result = ::llround( value );
- if( ::fetestexcept( FE_INVALID ) ) {
- ::feclearexcept( FE_ALL_EXCEPT );
- KS_FAIL_( range_error, ""s << value << " exceeds the range of `long long`." );
- }
- return result;
- }
- void cpp_main( const string_view& cmd_verb, const vector<string_view>& args )
- {
- hopefully( args.size() == 1 )
- or exit_app_with_message( ""s << "Usage: " << cmd_verb << " NUMBER" );
- const int n = to_int( args[0] );
- const double sqrt_5 = sqrt( 5 );
- const double phi_plus = (1 + sqrt_5)/2;
- const double phi_minus = (1 - sqrt_5)/2;
- const double fib_n_float = (pow( phi_plus, n ) - pow( phi_minus, n))/sqrt_5;
- const long long fib_n = to_longlong( fib_n_float );
- out << fib_n << endl;
- }
- auto main( int n_cmd_parts, char* cmd_parts[] )
- -> int
- { return with_exceptions_displayed( cpp_main, n_cmd_parts, cmd_parts ); }
RAW Paste Data