Image inserted by SSuite Office Fandango Desktop EditorSSuite Basic-Math Arithmetic Operators

Arithmetic operators are used to perform arithmetic between variables and/or values.


Given that y = 5, the table below explains the arithmetic operators:

Operator Description Example Result in y Result in x
+ Addition = y + 2 y = 5 x = 7
- Subtraction = y - 2 y = 5 x = 3
* Multiplication = y * 2 y = 5 x = 10
/ Division = y / 2 y = 5 x = 2.5
% Modulus (division remainder) = y % 2 y = 5 x = 1
** Exponent (standard =5^2 ) = y ** 2 y = 5 x = 25

 
 

Please be aware that all methods are case sensitive. They must be entered exactly as described e.g. Math.PI , Math.ceil(x), Math.SQRT2


To use more advanced methods, use the following notation for each function:

=Math.ceil(4.4)     // returns 5

=Math.floor(4.7)    // returns 4

=Math.sin(90 * Math.PI / 180)     // returns 1 (the sine of 90 degrees)

=Math.min(0, 150, 30, 20, -8, -200// returns -200

=Math.random()     // returns a random number

Example

Return a random number between 1 and 100:

=Math.floor((Math.random() * 100) + 1);

=Math.sqrt(16)      // Returns the square root of 16

 


 

Number Methods

Example 1
=(100 + 23).toString();   // returns 123 from expression 100 + 23

Example 2
=
9.656.toExponential(2)     // returns 9.66e+0
=
9.656.toExponential(4)     // returns 9.6560e+0
=
9.656.toExponential(6)     // returns 9.656000e+0

Example 3
=
9.656.toFixed(0)           // returns 10
=9.656.toFixed(2)           // returns 9.66
=9.656.toFixed(4)           // returns 9.6560
=9.656.toFixed(6)           // returns 9.656000

Example 4
=9.656.toPrecision()        // returns 9.656
=9.656.toPrecision(2)       // returns 9.7
=9.656.toPrecision(4)       // returns 9.656
=9.656.toPrecision(6)       // returns 9.65600

Example 5
=Boolean(10 > 9)        // returns true = 1
=(10 > 9)               // also returns true
=10 > 9                 // also returns true
=Boolean(A4 > E8      // will return either 1 or 0 / True=1 False=0



 

Math Objects

Math.E        // returns Euler's number
Math.PI       // returns PI
Math.SQRT2    // returns the square root of 2
Math.SQRT1_2  // returns the square root of 1/2
Math.LN2      // returns the natural logarithm of 2
Math.LN10     // returns the natural logarithm of 10
Math.LOG2E    // returns base 2 logarithm of E
Math.LOG10E   // returns base 10 logarithm of E



 

Math Methods / Functions        =Math.method( x )


Method Description
abs(x) Returns the absolute value of x
acos(x) Returns the arccosine of x, in radians
asin(x) Returns the arcsine of x, in radians
atan(x) Returns the arctangent of x as a numeric value between -PI/2 and PI/2 radians
atan2(y, x) Returns the arctangent of the quotient of its arguments
ceil(x) Returns the value of x rounded up to its nearest integer
cos(x) Returns the cosine of x (x is in radians)
exp(x) Returns the value of Ex
floor(x) Returns the value of x rounded down to its nearest integer
log(x) Returns the natural logarithm (base E) of x
max(x, y, z, ..., n) Returns the number with the highest value
min(x, y, z, ..., n) Returns the number with the lowest value
pow(x, y) Returns the value of x to the power of y
random() Returns a random number between 0 and 1
round(x) Returns the value of x rounded to its nearest integer
sin(x) Returns the sine of x (x is in radians)
sqrt(x) Returns the square root of x
tan(x) Returns the tangent of an angle

 
 

Bitwise Operators

Operator Name Description
& AND Sets each bit to 1 if both bits are 1
| OR Sets each bit to 1 if one of two bits is 1
^ XOR Sets each bit to 1 if only one of two bits is 1
~ NOT Inverts all the bits
<< Zero fill left shift Shifts left by pushing zeros in from the right and let the leftmost bits fall off
>> Signed right shift Shifts right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off
>>> Zero fill right shift Shifts right by pushing zeros in from the left, and let the rightmost bits fall off

 


 

Examples of Bitwise Operations

Operation Result Same as Result
= 5 & 1 1 0101 & 0001  0001
= 5 | 1 5 0101 | 0001  0101
= ~ 5 10 (4bit unsigned) / -6 (32bit signed)
 ~0101  1010
= 5 << 1 10 0101 << 1  1010
= 5 ^ 1 4 0101 ^ 0001  0100
= 5 >> 1 2 0101 >> 1  0010
= 5 >>> 1 2 0101 >>> 1  0010