OScript API/Built-in Package Index

Class: Math

The built-in functions in the Math Package allow mathematical operations to be performed upon integer and real number data. Note: All integers automatically convert to Reals with no data loss. All Reals automatically convert to Integers by being truncated.

The Math Package offers the following functionality:

  • Floor() and Ceil() to calculate the nearest integers not greater than or not less than a given number.
  • Round() to round a number, Trunc() to truncate a number, or TruncLeft() to left-truncate a number. RoundSignificant() rounds the integer part of a number to a specified number of significant digits, RoundSignificantInt() is an alias of RoundSignificant, RoundSignificantReal() rounds a number to a specified number of significant digits.
  • Min() and Max() to return the minimum and maximum of two numbers.
  • Abs() to return the absolute value of a number.
  • Log() and Log10() to compute the natural and base-10 logarithm of a number, Sqrt() to calculate a square root, and Power() to calculate an exponent for a given base.
  • Random() to generate random integers.

Class Attributes

Overflow range error -- i.e. math.power( 10, 1000 )

Argument singularity -- i.e. math.power( 0, -2 )

Floating point unit stack overflow.

Underflow range error -- i.e. math.power( 10, -1000 )

Class Methods

Abs( Real number )

Returns the absolute value.

Ceil( Real number )

Returns the number rounded up.

Floor( Real number )

Returns the number rounded down.

Log( Real number )

Returns the natural logarithm.

Log10( Real number )

Returns the base-10 logarithm.

Max( Real number1, Real number2 )

Returns the larger of the numbers.

Min( Real number1, Real number2 )

Returns the smaller of the numbers.

Power( Real base, Real exponent )

Returns the base raised to the exponent.

Random( Integer range, [Integer seed] )

Returns "random" number that is greater than or equal to 0 and less than range.

Round( Real number )

Returns the number rounded to the closest integer.

RoundSignificant( Real number, Integer digits )

Returns the integer part of a number rounded to the specified number of significant digits.

RoundSignificantInt( Real number, Integer digits )

Returns the integer part of a number rounded to the specified number of significant digits.

RoundSignificantReal( Real number, Integer digits )

Returns the number rounded to the specified number of significant digits.

Sqrt( Real number )

Returns the square root of the number.

Trunc( Real number )

Returns the number without the decimal part.

TruncLeft( Real number )

Returns the number without the whole number part.

Class Attributes

Not used.

Error E_PLOSS

Not used.

Argument singularity -- i.e. math.power( 0, -2 )

Floating point unit stack overflow.

Underflow range error -- i.e. math.power( 10, -1000 )

Class Methods

Abs

Real Abs( Real number )

Returns the absolute value of the specified number.

Parameters

number

An Integer or a Real to be evaluated.

Returns:

The absolute value of the specified number as a Real.

Ceil

Real Ceil( Real number )

Returns a Real number corresponding to the smallest Integer value that is not smaller than the specified number.

Parameters

number

A Real number.

Returns:

A Real number corresponding to the smallest Integer value that is not smaller than the specified number.

Example

Here is an example showing the effect of various truncation-related Math functions:

Real    num = 3.14159

Echo( num )
Echo( Math.Floor( num ) )
Echo( Math.Ceil( num ) )
Echo( Math.Round( num ) )
Echo( Math.Trunc( num ) )
Echo( Math.TruncLeft( num ) )

The output of the example is:

3.14159
3
4
3
3
0.14159

Floor

Real Floor( Real number )

Returns a Real number corresponding to the largest Integer value that is not larger than the specified number.

Parameters

number

A Real number.

Returns:

A Real number corresponding to the largest Integer value that is not smaller than the specified number.

Example

See Ceil() for an example.

Log

Real Log( Real number )

Computes the natural logarithm of the specified number.

Parameters

number

Specifies an Integer or a Real which should be non-zero and positive.

Returns:

A Real number corresponding to the natural logarithm, if number is a non-zero, positive value; Undefined otherwise.

Log10

Real Log10( Real number )

Computes the base-10 logarithm of the specified number.

Parameters

number

An Integer or Real which should be non-zero and positive.

Returns:

A Real number corresponding to the base-10 logarithm, if number is a non-zero, positive value; Undefined otherwise.

Example

This short example demonstrates how logarithms can be used to calculate the square root of two:

Echo( Math.Power( 10, Math.Log10( 2 ) / 2 ) )
Echo( Math.Sqrt( 2 ) )

The output of the example is:

1.4142135623731
1.4142135623731

Max

Real Max( Real number1,
          Real number2 )

Returns the larger of two given numbers.

Parameters

number1

An Integer or a Real which is one number to be compared.

number2

An Integer or a Real which is compared to number1.

Returns:

The Real which is the larger value, if successful; Undefined otherwise. Note: The Real value returned can be converted into an Integer by storing the result in an Integer.

Min

Real Min( Real number1,
          Real number2 )

Returns the smaller of two given numbers.

