Integer Arithmetic

Integer Arithmetic

By default, Perl assumes that it must do most of its arithmetic in floating point. But by saying

use integer;

you may tell the compiler to use integer operations (see integer for a detailed explanation) from here to the end of the enclosing BLOCK. An inner BLOCK may countermand this by saying

no integer;

which lasts until the end of that BLOCK. Note that this doesn't mean everything is an integer, merely that Perl will use integer operations for arithmetic, comparison, and bitwise operators. For example, even under use integer , if you take the sqrt(2), you'll still get 1.4142135623731 or so.

Used on numbers, the bitwise operators (& | ^ ~ << >> ) always produce integral results. (But see also Bitwise String Operators.) However, use integer still has meaning for them. By default, their results are interpreted as unsigned integers, but if use integer is in effect, their results are interpreted as signed integers. For example, ~0 usually evaluates to a large integral value. However, use integer; ~0 is -1 on two's-complement machines.

doc_perl
2016-12-06 03:20:25
Comments
Leave a Comment

Please login to continue.