cppx-core
floating-point-operations.hpp
Go to the documentation of this file.
1 #pragma once // Source encoding: UTF-8 with BOM (π is a lowercase Greek "pi").
2 
8 #include <cppx-core/language/calc/integer-operations.hpp> // cppx::is_odd
10 
11 namespace cppx
12 {
14  namespace impl
15  {
16  inline auto intpow( const double base, const int exponent )
17  -> double
18  {
19  double result = 1;
20  double weight = base;
21  for( int n = exponent; n != 0; weight *= weight )
22  {
23  if( is_odd( n ) )
24  {
25  result *= weight;
26  --n;
27  }
28  else
29  {
30  n /= 2;
31  }
32  }
33  return result;
34  }
35  } // namespace impl
37 
45  template< class Number = double >
46  inline auto intpow( const Number base, const int exponent )
47  -> Number
48  {
49  return (0?0
50  : exponent == 0? 1.0
51  : exponent < 0? 1.0/impl::intpow( base, -exponent )
52  : impl::intpow( base, exponent )
53  );
54  }
55 
57  inline auto squared( const double x )
58  -> double
59  { return x*x; }
60 
62  inline auto cubed( const double x )
63  -> double
64  { return x*x*x; }
65 
66  namespace calc
67  {
69  } // namespace math
70 } // namespace cppx
auto intpow(const Number base, const int exponent) -> Number
Efficient x to the n'th power, when n is an integer.
auto squared(const double x) -> double
The square (second power) of the argument.
CPPX_USE_CPPX(intpow, squared, cubed)
Common integer operations: is_even, is_odd, intdiv, intmod, intsquare, intcube, intmin and intmax.
auto cubed(const double x) -> double
The cube (third power) of the argument.
Macros for generating more concise and clear using statements, primarily $use_cppx and $use_std,...
auto is_odd(const Int x) -> Truth