Class Math: Difference between revisions

From TrainzOnline
Jump to navigationJump to search
Andi06 (talk | contribs)
 
Danny252 (talk | contribs)
m →Related Methods: Add link to RandBool
 
(2 intermediate revisions by one other user not shown)
Line 1: Line 1:
*[[TrainzScript Langage Reference|API Hierarchy]]
*[[TrainzScript Language Reference|API Hierarchy]]
**Math
**Math
<br>
<br>
Line 84: Line 84:
==Related Methods==
==Related Methods==


TrainUtil.RandBool()
[[Class TrainUtil#RandBool|TrainUtil.RandBool()]]


[[Class Str#ToInt|Str.ToInt()]]
[[Class Str#ToInt|Str.ToInt()]]
Line 93: Line 93:


[[Class Str#UnpackFloat|Str.UnpackFloat()]]
[[Class Str#UnpackFloat|Str.UnpackFloat()]]


==Categories==
==Categories==
[[Category:Script Class]]
[[Category:Script Class]]

Latest revision as of 14:50, 28 December 2020


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


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


TrainUtil.RandBool()

Str.ToInt()

Str.ToFloat()

Str.UnpackInt()

Str.UnpackFloat()

Categories