Chapter 3 DynaScript Predefined Objects


Math object

Object

The Math object provides basic Math functionality to users.

Standard: ECMAScript

Syntax

To use a Math property:

Math.propertyName

To use a Math method:

Math.MethodName( parameter ) 

Description

The Math object provides basic math functionality in your PowerDynamo script or template.

The Math object is provided as a built-in object. There are no constructors for creating instances of a Math object. When referring to Math in this chapter, we are referring to the built-in Math object, not an instance of the Math object.

Example

This example uses the pow method of the Math object and displays the results:

<!--SCRIPT 
   ans = Math.pow( 4, 3 );
   document.WriteLn(ans)
-->

E property

Syntax

Math.E 

Attributes

This property is read-only.

Description

Returns the number value for e, the base of the natural logarithms, which is approximately 2.71828182845905.

Return

Floating point

Example

To display the value of e:

<!--SCRIPT 
   a = Math.E;
   document.WriteLn( a );
-->

LN2 property

Syntax

Math.LN2 

Attributes

This property is read-only.

Description

Returns the number value for the natural logarithm of 2, which is approximately 0.693147180559945.

Return

Floating point

Example

To display the value of LN2:

<!--SCRIPT
    a = Math.LN2;
    document.WriteLn( a );
-->

LN10 property

Syntax

Math.LN10 

Attributes

This property is read-only.

Description

Returns the value for the natural logarithm of 10, which is approximately 2.30258509299405.

Return

Floating point

Example

To display the value of LN10:

<!--SCRIPT
    a = Math.LN10;
    document.WriteLn( a );
-->

LOG2E property

Syntax

Math.LOG2E 

Attributes

This property is read-only.

Description

Returns the number value for the base-2 logarithm of e, Euler's constant. This value is approximately 1.44269504088896.

Return

Floating point

Example

To display the value of LOG2E:

<!--SCRIPT 
    a = Math.LOG2E;
    document.WriteLn( a );
-->

LOG10E property

Syntax

Math.LOG10E 

Attributes

This property is read-only.

Description

Returns the number value for the base-10 logarithm of e, Euler's constant. This value is approximately 0.434294481903252.

Return

Floating point

Example

To display the value of LOG10E:

<!--SCRIPT
    a = Math.LOG10E;
    document.WriteLn( a );
-->

PI property

Syntax

Math.PI 

Attributes

This property is read-only.

Description

Returns the ratio of the circumference of a circle to its diameter, which is approximately 3.14159265358979.

Return

Floating point

Example

To display the value of PI:

<!--SCRIPT 
    a = Math.PI;
    document.WriteLn( a );
-->

SQRT1_2 property

Syntax

Math.SQRT1_2 

Attributes

This property is read-only.

Description

Returns the number value for the square root of 1/2, which is approximately 0.707106781186548.

Return

Floating point

Example

To display the value of the square root of 1/2:

<!--SCRIPT
    a = Math.SQRT1_2;
    document.WriteLn( a );
-->

SQRT2 property

Syntax

Math.SQRT2 

Attributes

This property is read-only.

Description

Returns the number value for the square root of 2, which is approximately 1.4142135623731.

Return

Floating point

Example

To display the value of the square root of 2:

<!--SCRIPT
    a = Math.SQRT2;
    document.WriteLn( a );
-->

abs method

Syntax

Math.abs( num )

Description

Returns the absolute value of a number.

Return

The absolute value of the number argument

Example

This example returns the absolute value of -8:

<!--SCRIPT 
   document.WriteLn( Math.abs( -8 ) );
-->

acos method

Syntax

Math.acos( num )

Description

Returns the arc cosine of a number.

Return

Floating point

Example

This example returns the arc cosine of .43:

<!--SCRIPT 
   document.WriteLn( Math.acos( .43 ) );
-->

asin method

Syntax

Math.asin( )

Description

Returns the arc sine of a number.

Return

Floating point

Example

This example returns the arc sine of 1:

<!--SCRIPT 
   document.WriteLn( Math.asin( 1 ) );
-->

atan method

Syntax

Math.atan( num )

Description

Returns the arc tangent of a number.

Return

Floating point

Example

