1*0Sstevel@tonic-gatepackage integer; 2*0Sstevel@tonic-gate 3*0Sstevel@tonic-gateour $VERSION = '1.00'; 4*0Sstevel@tonic-gate 5*0Sstevel@tonic-gate=head1 NAME 6*0Sstevel@tonic-gate 7*0Sstevel@tonic-gateinteger - Perl pragma to use integer arithmetic instead of floating point 8*0Sstevel@tonic-gate 9*0Sstevel@tonic-gate=head1 SYNOPSIS 10*0Sstevel@tonic-gate 11*0Sstevel@tonic-gate use integer; 12*0Sstevel@tonic-gate $x = 10/3; 13*0Sstevel@tonic-gate # $x is now 3, not 3.33333333333333333 14*0Sstevel@tonic-gate 15*0Sstevel@tonic-gate=head1 DESCRIPTION 16*0Sstevel@tonic-gate 17*0Sstevel@tonic-gateThis tells the compiler to use integer operations from here to the end 18*0Sstevel@tonic-gateof the enclosing BLOCK. On many machines, this doesn't matter a great 19*0Sstevel@tonic-gatedeal for most computations, but on those without floating point 20*0Sstevel@tonic-gatehardware, it can make a big difference in performance. 21*0Sstevel@tonic-gate 22*0Sstevel@tonic-gateNote that this only affects how most of the arithmetic and relational 23*0Sstevel@tonic-gateB<operators> handle their operands and results, and B<not> how all 24*0Sstevel@tonic-gatenumbers everywhere are treated. Specifically, C<use integer;> has the 25*0Sstevel@tonic-gateeffect that before computing the results of the arithmetic operators 26*0Sstevel@tonic-gate(+, -, *, /, %, +=, -=, *=, /=, %=, and unary minus), the comparison 27*0Sstevel@tonic-gateoperators (<, <=, >, >=, ==, !=, <=>), and the bitwise operators (|, &, 28*0Sstevel@tonic-gate^, <<, >>, |=, &=, ^=, <<=, >>=), the operands have their fractional 29*0Sstevel@tonic-gateportions truncated (or floored), and the result will have its 30*0Sstevel@tonic-gatefractional portion truncated as well. In addition, the range of 31*0Sstevel@tonic-gateoperands and results is restricted to that of familiar two's complement 32*0Sstevel@tonic-gateintegers, i.e., -(2**31) .. (2**31-1) on 32-bit architectures, and 33*0Sstevel@tonic-gate-(2**63) .. (2**63-1) on 64-bit architectures. For example, this code 34*0Sstevel@tonic-gate 35*0Sstevel@tonic-gate use integer; 36*0Sstevel@tonic-gate $x = 5.8; 37*0Sstevel@tonic-gate $y = 2.5; 38*0Sstevel@tonic-gate $z = 2.7; 39*0Sstevel@tonic-gate $a = 2**31 - 1; # Largest positive integer on 32-bit machines 40*0Sstevel@tonic-gate $, = ", "; 41*0Sstevel@tonic-gate print $x, -$x, $x + $y, $x - $y, $x / $y, $x * $y, $y == $z, $a, $a + 1; 42*0Sstevel@tonic-gate 43*0Sstevel@tonic-gatewill print: 5.8, -5, 7, 3, 2, 10, 1, 2147483647, -2147483648 44*0Sstevel@tonic-gate 45*0Sstevel@tonic-gateNote that $x is still printed as having its true non-integer value of 46*0Sstevel@tonic-gate5.8 since it wasn't operated on. And note too the wrap-around from the 47*0Sstevel@tonic-gatelargest positive integer to the largest negative one. Also, arguments 48*0Sstevel@tonic-gatepassed to functions and the values returned by them are B<not> affected 49*0Sstevel@tonic-gateby C<use integer;>. E.g., 50*0Sstevel@tonic-gate 51*0Sstevel@tonic-gate srand(1.5); 52*0Sstevel@tonic-gate $, = ", "; 53*0Sstevel@tonic-gate print sin(.5), cos(.5), atan2(1,2), sqrt(2), rand(10); 54*0Sstevel@tonic-gate 55*0Sstevel@tonic-gatewill give the same result with or without C<use integer;> The power 56*0Sstevel@tonic-gateoperator C<**> is also not affected, so that 2 ** .5 is always the 57*0Sstevel@tonic-gatesquare root of 2. Now, it so happens that the pre- and post- increment 58*0Sstevel@tonic-gateand decrement operators, ++ and --, are not affected by C<use integer;> 59*0Sstevel@tonic-gateeither. Some may rightly consider this to be a bug -- but at least it's 60*0Sstevel@tonic-gatea long-standing one. 61*0Sstevel@tonic-gate 62*0Sstevel@tonic-gateFinally, C<use integer;> also has an additional affect on the bitwise 63*0Sstevel@tonic-gateoperators. Normally, the operands and results are treated as 64*0Sstevel@tonic-gateB<unsigned> integers, but with C<use integer;> the operands and results 65*0Sstevel@tonic-gateare B<signed>. This means, among other things, that ~0 is -1, and -2 & 66*0Sstevel@tonic-gate-5 is -6. 67*0Sstevel@tonic-gate 68*0Sstevel@tonic-gateInternally, native integer arithmetic (as provided by your C compiler) 69*0Sstevel@tonic-gateis used. This means that Perl's own semantics for arithmetic 70*0Sstevel@tonic-gateoperations may not be preserved. One common source of trouble is the 71*0Sstevel@tonic-gatemodulus of negative numbers, which Perl does one way, but your hardware 72*0Sstevel@tonic-gatemay do another. 73*0Sstevel@tonic-gate 74*0Sstevel@tonic-gate % perl -le 'print (4 % -3)' 75*0Sstevel@tonic-gate -2 76*0Sstevel@tonic-gate % perl -Minteger -le 'print (4 % -3)' 77*0Sstevel@tonic-gate 1 78*0Sstevel@tonic-gate 79*0Sstevel@tonic-gateSee L<perlmodlib/"Pragmatic Modules">, L<perlop/"Integer Arithmetic"> 80*0Sstevel@tonic-gate 81*0Sstevel@tonic-gate=cut 82*0Sstevel@tonic-gate 83*0Sstevel@tonic-gate$integer::hint_bits = 0x1; 84*0Sstevel@tonic-gate 85*0Sstevel@tonic-gatesub import { 86*0Sstevel@tonic-gate $^H |= $integer::hint_bits; 87*0Sstevel@tonic-gate} 88*0Sstevel@tonic-gate 89*0Sstevel@tonic-gatesub unimport { 90*0Sstevel@tonic-gate $^H &= ~$integer::hint_bits; 91*0Sstevel@tonic-gate} 92*0Sstevel@tonic-gate 93*0Sstevel@tonic-gate1; 94