Arithmetic Operators

Arithmetic operators are the familiar mathematical ones and are all binary operators: plus (+), minus (-), divide (/), multiply (*). There is also remainder (%) which gives the leftovers of a division. For example: 9/4 is 2.25, but 4 goes into 9 twice with one left over, so 9 % 4 is 1. This operator is also called 'modulo'. For division of real numbers, not integers, the leftover is a real number, so for example 5.5 % 2.2 = 1.1. Used carelessly, these operators can produce NaN or Infinity: for example, if you accidentally divide by 0.

a = 2 + 3;  b = 2 * 3;   c = 2 / 3;   d = 2 - 3;   e = 2 % 3;

Microsoft products with Jscript version 1.0 truncate floating point numbers to integers before applying %, so 5.5 % 2.2 = 1.

© 1997 by Wrox Press. All rights reserved.