Parameters

number1

An Integer or a Real which is one number to be compared.

number2

An Integer or a Real which is compared to number1.

Returns:

A Real which is the smaller value, if successful; Undefined otherwise. Note: The Real value returned can be converted into an Integer by storing the result in an Integer.

Power

Real Power( Real base,
            Real exponent )

Raises the specified base number to the specified exponent power.

Parameters

base

A Real or Integer.

exponent

A Real or Integer.

Returns:

Specifies any number, Real or Integer. Note: The Real value returned can be forced into an Integer by storing the result in an Integer, if needed.

Example

See Log10() for an example.

Random

Integer Random( Integer range,
                [Integer seed] )

Generates and returns a pseudo-random Integer within the specified range.

Parameters

range

A positive Integer which is the upper limit for the generated random number, all generated random integers will be less than range.

seed

An optional Integer indicating a seed for the set of random numbers from which the returned random number is selected. Defaults to a random seed if not specified.

Returns:

A pseudo-random Integer in the range from 0 to less than range.

Example

Note: Successive execution of the Math.Random function results in a pseudo-random sequence of numbers. Therefore, seed should only be specified once per sequence of random numbers. Specifying the same seed for successive calls to the Math.Random function always generates the same set of numbers.

Round

Real Round( Real number )

Rounds the specified number up or down to the next closest Integer.

Parameters

number

A Real indicating the number to round.

Returns:

A Real number rounded to the next closest Integer value (i.e. 1.5 -> 2, 1.4 -> 1).

Example

See Ceil() for an example.

RoundSignificant

Integer RoundSignificant( Real number,
                          Integer digits )

Rounds the integer part of a number to the specified number of significant digits

Parameters

number
A real number to round. 
                If a value of zero is provided, the function will return MININTEGER.
digits

A positive integer indicating the number of significant digits to round to. If a zero or negative number is provided, the function will return zero.

Returns:

An integer number rounded to the number of significant digits.

Example

        Examples:

            Echo( Math.RoundSignificant( 0, 1 ) )
            Echo( Math.RoundSignificant( 12.3, -1 ) )
            Echo( Math.RoundSignificant( 12.3, 0 ) )
            Echo( Math.RoundSignificant( 12.3, 1 ) )
            Echo( Math.RoundSignificant( 12.3, 2 ) )
            Echo( Math.RoundSignificant( 12.3, 3 ) )
        The outputs of the examples are:

               -2147483648
               0
               0
               10
               12
               12

RoundSignificantInt

Integer RoundSignificantInt( Real number,
                             Integer digits )

Rounds the integer part of a number to the specified number of significant digits. It is an alias of RoundSignificant

Parameters

number
A real number to round. 
                If a value of zero is provided, the function will return MININTEGER.
digits

A positive integer indicating the number of significant digits to round to. If a zero or negative number is provided, the function will return zero.

Returns:

An integer number rounded to the number of significant digits.

Example

        See [[RoundSignificant()]] for examples.

RoundSignificantReal

Real RoundSignificantReal( Real number,
                           Integer digits )

Rounds the number to the specified number of significant digits

Parameters

number
A real number to round. 
                If a value of zero is provided, the function will return Undefined.
digits

A positive integer indicating the number of significant digits to round to. If a zero or negative number is provided, the function will return Undefined.

Returns:

A real number rounded to the number of significant digits.

Example

        Examples:

            Echo( Math.RoundSignificantReal( 0, 1 ) )
            Echo( Math.RoundSignificantReal( 12.3, -1 ) )
            Echo( Math.RoundSignificantReal( 12.3, 0 ) )
            Echo( Math.RoundSignificantReal( 12.3, 1 ) )
            Echo( Math.RoundSignificantReal( 12.3, 2 ) )
            Echo( Math.RoundSignificantReal( 12.3, 3 ) )
        The outputs of the examples are:

               Undefined
               Undefined
               Undefined
               10
               12
               12.3

Sqrt

Real Sqrt( Real number )

Calculates the square root of the specified number.

Parameters

number

An Integer or a Real indicating the number on which to operate.

Returns:

The square root of the specified number as a Real, if successful; Undefined otherwise.

Example

See Log10() for an example.

Trunc

Real Trunc( Real number )

Truncates the decimal part of a Real number.

Parameters

number

A Real number to be truncated.

Returns:

A Real number whose signed numerals to the left of the decimal point are the same as the specified number and whose numerals to the right of the decimal point are set to zero, if successful; Undefined otherwise.

Example

See Ceil() for an example.

TruncLeft

Real TruncLeft( Real number )

Truncates the whole number part from a Real number.

Parameters

number

A Real number to be left-truncated.

Returns:

A Real number whose sign and numerals to the right of the decimal point are the same as the specified number and whose numerals to the left of the decimal point are set to zero, if successful; Undefined otherwise.

Example

See Ceil() for an example.

 Copyright © 2021 OpenText Corporation. All rights reserved.