In addition to the arithmetical functions, FreeBasic has a rich set of mathematical functions. These functions are listed in the following table.

Function | Syntax | Comments |
---|---|---|

ABS (Absolute Value) | B = Abs(expression) | Returns the unsigned value of expression. This is an overloaded function so expression can resolve to an integer, longint or double. |

ACOS (ArcCosine) | B = Acos(expression) | Returns the ArcCosine of a double-type expression in radians. Expression must be in the range of -1 to 1. |

ASIN (ArcSine) | B = Asin(expression) | Returns the ArcSine of a double-type expression in radians. Expression must be in the range of -1 to 1. |

ATAN2 (ArcTangent of Ratio) | B = Atan2(expressionV, expressionH) | Returns the ArcTangent of of ratio of double-type expression1 / expression2 in radians. ExpressionV is the vertical component and expressionH is the horizontal component, where both expressions are double-type values. |

ATAN (ArcTangent) | B = Atan(expression) | Returns the ArcTangent of a double-type expression in radians. Expression must be in the range of -Pi/2 to Pi/2. |

COS (Cosine) | B = Cos(expression) | Returns the Cosine of a double-type expression in radians. Expression must be in the range of -1 to 1 and is the angle measured in radians. |

EXP (E Exponent) | B = Exp(expression) | Returns e (approx 2.716) raised to the power of a double-type expression. |

LOG (Logarithm) | B = Log(expression) | Returns the natural Logarithm (base e) of a double-type expression. |

SGN (Sign) | B = Sgn(expression) | Returns the sign of a double-type expression. If expression is greater than 0 then Sgn returns a 1. If expression is 0, then Sgn returns 0. If expression is less than 0, then Sgn return -1. |

SIN (Sine) | B = Sin(expression) | Returns the Sine of a double-type expression in radians. Expression must be in the range -1 to 1. |

SQR (Square Root) | B = Sqr(expression) | Returns the square root of a double-type expression. Expression must be greater than or equal to 0. |

TAN (Tangent) | B = Tan(expression) | Returns the Tangent of a double-type expression in radians. Expression is the angle measured in radians. |

## Trigonometric Functions

All of the trigonometric functions return the measured angle in radians not degrees. To convert degrees to radians you would use the formula:

`radian = degree * PI / 180`

To convert radians to degrees you would use the formula:

`degree = radian * 180 / PI`

PI is the ratio of the circumference of a circle to its diameter and can be calculated using the Atn function with the formula:

`PI = 4 * Atn(1.0)`

## Absolute Value and the Sgn Function

You would use the Abs function when you are interested in the absolute magnitude of a number. The absolute value of a number is the distance that number is from zero on the real number line. 3 is three units from 0 so its absolute magnitude is 3. -3 is also three units from 0 so its absolute magnitude is also 3.

The Sgn function, called the signum function in math, is related to the Abs function since you can express the absolute value of a number by using the Sgn function. Sgn returns -1 for a negative number, 0 for 0 and 1 for a positive number. To calculate the absolute value of a number you would use the formula:

`|a| = a * Sgn(a)`

The vertical lines indicate absolute value. If we replace a with -3 in the formula, you get |-3| = -3 * Sgn(-3), or |-3| = -3 * -1. Since multiplying a negative with a negative returns a positive, the result is |-3| = 3. Replacing a with 3, the formula would evaluate to |3| = 3 * 1, or |3| = 3.

One use of the Sgn function in programming is determining the relative positions of two points on the screen. If you have two points, A(5, 6,) and B(3, 4), where the first number is the row and the second number is the column, you can determine the relative position by moving the origin to point A and looking at the sign of point B. The origin (0, 0) of the screen is located in the upper left corner. To move the origin to point A you need to subtract 5 from the row and 6 from the column of each point giving A(0, 0) and B(-2, -2). You can now use the Sgn function on the row and column components of B.

- If Sgn(B.row) = -1, then the row is above A.
- If Sgn(B.row) = 0, then the row is in the same row as A.
- If Sgn(B.row) = 1 then the row is below A.
- If Sgn(B.column) = -1 then the column is to the left of A.
- If Sgn(B.column) = 0 then the column is in the same column as A.
- If Sgn(B.column) = -1 then the column is to the right of A.

Since B is located at row -2 and column -2 after adjusting the origin, the Sgn function will return -1 for the row and -1 for the column. This puts point B above and to the left of of point A.

## Logarithms

Logarithms have a wide range of uses in mathematics, physics and biology. For example, the decay rate of radioactive particles is logarithmic in nature. Logarithms are a way of expressing exponentiation.

For the following formula Ab = x can be expressed as Log(x) = b where the base of the log is a. The two most commonly used logarithms are the natural log, which uses e (2.7182818284590452354…) as the base, and log base 10 or the common log. The Log function in FreeBasic uses the natural log, but any log base can be expressed by the formula:

`logarithm = Log(number) / Log(base)1.`

One property of logarithms is that they can be used to multiply or divide two numbers together. This was a common use of logarithms before the invention of calculators.

## The C Runtime Library Math Constants

The C Runtime Library (CRT) has a number of math constants that you can use in your programs. To use these functions you would add the line #Include Once “crt.bi” to your program. The following table lists the math constants available in math.bi.

Constants | Value | Comment |
---|---|---|

M_E | 2.7182818284590452354 | The base of natural logarithms. |

M_LOG2E | 1.4426950408889634074 | The logarithm to base 2 of M_E. |

M_LOG10E | 0.43429448190325182765 | The logarithm to base 10 of M_E. |

M_LN2 | 0.69314718055994530942 | The natural logarithm of 2. |

M_LN10 | 2.30258509299404568402 | The natural logarithm of 10. |

M_PI | 3.14159265358979323846 | Pi, which is the ratio of a circle's circumference to its diameter. |

M_PI_2 | 1.57079632679489661923 | Pi divided by two. |

M_PI_4 | 0.78539816339744830962 | Pi divided by four. |

M_1_PI | 0.31830988618379067154 | The reciprocal of pi, that is 1/pi. |

M_2_PI | 0.63661977236758134308 | Two times the reciprocal of pi. |

M_2_SQRTPI | 1.12837916709551257390 | Two times the reciprocal of the square root of pi. |

M_SQRT2 | 1.41421356237309504880 | The square root of two. |

M_SQRT1_2 | 0.70710678118654752440 | The reciprocal of the square root of two which is also the square root of 1/2. |

## Selected CRT Math Functions

There are a number of math functions declared in the CRT. The following table lists three of the commonly used functions.

Function | Syntax | Comment |
---|---|---|

Hypot | B = Hypot(x as double, y as double) | Hypot returns the hypotenuse of sides x and y. |

Log10 | B = Log10(x as double) | Log10 returns the base 10 log of x. |

Pow | B = Pow(base as double, power as double) | Pow returns base raised to power. |

Using the CRT functions can make your program a bit more simple, and a bit less error prone. The CRT functions are stable and reliable, and many include optimizations that make the functions perform better than you could achieve by hand coding.