|Home | Tutorial | Classes | Functions | QSA Workbench | Language | Qt API | QSA Articles | Qt Script for Applications |

[Prev: Built-in Objects] [Home] [Next: System]

#### Math

The `Math` object always exists in a Qt Script program. Use the `Math` object to access mathematical constants and functions, e.g.

        var x, angle, y;
        with ( Math ) {
            x = PI * 2;
            angle = 1.3;
            y = x * sin( angle );
        }

The `Math` object supports all the common mathematical functions, for example: `abs()`, `acos()` and `cos()`, `asin()` and `sin()`, `atan()`, `atan2()` and `tan()`, `ceil()`, `floor()` and `round()`, `exp()` and `log()`, `max()` and `min()`, `pow()` and `sqrt()`, `random()`, and `round()`.

See also, + operator, ++ operator, - operator, -- operator, * operator, / operator, % operator, -= operator, += operator, *= operator, /= operator and %= operator.

##### Math Properties

All the `Math` properties are read-only constants.

- E -- Euler's constant. The base for natural logarithms.

- LN2 -- Natural logarithm of 2.

- LN10 -- Natural logarithm of 10.

- LOG2E -- Base 2 logarithm of E.

- LOG10E -- Base 10 logarithm of E.

- PI -- Pi.

- SQRT1_2 -- Square root of 1/2.

- SQRT2 -- Square root of 2.

##### Math Functions

- abs( number : Number ) : Number;

  Returns the absolute value of the given `number`. The equivalent of

          x = x < 0 ? -x : x;

          var x = -99;
          var y = 99;
          with ( Math ) {
              x = abs( x );
              y = abs( y );
          }
          if ( x == y ) System.println( "equal" );

- acos( number : Number ) : Number; Returns the arccosine of the given `number` in radians between 0 and `Math.PI`. If the number is out of range, returns NaN.

- asin( number : Number ) : Number; Returns the arcsine of the given `number` in radians between `-Math.PI/2` and `Math.PI/2`. If the number is out of range, returns NaN.

- atan( number : Number ) : Number; Returns the arctangent of the given `number` in radians between `-Math.PI/2` and `Math.PI/2`. If the number is out of range, returns NaN.

- atan2( yCoord : Number, xCoord : Number ) : Number; Returns the counter-clockwise angle in radians between the positive x-axis and the point at (`xCoord`, `yCoord`). The value returned is always between `-Math.PI` and `Math.PI`.

  Example:

          function polar( x, y )
          {
              return Math.atan2( y, x );
          }

- ceil( number : Number ) : Number; If the `number` is an integer, it returns the `number`. If the `number` is a floating point value, it returns the smallest integer greater than the `number`.

  Example:

          var x = 913.41;
          x = Math.ceil( x ); // x == 914
          var y = -33.97;
          y = Math.ceil( y ); // y == -33

- cos( number : Number ) : Number; Returns the cosine of the given `number`. The value will be in the range -1..1.

- exp( number : Number ) : Number; Returns `Math.E` raised to the power of the given `number`.

- floor( number : Number ) : Number; If the `number` is an integer, it returns the `number`. If the `number` is a floating point value, it returns the greatest integer less than the `number`.

- log( number : Number ) : Number; If the `number` is > 0, it returns the natural logarithm of the given `number`. If the `number` is 0, it returns `Infinity`. If the `number` is < 0, it returns NaN.

- max( number1 : Number, number2 : Number ) : Number; Returns the largest of `number1` and `number2`.

- min( number1 : Number, number2 : Number ) : Number; Returns the smallest of `number1` and `number2`.

- pow( number : Number, power : Number ) : Number; Returns the value of the `number` raised to the `power`.

- random() : Number; Returns a pseudo-random floating point number between 0 and 1. Pseudo random numbers are not truly random, but may be adequate for some applications, for example, games and simple simulations.

- round( number : Number ) : Number; Returns the `number` rounded to the nearest integer. If the fractional part of the `number` is >= 0.5, the `number` is rounded up; otherwise it is rounded down.

- sin( number : Number ) : Number; Returns the sine of the given `number`. The value will be in the range -1..1.

- sqrt( number : Number ) : Number; If the `number` is >= 0, it returns the square root. If the `number` is < 0, it returns NaN.

- tan( number : Number ) : Number; Returns the tangent of the given `number`.

[Prev: Built-in Objects] [Home] [Next: System]