Debian Codegraph @

upstream/mageia-cauldron/man3pm/integer.3pm

raw
1 .\" -*- mode: troff; coding: utf-8 -*-
2 .\" Automatically generated by Pod::Man v6.0.2 (Pod::Simple 3.45)
3 .\"
4 .\" Standard preamble:
5 .\" ========================================================================
6 .de Sp \" Vertical space (when we can't use .PP)
7 .if t .sp .5v
8 .if n .sp
9 ..
10 .de Vb \" Begin verbatim text
11 .ft CW
12 .nf
13 .ne \\$1
14 ..
15 .de Ve \" End verbatim text
16 .ft R
17 .fi
18 ..
19 .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>.
20 .ie n \{\
21 . ds C` ""
22 . ds C' ""
23 'br\}
24 .el\{\
25 . ds C`
26 . ds C'
27 'br\}
28 .\"
29 .\" Escape single quotes in literal strings from groff's Unicode transform.
30 .ie \n(.g .ds Aq \(aq
31 .el .ds Aq '
32 .\"
33 .\" If the F register is >0, we'll generate index entries on stderr for
34 .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
35 .\" entries marked with X<> in POD. Of course, you'll have to process the
36 .\" output yourself in some meaningful fashion.
37 .\"
38 .\" Avoid warning from groff about undefined register 'F'.
39 .de IX
40 ..
41 .nr rF 0
42 .if \n(.g .if rF .nr rF 1
43 .if (\n(rF:(\n(.g==0)) \{\
44 . if \nF \{\
45 . de IX
46 . tm Index:\\$1\t\\n%\t"\\$2"
47 ..
48 . if !\nF==2 \{\
49 . nr % 0
50 . nr F 2
51 . \}
52 . \}
53 .\}
54 .rr rF
55 .\"
56 .\" Required to disable full justification in groff 1.23.0.
57 .if n .ds AD l
58 .\" ========================================================================
59 .\"
60 .IX Title "integer 3pm"
61 .TH integer 3pm 2023-12-22 "perl v5.42.0" "Perl Programmers Reference Guide"
62 .\" For nroff, turn off justification. Always turn off hyphenation; it makes
63 .\" way too many mistakes in technical documents.
64 .if n .ad l
65 .nh
66 .SH NAME
67 integer \- Perl pragma to use integer arithmetic instead of floating point
68 .SH SYNOPSIS
69 .IX Header "SYNOPSIS"
70 .Vb 3
71 \& use integer;
72 \& $x = 10/3;
73 \& # $x is now 3, not 3.33333333333333333
74 .Ve
75 .SH DESCRIPTION
76 .IX Header "DESCRIPTION"
77 This tells the compiler to use integer operations from here to the end
78 of the enclosing BLOCK. On many machines, this doesn\*(Aqt matter a great
79 deal for most computations, but on those without floating point
80 hardware, it can make a big difference in performance.
81 .PP
82 Note that this only affects how most of the arithmetic and relational
83 \&\fBoperators\fR handle their operands and results, and \fBnot\fR how all
84 numbers everywhere are treated. Specifically, \f(CW\*(C`use integer;\*(C'\fR has the
85 effect that before computing the results of the arithmetic operators
86 (+, \-, *, /, %, +=, \-=, *=, /=, %=, and unary minus), the comparison
87 operators (<, <=, >, >=, ==, !=, <=>), and the bitwise operators (|, &,
88 ^, <<, >>, |=, &=, ^=, <<=, >>=), the operands have their fractional
89 portions truncated (or floored), and the result will have its
90 fractional portion truncated as well. In addition, the range of
91 operands and results is restricted to that of familiar two\*(Aqs complement
92 integers, i.e., \-(2**31) .. (2**31\-1) on 32\-bit architectures, and
93 \&\-(2**63) .. (2**63\-1) on 64\-bit architectures. For example, this code
94 .PP
95 .Vb 7
96 \& use integer;
97 \& $x = 5.8;
98 \& $y = 2.5;
99 \& $z = 2.7;
100 \& $a = 2**31 \- 1; # Largest positive integer on 32\-bit machines
101 \& $, = ", ";
102 \& print $x, \-$x, $x+$y, $x\-$y, $x/$y, $x*$y, $y==$z, $a, $a+1;
103 .Ve
104 .PP
105 will print: 5.8, \-5, 7, 3, 2, 10, 1, 2147483647, \-2147483648
106 .PP
107 Note that \f(CW$x\fR is still printed as having its true non\-integer value of
108 5.8 since it wasn\*(Aqt operated on. And note too the wrap\-around from the
109 largest positive integer to the largest negative one. Also, arguments
110 passed to functions and the values returned by them are \fBnot\fR affected
111 by \f(CW\*(C`use integer;\*(C'\fR. E.g.,
112 .PP
113 .Vb 3
114 \& srand(1.5);
115 \& $, = ", ";
116 \& print sin(.5), cos(.5), atan2(1,2), sqrt(2), rand(10);
117 .Ve
118 .PP
119 will give the same result with or without \f(CW\*(C`use integer;\*(C'\fR The power
120 operator \f(CW\*(C`**\*(C'\fR is also not affected, so that 2 ** .5 is always the
121 square root of 2. Now, it so happens that the pre\- and post\- increment
122 and decrement operators, ++ and \-\-, are not affected by \f(CW\*(C`use integer;\*(C'\fR
123 either. Some may rightly consider this to be a bug \-\- but at least it\*(Aqs
124 a long\-standing one.
125 .PP
126 Finally, \f(CW\*(C`use integer;\*(C'\fR also has an additional affect on the bitwise
127 operators. Normally, the operands and results are treated as
128 \&\fBunsigned\fR integers, but with \f(CW\*(C`use integer;\*(C'\fR the operands and results
129 are \fBsigned\fR. This means, among other things, that ~0 is \-1, and \-2 &
130 \&\-5 is \-6.
131 .PP
132 Internally, native integer arithmetic (as provided by your C compiler)
133 is used. This means that Perl\*(Aqs own semantics for arithmetic
134 operations may not be preserved. One common source of trouble is the
135 modulus of negative numbers, which Perl does one way, but your hardware
136 may do another.
137 .PP
138 .Vb 4
139 \& % perl \-le \*(Aqprint (4 % \-3)\*(Aq
140 \& \-2
141 \& % perl \-Minteger \-le \*(Aqprint (4 % \-3)\*(Aq
142 \& 1
143 .Ve
144 .PP
145 See "Pragmatic Modules" in perlmodlib, "Integer Arithmetic" in perlop