Perfect Developer basic tutorial 2 | This page last modified 2011-10-29 (JAC) |
Class int represents all integers (positive, negative and zero). In principle, there are no bounds on the value that can be represented by an int.
Perfect allows non-negative integer literals to be expressed in
decimal format (e.g. 123
),
in binary (e.g. 0b1010
)
or in hexadecimal format (e.g. 0x4ac5
).
In the case of binary and hex formats, the letters may be any
mixture of upper and lower case.
A construct such as -12 is not an integer literal, it is a minus-sign (representing the negation operator) followed by an integer literal.
The methods of class int include the following binary operators:
+ | addition |
- | subtraction |
* | multiplication |
/ | integer division |
% | remainder |
^ | exponentiation |
In each case, the second operand is also of type int and so is the result.
For the integer division and remainder operators, the divisor must be positive. This sort of restriction is called a precondition. We will discuss preconditions further in a later tutorial.
You can look up the preconditions for all the predefined operators of Perfect in the Library Reference page of the Language Reference Manual (look under the entry for the class of which the operator is a member, e.g. int).
The result of integer division is rounded towards minus infinity, and
the result of the remainder operator is always in the range 0 to one less than
the second operand. This means that the expression
(a/b)*b + a%b = a
is always true (remembering that b
must always be positive).
For the exponentiation operator, the second operand must not be negative, and at least one of the operands must be non-zero.
Class int also defines the following unary operators:
- | negation |
< | predecessor, equivalent to subtracting one |
> | successor, equivalent to adding one |
Again, each of these yields another int.
The toString method of class int yields a string of decimal
digits representing the integer. An int can be constructed from a string of
decimal digits with an optional leading minus-sign, for example
int{"123"} = 123
.
Class int is a final class, so your own classes cannot inherit from it.
Knowledge round-up quiz
Next: Enumeration classes
Save My Place | Glossary | Language Reference Manual |
Copyright © 1997-2012 Escher Technologies Limited. All rights reserved. Information is subject to change without notice. |