Antiprism 0.23
Functions

base/math_utils.h File Reference

utility routines for text operations, I/O conversions, etc More...

#include <algorithm>
#include <math.h>
#include "const.h"
Include dependency graph for math_utils.h:
This graph shows which files directly or indirectly include this file:

Functions

long gcd (long m, long n)
 Get the Greatest Common Divisor.
int factorial (int n)
 Get a factorial.
void double2rational (double f, long &num, long &denom, double eps=0.00001, int max_steps=100)
 Convert a floating point number to a close rational number.
double deg2rad (double ang=1.0)
 Convert degrees to radians.
double rad2deg (double ang=1.0)
 Convert radians to degrees.
bool is_even (int n)
 Check whether an integer is even.
int quartic (double coeffs[5], double sol[4], double sol_i[4]=0)
 Solve a quartic equation.
int cubic (double coeffs[4], double sol[3])
 Solve a cubic equation.
double safe_for_trig (double val)
 Make a value safe to use as an argument with acos and asin.

Detailed Description

utility routines for text operations, I/O conversions, etc


Function Documentation

int cubic ( double  coeffs[4],
double  sol[3] 
)

Solve a cubic equation.

Parameters:
coeffsthe coefficients of the cubic, indexed by power of x.
solthe real solutions (in ascending order).
Returns:
The number of real solutions.
double deg2rad ( double  ang = 1.0) [inline]

Convert degrees to radians.

Parameters:
angangle in degres, to convert.
Returns:
The angle in radians.
void double2rational ( double  f,
long &  num,
long &  denom,
double  eps = 0.00001,
int  max_steps = 100 
)

Convert a floating point number to a close rational number.

Can easily fail if MAX_LONG*eps^2 > 1 .

Parameters:
fthe floating point number.
numto return the numerator of the rational number.
denomto return the denominator of the rational number.
epsthe maximum difference between the floating point number and the rational approximation.
max_stepsthe maximum number of iterations, in case the process does not converge (default 100 should exceed any reasonable number of steps).
int factorial ( int  n)

Get a factorial.

Parameters:
nan integer.
Returns:
the factorial of the integer.
long gcd ( long  m,
long  n 
)

Get the Greatest Common Divisor.

Parameters:
mthe first number.
nthe second number.
Returns:
the greatest common divisor.
bool is_even ( int  n) [inline]

Check whether an integer is even.

Parameters:
nthe number to check.
Returns:
true if the number is even, otherwise false.
int quartic ( double  coeffs[5],
double  sol[4],
double  sol_i[4] = 0 
)

Solve a quartic equation.

Parameters:
coeffsthe coefficients of the quartic, indexed by power of x.
solthe real parts of the solutions (in increasing order).
sol_ithe imaginary parts of the solutions (if sol_i is not zero).
Returns:
The number of real solutions.
double rad2deg ( double  ang = 1.0) [inline]

Convert radians to degrees.

Parameters:
angangle in radians, to convert.
Returns:
The angle in degrees.
double safe_for_trig ( double  val) [inline]

Make a value safe to use as an argument with acos and asin.

Map the value to the nearest value in the range -1.0<=val<=1.0 to ensure that it is safe to use with acos() and asin().

Parameters:
valvalue to make safe.
Returns:
A safe value.