Tutorial - Numerical Expressions

We have already seen the use of the type 'Number' without really going into detail. Now is the time for that.

The Number Type

The Number type can contain either an integer or a floating point number. Pixel coordinates are actually floating point values. The distinction between integer and floating point will not affect most people so feel free to ignore the following discussion.

Integers are stored as 31 bit signed values. Floating point numbers are stored like standard 32 bit floating point but with the least significant bit of the mantissa lopped off.

Generally when an operation acts on integers the result is an integer unless the result is too large for 31 bits - the one exception is the real division operator which always yields a floating point.

Literal Numbers

It is possible to specify an actual numeric value in your program in a number of forms. For an integer the number will be a sequence of alphanumeric 'digits', always starting with a digit. The letters are for use with bases above 10. So 0F#16 will hexadecimal for 15 decimal. Note that the base of the number is preceded by the '#'. The default base is 10 and any other base must be explicitly indicated ( "0F" on its own will generate an error, as will the C style "0x0F" ).

Floating point numbers will contain the decimal point ( always '.' ) or the exponent marker 'e'. So 100.0 and 1e2 are both ways of entering 100.0 as a floating point number.

	n := 4;		[= 4 =]
	n := 04;	[= 4 =] 
	n := 40#16;	[= 64 =]
	n := 1e2;	[= 100.0 =]

Numeric Operators

The numeric operators are the same as in most other languages and the usual rules of precedence ( i.e. BODMAS ) apply. The "a + b * c" will evaluate the multiplication first. The precedence can be over-ridden with brackets - "(a + b ) * c" with do the addition first. See the Language Specification for all the details.

More Obscure Operators

The normal division operator always yields a real number, but there is also an integer division operator '//' that will give the largest whole number at or below the actual result. Thus 10 // 3 will give the result 3.

There is a remainder operator 'Mod' that will give the remainder of the corresponding division: thus 10 Mod 3 will give 1.

Multiplication is represented by the asterisk '*' ( 'x' would be a variable ).

There is also a power operator "a ^ b" is "a to the power of b" e.g. "2 ^ 3" is 8. This operator is right associative: "a ^ b ^ c" evaluates as "a ^ ( b ^ c )".