16
Arithmetic in JavaScript Mathematical operations

Javascript - Arithmetic in Javascript

Embed Size (px)

Citation preview

Page 1: Javascript - Arithmetic in Javascript

Arithmetic in JavaScript

Mathematical operations

Page 2: Javascript - Arithmetic in Javascript

Arithmetic in JavaScript

• JavaScript programs work with numbers

using the arithmetic operators that the

language provides.

Page 3: Javascript - Arithmetic in Javascript

Arithmetic in JavaScript

• + for addition,

• - for subtraction,

• * for multiplication,

• / for division,

• and % for modulo.

Page 4: Javascript - Arithmetic in Javascript

Arithmetic in JavaScript

• + for addition,

• - for subtraction,

• * for multiplication,

• / for division,

• and % for modulo.

Page 5: Javascript - Arithmetic in Javascript

Arithmetic in JavaScript

• JavaScript supports more complexmathematical operations through a

set of functions and constants defined

as properties of the Math object.

Page 6: Javascript - Arithmetic in Javascript

Arithmetic in JavaScript

Page 7: Javascript - Arithmetic in Javascript

Arithmetic in JavaScript

• Arithmetic in JavaScript does not raise

errors in cases of overflow,underflow, or division by zero.

Page 8: Javascript - Arithmetic in Javascript

Arithmetic in JavaScript

• When the result of a numeric operation is

larger than the largestrepresentable number (overflow),

the result is a special infinity value, whichJavaScript prints as Infinity.

Page 9: Javascript - Arithmetic in Javascript

Arithmetic in JavaScript

• when a negative value becomes larger

than the largest representable negativenumber, the result is negative infinity,

printed as -Infinity.

Page 10: Javascript - Arithmetic in Javascript

Arithmetic in JavaScript

• When a negative value becomes larger

than the largest representable negativenumber, the result is negative infinity,

printed as -Infinity.

Page 11: Javascript - Arithmetic in Javascript

Arithmetic in JavaScript

• Underflow occurs when the result of a

numeric operation is closer tozero than the smallest representable

number.

• In this case, JavaScript returns 0.

Page 12: Javascript - Arithmetic in Javascript

Arithmetic in JavaScript

• If underflow occurs from a

negative number,

• JavaScript returns a special value known

as “negative zero.”

Page 13: Javascript - Arithmetic in Javascript

Arithmetic in JavaScript

• Division by zero is not an error in

JavaScript: it simply returns infinity or

negative infinity.

• 0/0 is not Infinity

• There is one exception!!

Page 14: Javascript - Arithmetic in Javascript

Arithmetic in JavaScript

• zero divided by zero does not have a

well defined value, and the result of

this operation is the special not-a-

number value, printed as NaN.

• 0/0 = NaN

Page 15: Javascript - Arithmetic in Javascript

Arithmetic in JavaScript

• NaN also arises if you attempt to

divide infinity by infinity.

• take the square root of a negativenumber (NaN).

Page 16: Javascript - Arithmetic in Javascript

Arithmetic in JavaScript