This example displays the arc tangent of .43:

<!--SCRIPT 
   document.WriteLn( Math.atan( .43 ) );
-->

atan2 method

Syntax

Math.atan2( y, x )

Description

Returns the angle in radians from the x axis to the y, x point. The parameters are:

Return

Floating point

Example

This example returns the angle from the x axis to the y, x point:

<!--SCRIPT 
   document.WriteLn( Math.atan2( 3, -12 ) );
-->

ceil method

Syntax

Math.ceil( num )

Description

Returns the smallest number value that is not less than the argument and is equal to a mathematical integer.

Return

Integer.

Example

This example displays the smallest integer possible that is not less than 6.333:

<!--SCRIPT 
   document.WriteLn( Math.ceil( 6.333 ) );
-->

cos method

Syntax

Math.cos( num )

Description

Returns the cosine of a number.

Return

Floating point

Example

This example returns the cosine of 45:

<!--SCRIPT 
   document.WriteLn( Math.cos( 45 ) );
-->

exp method

Syntax

Math.exp( num )

Description

Returns E raised to the power of the argument (num).

Return

Floating point

Example

This example calculates and displays the value of E to the power of 8:

<!--SCRIPT 
   document.WriteLn( Math.exp( 8 ) );
-->

floor method

Syntax

Math.floor( num )

Description

Returns the greatest number value that is not greater than the argument and is equal to a mathematical integer.

Return

Integer

Example

This example calculates and displays the greatest number value that is not greater than the argument and is equal to a mathematical integer:

<!--SCRIPT 
   document.WriteLn( Math.floor( 634.8 ));
-->

log method

Syntax

Math.log( num )

Description

Returns the natural logarithm of a number.

Return

Floating point

Example

This example displays the logarithm of the number 6:

<!--SCRIPT 
   document.WriteLn( Math.log( 6 ) );
-->

max method

Syntax

Math.max( numExp, numValue )

Description

Returns the larger of two arguments. The parameters are:

Return

Floating point

Example

This example returns the larger value of x and y:

<!--SCRIPT 
    x = 7.543643;
    y = 7.954854345;
    minValue = Math.max( x, y );
    document.WriteLn( minValue );
-->

min method

Syntax

Math.min( numExp, numValue )

Description

Returns the smaller of the two arguments. The parameters are:

Return

Floating point

Example

This example returns the smaller value:

<!--SCRIPT 
    x = 7.543643;
    y = 7.954854345;
    minValue = Math.min( x, y );
    document.WriteLn( minValue );
-->

pow method

Syntax

Math.pow( num, exponent )

Description

Returns the result of raising x to the power y. The parameters are:

Return

Floating point

Example

This example returns the results of calculating 3 to the power of 8:

<!--SCRIPT 
    x = Math.pow( 3, 8 );
    document.WriteLn( x );
-->

random method

Syntax

Math.random( )

Description

Returns a randomly generated positive number equal to or greater than 0 but less than 1.

Return

Floating point

Example

To return a number s equal to or greater than 0 but less than 1:

<!--SCRIPT 
    x = Math.random();
    document.WriteLn( x );
-->

round method

Syntax

Math.round( num )

Description

Returns the supplied number, rounded to the nearest integer.

Return

Integer

Example

This example rounds the number 54.354 to the nearest integer:

<!--SCRIPT 
    document.WriteLn( Math.round( 54.354 ) );
-->

sin method

Syntax

Math.sin( num )

Description

Returns the sine of a number.

Return

Floating point

Example

This example displays the sine of 54:

<!--SCRIPT 
    document.WriteLn( Math.sin( 54 ) );
-->

sqrt method

Syntax

Math.sqrt( num )

Description

Returns the square root of a number.

Return

Floating point

Example

This example displays the square root of 64:

<!--SCRIPT 
    document.WriteLn( Math.sqrt(64) );
-->

tan method

Syntax

Math.tan( num )

Description

Returns the tangent of a number.

Return

Floating point

Example

This example displays the tangent of 43:

<!--SCRIPT 
    document.WriteLn( Math.tan( 43 ) );
-->

 


Copyright © 2001 Sybase, Inc. All rights reserved.