BlitzMax/Modules/Math/Math

From Wikibooks, open books for an open world
Jump to: navigation, search

Functions[edit]

IsNan[edit]

Function IsNan( x:Double )

Description: Check if a value is NAN

Returns: True if x is 'not a number' (e.g.: Sqr(-1))

Example:

Local a:Float = Sqr(-1)

Local b:Int = IsNan(a)  'b will equal True in this example, because the square root of -1 is 'not a number'

IsInf[edit]

Function IsInf( x:Double )

Description: Check if a value is infinite (e.g.: 1.0/0.0)

Returns: True if x is infinite

Example:

Local a:Float = 1.0 / 0.0

Local b:Int = IsInf(a)  'b will equal True in this example, because 1.0 divided by 0.0 is infinite

Sqr[edit]

Function Sqr:Double( x:Double )

Description: Square root of x

Example:

Local a:Int = 4
Local b:Int = Sqr(a)

Sin[edit]

Function Sin:Double( x:Double )

Description: Sine of x degrees

Cos[edit]

Function Cos:Double( x:Double )

Description: Cosine of x degrees

Tan[edit]

Function Tan:Double( x:Double )

Description: Tangent of x degrees

ASin[edit]

Function ASin:Double( x:Double )

Description: Inverse Sine of x

Comments: The inverse sine can determine the remaining angles of a right triangle when the lengths of the sides are known. For instance, an angle may be calculated using the length of the opposite side and the hypotenuse.

Example:

Local alpha:Double = ASin(length_opposite / length_hypotenuse)

ACos[edit]

Function ACos:Double( x:Double )

Description: Inverse Cosine of x

Comments: The inverse cosine can determine the remaining angles of a right triangle when the lengths of the sides are known. For instance, an angle may be calculated using the length of the adjacent side and the hypotenuse.

Example:

Local alpha:Double = ACos(length_adjacent / length_hypotenuse)

ATan[edit]

Function ATan:Double( x:Double )

Description: Inverse Tangent of x

Comments: The inverse tangent can determine the remaining angles of a right triangle when the lengths of the sides are known. For instance, an angle may be calculated using the length of the opposite side and the adjacent side.

Example:

Local alpha:Double = ATan(length_opposite / length_adjacent)

ATan2[edit]

Function ATan2:Double( y:Double,x:Double )

Description: Inverse Tangent of two variables x , y

Comments: ATan2 is similar to ATan. However, it makes use of the two arguments signs to determine the angle of a imaginary vector (x,y) relatively to the x-axis (counterclockwise). This is very useful, for example in determining the direction from one point to another.

Example:

Local ax:Float = -1, ay:Float =- 2 'Point A
Local bx:Float = 3, by:Float = 2        'Point B

'Notice how the variables we are using are Float's, BlitzMax will automatically
'convert them into Double's when we call ATan2()
Local angle:Float = ATan2(by - ay, bx - ax)     'Angle from A to B

Sinh[edit]

Function Sinh:Double( x:Double )

Description: Hyperbolic sine of x

Cosh[edit]

Function Cosh:Double( x:Double )

Description: Hyperbolic cosine of x

Tanh[edit]

Function Tanh:Double( x:Double )

Description: Hyperbolic tangent of x

Exp[edit]

Function Exp:Double( x:Double )

Description: Exponential function

Log[edit]

Function Log:Double( x:Double )

Description: Natural logarithm

Log10[edit]

Function Log10:Double( x:Double )

Description: Base 10 logarithm

Ceil[edit]

Function Ceil:Double( x:Double )

Description: Smallest integral value not less than x

Floor[edit]

Function Floor:Double( x:Double )

Description: Largest integral value not greater than x

Comments: Ceil() will round a variable up, whereas Floor() will round it down.

Example:

Local a:Float = 3.5

Local round_up:Float = Ceil(a)          'Becomes 4.0
Local round_down:Float = Floor(a)       'Becomes 3.0