7 integer - Perl pragma to use integer arithmetic instead of floating point
13 # $x is now 3, not 3.33333333333333333
17 This tells the compiler to use integer operations from here to the end
18 of the enclosing BLOCK. On many machines, this doesn't matter a great
19 deal for most computations, but on those without floating point
20 hardware, it can make a big difference in performance.
22 Note that this only affects how certain operators handle their operands
23 and results, and not all numbers everywhere. Specifically, C<use
24 integer;> has the effect that before computing the result of X + Y, X -
25 Y, X / Y, X * Y, X % Y, or -X (unary minus), the operands X and Y have
26 their fractional portions truncated, and the result will have its
27 fractional portion truncated as well. For example, this code
33 print $x, -$x, $x + $y, $x - $y, $x / $y, $x * $y;
35 will print: 5.8, -5, 7, 3, 2, 10
37 Note that $x is still printed as having its true non-integer value of
38 5.8 since it wasn't operated on. Also, arguments passed to functions
39 and the values returned by them are not affected by C<use integer;>.
44 print sin(.5), cos(.5), atan2(1,2), sqrt(2), rand(10);
46 will give the same result with or without C<use integer;> The power
47 operator C<**> is also not affected, so that 2 ** .5 is always the
50 Finally, C<use integer;> also has an affect on the bitwise operators
51 "&", "|", "^", "~", "<<", and ">>". Normally, the operands and results
52 are treated as unsigned integers, but with C<use integer;> the operands
53 and results are signed. This means, among other things, that ~0 is -1,
56 Internally, native integer arithmetic (as provided by your C compiler)
57 is used. This means that Perl's own semantics for arithmetic
58 operations may not be preserved. One common source of trouble is the
59 modulus of negative numbers, which Perl does one way, but your hardware
62 % perl -le 'print (4 % -3)'
64 % perl -Minteger -le 'print (4 % -3)'
67 See L<perlmod/Pragmatic Modules>.
71 $integer::hint_bits = 0x1;
74 $^H |= $integer::hint_bits;
78 $^H &= ~$integer::hint_bits;