Antiprism 0.23

- Main Page
- Classes
- Files

- File List
- File Members

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:

  coeffs | the coefficients of the cubic, indexed by power of x.
  sol | the real solutions (in ascending order).

Returns:
  The number of real solutions.

double deg2rad | ( | double | ang = `1.0` | ) | `[inline]`

Convert degrees to radians.

Parameters:

  ang | angle 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:

  f | the floating point number.
  num | to return the numerator of the rational number.
  denom | to return the denominator of the rational number.
  eps | the maximum difference between the floating point number and the rational approximation.
  max_steps | the 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:

  n | an integer.

Returns:
  the factorial of the integer.

long gcd | ( | long | m,
|  | long | n
| ) |  |

Get the Greatest Common Divisor.

Parameters:

  m | the first number.
  n | the second number.

Returns:
  the greatest common divisor.

bool is_even | ( | int | n | ) | `[inline]`

Check whether an integer is even.

Parameters:

  n | the 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:

  coeffs | the coefficients of the quartic, indexed by power of x.
  sol | the real parts of the solutions (in increasing order).
  sol_i | the 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:

  ang | angle 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:

  val | value to make safe.

Returns:
  A safe value.