integer(3perl)
π integer β the integer pragma (perl, section 3perl) π
okay bestie so this is integer, straight outta the Perl Programmers Reference Guide β a pragma that makes Perl do integer arithmetic instead of floating point. no cap. let's get into it π«Ά
β¨ NAME β¨
integer β Perl pragma to use integer arithmetic instead of floating point π
β¨ SYNOPSIS β¨
use integer;
$x = 10/3;
# $x is now 3, not 3.33333333333333333
literally that's it. you flip the switch and the vibes become whole numbers π
β¨ DESCRIPTION β¨
So what this does is tell the compiler to use integer operations from wherever you wrote it all the way to the end of the enclosing BLOCK. π On a lot of machines this doesn't matter a great deal for most computations, ngl β but on the ones without floating point hardware? it can make a big difference in performance. like, glow-up levels of difference. π₯
β οΈ important caveat bestie: this only affects how most of the arithmetic and relational operators handle their operands and results. it does not mean every number everywhere suddenly becomes an integer. don't get it twisted. π
Specifically, use integer; means that before computing the results of:
- the arithmetic operators β
+,-,*,/,%,+=,-=,*=,/=,%=, and unary minus π€ - the comparison operators β
<,<=,>,>=,==,!=,<=>π - the bitwise operators β
|,&,^,<<,>>,|=,&=,^=,<<=,>>=β¨
β¦the operands have their fractional portions truncated (or floored), and the result will have its fractional portion truncated as well. slay.
And on top of that, the range of operands and results gets restricted to that of familiar two's complement integers, i.e.:
-(2**31) .. (2**31-1)on 32-bit architectures-(2**63) .. (2**63-1)on 64-bit architectures
periodt. so like, for example, this code:
use integer;
$x = 5.8;
$y = 2.5;
$z = 2.7;
$a = 2**31 - 1; # Largest positive integer on 32-bit machines
$, = ", ";
print $x, -$x, $x+$y, $x-$y, $x/$y, $x*$y, $y==$z, $a, $a+1;
will print:
5.8, -5, 7, 3, 2, 10, 1, 2147483647, -2147483648
okay so notice how $x is still printed as having its true non-integer value of 5.8 β that's because it wasn't operated on. it just sat there being its floaty self. π
and also! notice the wrap-around from the largest positive integer to the largest negative one. iconic behavior honestly. π
Also β arguments passed to functions and the values returned by them are not affected by use integer;. like, for example:
srand(1.5);
$, = ", ";
print sin(.5), cos(.5), atan2(1,2), sqrt(2), rand(10);
will give the same result with or without use integer; π§
The power operator ** is also not affected, so that 2 ** .5 is always the square root of 2, always, no matter what. β
And now for the plot twist: it so happens that the pre- and post- increment and decrement operators, ++ and --, are not affected by use integer; either. some may rightly consider this to be a bug β but at least it's a long-standing one. π a legacy bug is still a legacy, bestie.
Finally, use integer; also has an additional affect on the bitwise operators. normally the operands and results are treated as unsigned integers, but with use integer; the operands and results are signed. this means, among other things, that ~0 is -1, and -2 & -5 is -6. yes, really. π
π¦ the internals (iykyk) π¦
Internally, native integer arithmetic (as provided by your C compiler) is used. which means Perl's own semantics for arithmetic operations may not be preserved. π
One common source of trouble is the modulus of negative numbers, which Perl does one way, but your hardware may do another. so you get situations like:
% perl -le 'print (4 % -3)'
-2
% perl -Minteger -le 'print (4 % -3)'
1
same expression, different answers. vibe check failed. π
β¨ SEE ALSO β¨
"Pragmatic Modules" in perlmodlibπ"Integer Arithmetic" in perlopπ
and that's the whole page!! π written for perl v5.44.0, dated 2026-10-17, and this is the integer(3perl) page from the Perl Programmers Reference Guide. go forth and truncate those fractional portions, bestie π«Άβ¨πΈ
π¦ where this page comes from
- arch rolling: perl 5.44.0-1 Β· Artistic-1.0-Perl AND GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms