Commit | Line | Data |
---|---|---|
a0d0e21e LW |
1 | package integer; |
2 | ||
b75c8c73 MS |
3 | our $VERSION = '1.00'; |
4 | ||
f06db76b AD |
5 | =head1 NAME |
6 | ||
34bbe29d | 7 | integer - Perl pragma to use integer arithmetic instead of floating point |
f06db76b AD |
8 | |
9 | =head1 SYNOPSIS | |
10 | ||
11 | use integer; | |
12 | $x = 10/3; | |
13 | # $x is now 3, not 3.33333333333333333 | |
14 | ||
15 | =head1 DESCRIPTION | |
16 | ||
34bbe29d JA |
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. | |
f06db76b | 21 | |
34bbe29d JA |
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 | |
a3cb178b GS |
28 | |
29 | use integer; | |
34bbe29d JA |
30 | $x = 5.8; |
31 | $y = 2.5; | |
32 | $, = ", "; | |
33 | print $x, -$x, $x + $y, $x - $y, $x / $y, $x * $y; | |
34 | ||
35 | will print: 5.8, -5, 7, 3, 2, 10 | |
36 | ||
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;>. | |
40 | E.g., | |
41 | ||
42 | srand(1.5); | |
43 | $, = ", "; | |
44 | print sin(.5), cos(.5), atan2(1,2), sqrt(2), rand(10); | |
45 | ||
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 | |
48 | square root of 2. | |
49 | ||
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, | |
54 | and -2 & -5 is -6. | |
55 | ||
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 | |
60 | may do another. | |
61 | ||
62 | % perl -le 'print (4 % -3)' | |
63 | -2 | |
64 | % perl -Minteger -le 'print (4 % -3)' | |
65 | 1 | |
47f6b1df | 66 | |
f06db76b AD |
67 | See L<perlmod/Pragmatic Modules>. |
68 | ||
69 | =cut | |
70 | ||
d5448623 GS |
71 | $integer::hint_bits = 0x1; |
72 | ||
a0d0e21e | 73 | sub import { |
d5448623 | 74 | $^H |= $integer::hint_bits; |
a0d0e21e LW |
75 | } |
76 | ||
77 | sub unimport { | |
d5448623 | 78 | $^H &= ~$integer::hint_bits; |
a0d0e21e LW |
79 | } |
80 | ||
81 | 1; |