Math Functions

Math Functions

What would computers and writing programs (particularly 3D game engines) be without the necessary math functions? What happens when you need to use Sin, Cos or Tan? Well, again, the guys that created the C compiler definitions come to your rescue, since these functions are provided for you. The prototypes for these functions are inside of “math.h”
What I’ve done here is listed the more commonly used ones, along with their prototypes. This gives you how to call the functions. Notice that most use the “double” data type. If you call these with floats, then most compilers will give you a warning, since the functions aren’t expecting to get a float, it’s expecting a double float. This means that you need to cast any floats you use to doubles, and cast the returning doubles back to a float. The reason it uses doubles is that they are the highest resolution floating point number that most compilers can use. If you are writing C code to test laser beams, or some kind of scientific application often you need the highest precision you can get, so by default C uses that as it’s standard. There are ways to tell the compiler to NOT generate warnings when you call math functions with double prototypes with a float. However, it’s often different for every compiler, so I’m not going to go into how that works right now. Think of that as a lesson for you to find out:).

Errors

All math.h functions handle errors similarly.
In the case that the argument passed to the function exceeds the range of that function, then the variable errno is set to EDOM. The value that the function returns is implementation specific.
In the case that the value being returned is too large to be represented in a double, then the function returns the #define HUGE_VAL, and sets the variable errno to ERANGE to represent an overflow. If the value is too small to be represented in a double, then the function returns zero. In this case whether or not errno is set to ERANGE is implementation specific

acos
double acos(double x);

Returns the arc cosine of x in radians.
The value x must be within the range of -1 to +1 (inclusive). The returned value is in the range of 0 to pi (inclusive).

asin
double asin(double x);

Returns the arc sine of x in radians.
The value of x must be within the range of -1 to +1 (inclusive). The returned value is in the range of -p/2 to +p/2 (inclusive).

atan
double atan(double x);

Returns the arc tangent of x in radians.
The value of x has no range. The returned value is in the range of -p/2 to +p/2 (inclusive).

atan2
double atan2(doubly y, double x);

Returns the arc tangent in radians of y/x based on the signs of both values to determine the correct quadrant.
Range:
Both y and x cannot be zero. The returned value is in the range of -p/2 to +p/2 (inclusive).

cos
double cos(double x);

Returns the cosine of a radian angle x.
The value of x has no range. The returned value is in the range of -1 to +1 (inclusive).

cosh
double cosh(double x);

Returns the hyperbolic cosine of x.

sin
double sin(double x);

Returns the sine of a radian angle x.
The value of x has no range. The returned value is in the range of -1 to +1 (inclusive).

sinh
double sinh(double x);

Returns the hyperbolic sine of x.

tan
double tan(double x);

Returns the tangent of a radian angle x.

tanh
double tanh(double x);

Returns the hyperbolic tangent of x.
The value of x has no range. The returned value is in the range of -1 to +1 (inclusive).

exp
double exp(double x);

Returns the value of e raised to the xth power.

frexp
double frexp(double x, int *exponent);

The floating-point number x is broken up into a mantissa and exponent.
The returned value is the mantissa and the integer pointed to by exponent is the exponent. The resultant value is x=mantissa * 2^exponent.
The mantissa is in the range of .5 (inclusive) to 1 (exclusive).

ldexp
double ldexp(double x, int exponent);

Returns x multiplied by 2 raised to the power of exponent.
x*2^exponent

log
double log(double x);

Returns the natural logarithm (base-e logarithm) of x.

sqrt
double sqrt(double x);

Returns the square root of x.
The argument cannot be negative. The returned value is always positive.

Other Stuff

There are a couple of other really handy math functions that C gives you, like how to remove fractions from float numbers, and so on.

ceil
double ceil(double x);

Returns the smallest integer value greater than or equal to x. Useful for when you have a float, but you need to round it up to a whole integer.

fabs
double fabs(double x);

Returns the absolute value of x (a negative value becomes positive, positive value is unchanged).

floor
double floor(double x);

Returns the largest integer value less than or equal to x. Like ceil, but used when you want to round down to the nearest whole number.

fmod
double fmod(double x, double y);

Returns the remainder of x divided by y.
There is no range limit on the return value. If y is zero, then either a range error will occur or the function will return zero (implementation-defined).

Non “math.h” math functions.
There are some math functions that are located in the “stdlib.h” . These are also extremely useful, and it’s best we mention them here.

abs
int abs(int x);

Returns the absolute value of x. Note that in two’s compliment that the most maximum number cannot be represented as a positive number. The result in this case is undefined.
The absolute value is returned.

div
div_t div(int numer, int denom);

Divides numer (numerator) by denom (denominator). The result is stored in the structure div_t which has two members:
int qout;
int rem;
Where quot is the quotient and rem is the remainder. In the case of inexact division, quot is rounded down to the nearest integer. The value numer is equal to quot * denom + rem.
The value of the division is returned in the structure.

labs
long int labs(long int x);

Returns the absolute value of x. Not that in two’s compliment that the most maximum number cannot be represented as a positive number. The result in this case is undefined.
The absolute value is returned.

ldiv
ldiv_t ldiv(long int numer, long int denom);

Divides numer (numerator) by denom (denominator). The result is stored in the structure ldiv_t which has two members:
long int qout;
long int rem;
Where quot is the quotient and rem is the remainder. In the case of inexact division, quot is rounded down to the nearest integer. The value numer is equal to quot * denom + rem.
The value of the division is returned in the structure.

rand
int rand(void);

Returns a pseudo-random number in the range of 0 to RAND_MAX.
The random number is returned.

srand
void srand(unsigned int seed);

This function seeds the random number generator used by the function rand. Seeding srand with the same seed will cause rand to return the same sequence of pseudo-random numbers. If srand is not called, rand acts as if srand(1) has been called.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>