Class Math

From TrainzOnline
Revision as of 02:21, 10 February 2010 by Windwalkr (Talk | contribs)

Jump to: navigation, search


  • Math is a static class proving mathematical utility functions.
  • When used in game code these methods must be prefixed Math. to reference the class.


Contents

Abs

public int Abs(int number)
Parameters
  • number = integer value to be processed.
Returned Value
  • The absolute value of number.
Syntax
int answer = Math.Abs(-1);              // returns 1
int answer = Math.Abs(1);               // returns 1
Notes


Fabs

public float Fabs(float number)
Parameters
  • number = floating point value to be processed.
Returned Value
  • The absolute value of number.
Syntax
float answer = Math.Fabs(-1.0);              // returns 1.0
float answer = Math.Fabs(1.0);               // returns 1.0
Notes


Fmax

public float Fmax(float number1, float number2)
Parameters
  • number1, number2 = floating point values to be compared.
Returned Value
  • The greater of the two parameters.
Syntax
float answer = Math.Fmax(1.0,2.0);              // returns 2.0
Notes


Fmin

public float Fmin(float number1, float number2)
Parameters
  • number1, number2 = floating point values to be compared.
Returned Value
  • The lesser of the two parameters.
Syntax
float answer = Math.Fmin(1.0,2.0);              // returns 1.0
Notes


Rand

public native int Rand(int lower, int upper)
public native float Rand(float lower, float upper)
Parameters
  • lower = minimum value to be returned (inclusive)
  • upper = maximum value to be returned (exclusive)
Returned Value
  • A random floating point or integer value within the specified range.
Syntax
int random = Math.Rand(1,10);         // returns a random number between 1 and 9 (see below) 
float random = Math.Rand(1.0,10.0);   // returns a random number between 1.0 and 9.9999999... (see below) 
Notes
  • Rand operates on floating point numbers but if both parameters are integers it will return an integer result.
  • If either parameter is a float then the result will also be a float.
  • The lower bound is inclusive and is therefore included in the set of possible return values.
  • The upper bound is exclusive and is never included.
  • In the case of an integer result the returned value is rounded down to an integral value. Because of this the call Math.Rand(1,10) will return a result between 1 and 9.


Sqrt

public native float Sqrt(float number)
Parameters
  • number = floating point value to find the square root of.
Returned Value
  • The square root of the parameter.
Syntax
float root = Math.Sqrt(64.0);              // returns 8.0
Notes


Related Methods

TrainUtil.RandBool()

Str.ToInt()

Str.ToFloat()

Str.UnpackInt()

Str.UnpackFloat()


Categories

Personal tools