| 1 | .\" Automatically generated by Pod::Man v1.34, Pod::Parser v1.13 |
| 2 | .\" |
| 3 | .\" Standard preamble: |
| 4 | .\" ======================================================================== |
| 5 | .de Sh \" Subsection heading |
| 6 | .br |
| 7 | .if t .Sp |
| 8 | .ne 5 |
| 9 | .PP |
| 10 | \fB\\$1\fR |
| 11 | .PP |
| 12 | .. |
| 13 | .de Sp \" Vertical space (when we can't use .PP) |
| 14 | .if t .sp .5v |
| 15 | .if n .sp |
| 16 | .. |
| 17 | .de Vb \" Begin verbatim text |
| 18 | .ft CW |
| 19 | .nf |
| 20 | .ne \\$1 |
| 21 | .. |
| 22 | .de Ve \" End verbatim text |
| 23 | .ft R |
| 24 | .fi |
| 25 | .. |
| 26 | .\" Set up some character translations and predefined strings. \*(-- will |
| 27 | .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left |
| 28 | .\" double quote, and \*(R" will give a right double quote. | will give a |
| 29 | .\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used to |
| 30 | .\" do unbreakable dashes and therefore won't be available. \*(C` and \*(C' |
| 31 | .\" expand to `' in nroff, nothing in troff, for use with C<>. |
| 32 | .tr \(*W-|\(bv\*(Tr |
| 33 | .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' |
| 34 | .ie n \{\ |
| 35 | . ds -- \(*W- |
| 36 | . ds PI pi |
| 37 | . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch |
| 38 | . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch |
| 39 | . ds L" "" |
| 40 | . ds R" "" |
| 41 | . ds C` "" |
| 42 | . ds C' "" |
| 43 | 'br\} |
| 44 | .el\{\ |
| 45 | . ds -- \|\(em\| |
| 46 | . ds PI \(*p |
| 47 | . ds L" `` |
| 48 | . ds R" '' |
| 49 | 'br\} |
| 50 | .\" |
| 51 | .\" If the F register is turned on, we'll generate index entries on stderr for |
| 52 | .\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index |
| 53 | .\" entries marked with X<> in POD. Of course, you'll have to process the |
| 54 | .\" output yourself in some meaningful fashion. |
| 55 | .if \nF \{\ |
| 56 | . de IX |
| 57 | . tm Index:\\$1\t\\n%\t"\\$2" |
| 58 | .. |
| 59 | . nr % 0 |
| 60 | . rr F |
| 61 | .\} |
| 62 | .\" |
| 63 | .\" For nroff, turn off justification. Always turn off hyphenation; it makes |
| 64 | .\" way too many mistakes in technical documents. |
| 65 | .hy 0 |
| 66 | .if n .na |
| 67 | .\" |
| 68 | .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). |
| 69 | .\" Fear. Run. Save yourself. No user-serviceable parts. |
| 70 | . \" fudge factors for nroff and troff |
| 71 | .if n \{\ |
| 72 | . ds #H 0 |
| 73 | . ds #V .8m |
| 74 | . ds #F .3m |
| 75 | . ds #[ \f1 |
| 76 | . ds #] \fP |
| 77 | .\} |
| 78 | .if t \{\ |
| 79 | . ds #H ((1u-(\\\\n(.fu%2u))*.13m) |
| 80 | . ds #V .6m |
| 81 | . ds #F 0 |
| 82 | . ds #[ \& |
| 83 | . ds #] \& |
| 84 | .\} |
| 85 | . \" simple accents for nroff and troff |
| 86 | .if n \{\ |
| 87 | . ds ' \& |
| 88 | . ds ` \& |
| 89 | . ds ^ \& |
| 90 | . ds , \& |
| 91 | . ds ~ ~ |
| 92 | . ds / |
| 93 | .\} |
| 94 | .if t \{\ |
| 95 | . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" |
| 96 | . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' |
| 97 | . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' |
| 98 | . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' |
| 99 | . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' |
| 100 | . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' |
| 101 | .\} |
| 102 | . \" troff and (daisy-wheel) nroff accents |
| 103 | .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' |
| 104 | .ds 8 \h'\*(#H'\(*b\h'-\*(#H' |
| 105 | .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] |
| 106 | .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' |
| 107 | .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' |
| 108 | .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] |
| 109 | .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] |
| 110 | .ds ae a\h'-(\w'a'u*4/10)'e |
| 111 | .ds Ae A\h'-(\w'A'u*4/10)'E |
| 112 | . \" corrections for vroff |
| 113 | .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' |
| 114 | .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' |
| 115 | . \" for low resolution devices (crt and lpr) |
| 116 | .if \n(.H>23 .if \n(.V>19 \ |
| 117 | \{\ |
| 118 | . ds : e |
| 119 | . ds 8 ss |
| 120 | . ds o a |
| 121 | . ds d- d\h'-1'\(ga |
| 122 | . ds D- D\h'-1'\(hy |
| 123 | . ds th \o'bp' |
| 124 | . ds Th \o'LP' |
| 125 | . ds ae ae |
| 126 | . ds Ae AE |
| 127 | .\} |
| 128 | .rm #[ #] #H #V #F C |
| 129 | .\" ======================================================================== |
| 130 | .\" |
| 131 | .IX Title "Math::BigInt 3" |
| 132 | .TH Math::BigInt 3 "2002-06-01" "perl v5.8.0" "Perl Programmers Reference Guide" |
| 133 | .SH "NAME" |
| 134 | Math::BigInt \- Arbitrary size integer math package |
| 135 | .SH "SYNOPSIS" |
| 136 | .IX Header "SYNOPSIS" |
| 137 | .Vb 1 |
| 138 | \& use Math::BigInt; |
| 139 | .Ve |
| 140 | .PP |
| 141 | .Vb 8 |
| 142 | \& # Number creation |
| 143 | \& $x = Math::BigInt->new($str); # defaults to 0 |
| 144 | \& $nan = Math::BigInt->bnan(); # create a NotANumber |
| 145 | \& $zero = Math::BigInt->bzero(); # create a +0 |
| 146 | \& $inf = Math::BigInt->binf(); # create a +inf |
| 147 | \& $inf = Math::BigInt->binf('-'); # create a -inf |
| 148 | \& $one = Math::BigInt->bone(); # create a +1 |
| 149 | \& $one = Math::BigInt->bone('-'); # create a -1 |
| 150 | .Ve |
| 151 | .PP |
| 152 | .Vb 11 |
| 153 | \& # Testing |
| 154 | \& $x->is_zero(); # true if arg is +0 |
| 155 | \& $x->is_nan(); # true if arg is NaN |
| 156 | \& $x->is_one(); # true if arg is +1 |
| 157 | \& $x->is_one('-'); # true if arg is -1 |
| 158 | \& $x->is_odd(); # true if odd, false for even |
| 159 | \& $x->is_even(); # true if even, false for odd |
| 160 | \& $x->is_positive(); # true if >= 0 |
| 161 | \& $x->is_negative(); # true if < 0 |
| 162 | \& $x->is_inf(sign); # true if +inf, or -inf (sign is default '+') |
| 163 | \& $x->is_int(); # true if $x is an integer (not a float) |
| 164 | .Ve |
| 165 | .PP |
| 166 | .Vb 5 |
| 167 | \& $x->bcmp($y); # compare numbers (undef,<0,=0,>0) |
| 168 | \& $x->bacmp($y); # compare absolutely (undef,<0,=0,>0) |
| 169 | \& $x->sign(); # return the sign, either +,- or NaN |
| 170 | \& $x->digit($n); # return the nth digit, counting from right |
| 171 | \& $x->digit(-$n); # return the nth digit, counting from left |
| 172 | .Ve |
| 173 | .PP |
| 174 | .Vb 1 |
| 175 | \& # The following all modify their first argument: |
| 176 | .Ve |
| 177 | .PP |
| 178 | .Vb 7 |
| 179 | \& # set |
| 180 | \& $x->bzero(); # set $x to 0 |
| 181 | \& $x->bnan(); # set $x to NaN |
| 182 | \& $x->bone(); # set $x to +1 |
| 183 | \& $x->bone('-'); # set $x to -1 |
| 184 | \& $x->binf(); # set $x to inf |
| 185 | \& $x->binf('-'); # set $x to -inf |
| 186 | .Ve |
| 187 | .PP |
| 188 | .Vb 6 |
| 189 | \& $x->bneg(); # negation |
| 190 | \& $x->babs(); # absolute value |
| 191 | \& $x->bnorm(); # normalize (no-op) |
| 192 | \& $x->bnot(); # two's complement (bit wise not) |
| 193 | \& $x->binc(); # increment x by 1 |
| 194 | \& $x->bdec(); # decrement x by 1 |
| 195 | .Ve |
| 196 | .PP |
| 197 | .Vb 5 |
| 198 | \& $x->badd($y); # addition (add $y to $x) |
| 199 | \& $x->bsub($y); # subtraction (subtract $y from $x) |
| 200 | \& $x->bmul($y); # multiplication (multiply $x by $y) |
| 201 | \& $x->bdiv($y); # divide, set $x to quotient |
| 202 | \& # return (quo,rem) or quo if scalar |
| 203 | .Ve |
| 204 | .PP |
| 205 | .Vb 3 |
| 206 | \& $x->bmod($y); # modulus (x % y) |
| 207 | \& $x->bmodpow($exp,$mod); # modular exponentation (($num**$exp) % $mod)) |
| 208 | \& $x->bmodinv($mod); # the inverse of $x in the given modulus $mod |
| 209 | .Ve |
| 210 | .PP |
| 211 | .Vb 5 |
| 212 | \& $x->bpow($y); # power of arguments (x ** y) |
| 213 | \& $x->blsft($y); # left shift |
| 214 | \& $x->brsft($y); # right shift |
| 215 | \& $x->blsft($y,$n); # left shift, by base $n (like 10) |
| 216 | \& $x->brsft($y,$n); # right shift, by base $n (like 10) |
| 217 | .Ve |
| 218 | .PP |
| 219 | .Vb 4 |
| 220 | \& $x->band($y); # bitwise and |
| 221 | \& $x->bior($y); # bitwise inclusive or |
| 222 | \& $x->bxor($y); # bitwise exclusive or |
| 223 | \& $x->bnot(); # bitwise not (two's complement) |
| 224 | .Ve |
| 225 | .PP |
| 226 | .Vb 2 |
| 227 | \& $x->bsqrt(); # calculate square-root |
| 228 | \& $x->bfac(); # factorial of $x (1*2*3*4*..$x) |
| 229 | .Ve |
| 230 | .PP |
| 231 | .Vb 3 |
| 232 | \& $x->round($A,$P,$round_mode); # round to accuracy or precision using mode $r |
| 233 | \& $x->bround($N); # accuracy: preserve $N digits |
| 234 | \& $x->bfround($N); # round to $Nth digit, no-op for BigInts |
| 235 | .Ve |
| 236 | .PP |
| 237 | .Vb 3 |
| 238 | \& # The following do not modify their arguments in BigInt, but do in BigFloat: |
| 239 | \& $x->bfloor(); # return integer less or equal than $x |
| 240 | \& $x->bceil(); # return integer greater or equal than $x |
| 241 | .Ve |
| 242 | .PP |
| 243 | .Vb 1 |
| 244 | \& # The following do not modify their arguments: |
| 245 | .Ve |
| 246 | .PP |
| 247 | .Vb 2 |
| 248 | \& bgcd(@values); # greatest common divisor (no OO style) |
| 249 | \& blcm(@values); # lowest common multiplicator (no OO style) |
| 250 | .Ve |
| 251 | .PP |
| 252 | .Vb 3 |
| 253 | \& $x->length(); # return number of digits in number |
| 254 | \& ($x,$f) = $x->length(); # length of number and length of fraction part, |
| 255 | \& # latter is always 0 digits long for BigInt's |
| 256 | .Ve |
| 257 | .PP |
| 258 | .Vb 5 |
| 259 | \& $x->exponent(); # return exponent as BigInt |
| 260 | \& $x->mantissa(); # return (signed) mantissa as BigInt |
| 261 | \& $x->parts(); # return (mantissa,exponent) as BigInt |
| 262 | \& $x->copy(); # make a true copy of $x (unlike $y = $x;) |
| 263 | \& $x->as_number(); # return as BigInt (in BigInt: same as copy()) |
| 264 | .Ve |
| 265 | .PP |
| 266 | .Vb 5 |
| 267 | \& # conversation to string |
| 268 | \& $x->bstr(); # normalized string |
| 269 | \& $x->bsstr(); # normalized string in scientific notation |
| 270 | \& $x->as_hex(); # as signed hexadecimal string with prefixed 0x |
| 271 | \& $x->as_bin(); # as signed binary string with prefixed 0b |
| 272 | .Ve |
| 273 | .PP |
| 274 | .Vb 1 |
| 275 | \& Math::BigInt->config(); # return hash containing configuration/version |
| 276 | .Ve |
| 277 | .PP |
| 278 | .Vb 5 |
| 279 | \& # precision and accuracy (see section about rounding for more) |
| 280 | \& $x->precision(); # return P of $x (or global, if P of $x undef) |
| 281 | \& $x->precision($n); # set P of $x to $n |
| 282 | \& $x->accuracy(); # return A of $x (or global, if A of $x undef) |
| 283 | \& $x->accuracy($n); # set A $x to $n |
| 284 | .Ve |
| 285 | .PP |
| 286 | .Vb 2 |
| 287 | \& Math::BigInt->precision(); # get/set global P for all BigInt objects |
| 288 | \& Math::BigInt->accuracy(); # get/set global A for all BigInt objects |
| 289 | .Ve |
| 290 | .SH "DESCRIPTION" |
| 291 | .IX Header "DESCRIPTION" |
| 292 | All operators (inlcuding basic math operations) are overloaded if you |
| 293 | declare your big integers as |
| 294 | .PP |
| 295 | .Vb 1 |
| 296 | \& $i = new Math::BigInt '123_456_789_123_456_789'; |
| 297 | .Ve |
| 298 | .PP |
| 299 | Operations with overloaded operators preserve the arguments which is |
| 300 | exactly what you expect. |
| 301 | .IP "Canonical notation" 2 |
| 302 | .IX Item "Canonical notation" |
| 303 | Big integer values are strings of the form \f(CW\*(C`/^[+\-]\ed+$/\*(C'\fR with leading |
| 304 | zeros suppressed. |
| 305 | .Sp |
| 306 | .Vb 3 |
| 307 | \& '-0' canonical value '-0', normalized '0' |
| 308 | \& ' -123_123_123' canonical value '-123123123' |
| 309 | \& '1_23_456_7890' canonical value '1234567890' |
| 310 | .Ve |
| 311 | .IP "Input" 2 |
| 312 | .IX Item "Input" |
| 313 | Input values to these routines may be either Math::BigInt objects or |
| 314 | strings of the form \f(CW\*(C`/^[+\-]?[\ed]+\e.?[\ed]*E?[+\-]?[\ed]*$/\*(C'\fR. |
| 315 | .Sp |
| 316 | You can include one underscore between any two digits. The input string may |
| 317 | have leading and trailing whitespace, which will be ignored. In later |
| 318 | versions, a more strict (no whitespace at all) or more lax (whitespace |
| 319 | allowed everywhere) input checking will also be possible. |
| 320 | .Sp |
| 321 | This means integer values like 1.01E2 or even 1000E\-2 are also accepted. |
| 322 | Non integer values result in NaN. |
| 323 | .Sp |
| 324 | \&\fIMath::BigInt::new()\fR defaults to 0, while Math::BigInt::new('') results |
| 325 | in 'NaN'. |
| 326 | .Sp |
| 327 | \&\fIbnorm()\fR on a BigInt object is now effectively a no\-op, since the numbers |
| 328 | are always stored in normalized form. On a string, it creates a BigInt |
| 329 | object. |
| 330 | .IP "Output" 2 |
| 331 | .IX Item "Output" |
| 332 | Output values are BigInt objects (normalized), except for \fIbstr()\fR, which |
| 333 | returns a string in normalized form. |
| 334 | Some routines (\f(CW\*(C`is_odd()\*(C'\fR, \f(CW\*(C`is_even()\*(C'\fR, \f(CW\*(C`is_zero()\*(C'\fR, \f(CW\*(C`is_one()\*(C'\fR, |
| 335 | \&\f(CW\*(C`is_nan()\*(C'\fR) return true or false, while others (\f(CW\*(C`bcmp()\*(C'\fR, \f(CW\*(C`bacmp()\*(C'\fR) |
| 336 | return either undef, <0, 0 or >0 and are suited for sort. |
| 337 | .SH "METHODS" |
| 338 | .IX Header "METHODS" |
| 339 | Each of the methods below accepts three additional parameters. These arguments |
| 340 | \&\f(CW$A\fR, \f(CW$P\fR and \f(CW$R\fR are accuracy, precision and round_mode. Please see more in the |
| 341 | section about \s-1ACCURACY\s0 and \s-1ROUNDIND\s0. |
| 342 | .Sh "config" |
| 343 | .IX Subsection "config" |
| 344 | .Vb 1 |
| 345 | \& use Data::Dumper; |
| 346 | .Ve |
| 347 | .PP |
| 348 | .Vb 1 |
| 349 | \& print Dumper ( Math::BigInt->config() ); |
| 350 | .Ve |
| 351 | .PP |
| 352 | Returns a hash containing the configuration, e.g. the version number, lib |
| 353 | loaded etc. |
| 354 | .Sh "accuracy" |
| 355 | .IX Subsection "accuracy" |
| 356 | .Vb 2 |
| 357 | \& $x->accuracy(5); # local for $x |
| 358 | \& $class->accuracy(5); # global for all members of $class |
| 359 | .Ve |
| 360 | .PP |
| 361 | Set or get the global or local accuracy, aka how many significant digits the |
| 362 | results have. Please see the section about \*(L"\s-1ACCURACY\s0 \s-1AND\s0 \s-1PRECISION\s0\*(R" for |
| 363 | further details. |
| 364 | .PP |
| 365 | Value must be greater than zero. Pass an undef value to disable it: |
| 366 | .PP |
| 367 | .Vb 2 |
| 368 | \& $x->accuracy(undef); |
| 369 | \& Math::BigInt->accuracy(undef); |
| 370 | .Ve |
| 371 | .PP |
| 372 | Returns the current accuracy. For \f(CW\*(C`$x\-\*(C'\fR\fIaccuracy()\fR> it will return either the |
| 373 | local accuracy, or if not defined, the global. This means the return value |
| 374 | represents the accuracy that will be in effect for \f(CW$x:\fR |
| 375 | .PP |
| 376 | .Vb 9 |
| 377 | \& $y = Math::BigInt->new(1234567); # unrounded |
| 378 | \& print Math::BigInt->accuracy(4),"\en"; # set 4, print 4 |
| 379 | \& $x = Math::BigInt->new(123456); # will be automatically rounded |
| 380 | \& print "$x $y\en"; # '123500 1234567' |
| 381 | \& print $x->accuracy(),"\en"; # will be 4 |
| 382 | \& print $y->accuracy(),"\en"; # also 4, since global is 4 |
| 383 | \& print Math::BigInt->accuracy(5),"\en"; # set to 5, print 5 |
| 384 | \& print $x->accuracy(),"\en"; # still 4 |
| 385 | \& print $y->accuracy(),"\en"; # 5, since global is 5 |
| 386 | .Ve |
| 387 | .Sh "brsft" |
| 388 | .IX Subsection "brsft" |
| 389 | .Vb 1 |
| 390 | \& $x->brsft($y,$n); |
| 391 | .Ve |
| 392 | .PP |
| 393 | Shifts \f(CW$x\fR right by \f(CW$y\fR in base \f(CW$n\fR. Default is base 2, used are usually 10 and |
| 394 | 2, but others work, too. |
| 395 | .PP |
| 396 | Right shifting usually amounts to dividing \f(CW$x\fR by \f(CW$n\fR ** \f(CW$y\fR and truncating the |
| 397 | result: |
| 398 | .PP |
| 399 | .Vb 4 |
| 400 | \& $x = Math::BigInt->new(10); |
| 401 | \& $x->brsft(1); # same as $x >> 1: 5 |
| 402 | \& $x = Math::BigInt->new(1234); |
| 403 | \& $x->brsft(2,10); # result 12 |
| 404 | .Ve |
| 405 | .PP |
| 406 | There is one exception, and that is base 2 with negative \f(CW$x:\fR |
| 407 | .PP |
| 408 | .Vb 2 |
| 409 | \& $x = Math::BigInt->new(-5); |
| 410 | \& print $x->brsft(1); |
| 411 | .Ve |
| 412 | .PP |
| 413 | This will print \-3, not \-2 (as it would if you divide \-5 by 2 and truncate the |
| 414 | result). |
| 415 | .Sh "new" |
| 416 | .IX Subsection "new" |
| 417 | .Vb 1 |
| 418 | \& $x = Math::BigInt->new($str,$A,$P,$R); |
| 419 | .Ve |
| 420 | .PP |
| 421 | Creates a new BigInt object from a string or another BigInt object. The |
| 422 | input is accepted as decimal, hex (with leading '0x') or binary (with leading |
| 423 | \&'0b'). |
| 424 | .Sh "bnan" |
| 425 | .IX Subsection "bnan" |
| 426 | .Vb 1 |
| 427 | \& $x = Math::BigInt->bnan(); |
| 428 | .Ve |
| 429 | .PP |
| 430 | Creates a new BigInt object representing NaN (Not A Number). |
| 431 | If used on an object, it will set it to NaN: |
| 432 | .PP |
| 433 | .Vb 1 |
| 434 | \& $x->bnan(); |
| 435 | .Ve |
| 436 | .Sh "bzero" |
| 437 | .IX Subsection "bzero" |
| 438 | .Vb 1 |
| 439 | \& $x = Math::BigInt->bzero(); |
| 440 | .Ve |
| 441 | .PP |
| 442 | Creates a new BigInt object representing zero. |
| 443 | If used on an object, it will set it to zero: |
| 444 | .PP |
| 445 | .Vb 1 |
| 446 | \& $x->bzero(); |
| 447 | .Ve |
| 448 | .Sh "binf" |
| 449 | .IX Subsection "binf" |
| 450 | .Vb 1 |
| 451 | \& $x = Math::BigInt->binf($sign); |
| 452 | .Ve |
| 453 | .PP |
| 454 | Creates a new BigInt object representing infinity. The optional argument is |
| 455 | either '\-' or '+', indicating whether you want infinity or minus infinity. |
| 456 | If used on an object, it will set it to infinity: |
| 457 | .PP |
| 458 | .Vb 2 |
| 459 | \& $x->binf(); |
| 460 | \& $x->binf('-'); |
| 461 | .Ve |
| 462 | .Sh "bone" |
| 463 | .IX Subsection "bone" |
| 464 | .Vb 1 |
| 465 | \& $x = Math::BigInt->binf($sign); |
| 466 | .Ve |
| 467 | .PP |
| 468 | Creates a new BigInt object representing one. The optional argument is |
| 469 | either '\-' or '+', indicating whether you want one or minus one. |
| 470 | If used on an object, it will set it to one: |
| 471 | .PP |
| 472 | .Vb 2 |
| 473 | \& $x->bone(); # +1 |
| 474 | \& $x->bone('-'); # -1 |
| 475 | .Ve |
| 476 | .Sh "\fIis_one()\fP/\fIis_zero()\fP/\fIis_nan()\fP/\fIis_inf()\fP" |
| 477 | .IX Subsection "is_one()/is_zero()/is_nan()/is_inf()" |
| 478 | .Vb 6 |
| 479 | \& $x->is_zero(); # true if arg is +0 |
| 480 | \& $x->is_nan(); # true if arg is NaN |
| 481 | \& $x->is_one(); # true if arg is +1 |
| 482 | \& $x->is_one('-'); # true if arg is -1 |
| 483 | \& $x->is_inf(); # true if +inf |
| 484 | \& $x->is_inf('-'); # true if -inf (sign is default '+') |
| 485 | .Ve |
| 486 | .PP |
| 487 | These methods all test the BigInt for beeing one specific value and return |
| 488 | true or false depending on the input. These are faster than doing something |
| 489 | like: |
| 490 | .PP |
| 491 | .Vb 1 |
| 492 | \& if ($x == 0) |
| 493 | .Ve |
| 494 | .Sh "\fIis_positive()\fP/\fIis_negative()\fP" |
| 495 | .IX Subsection "is_positive()/is_negative()" |
| 496 | .Vb 2 |
| 497 | \& $x->is_positive(); # true if >= 0 |
| 498 | \& $x->is_negative(); # true if < 0 |
| 499 | .Ve |
| 500 | .PP |
| 501 | The methods return true if the argument is positive or negative, respectively. |
| 502 | \&\f(CW\*(C`NaN\*(C'\fR is neither positive nor negative, while \f(CW\*(C`+inf\*(C'\fR counts as positive, and |
| 503 | \&\f(CW\*(C`\-inf\*(C'\fR is negative. A \f(CW\*(C`zero\*(C'\fR is positive. |
| 504 | .PP |
| 505 | These methods are only testing the sign, and not the value. |
| 506 | .Sh "\fIis_odd()\fP/\fIis_even()\fP/\fIis_int()\fP" |
| 507 | .IX Subsection "is_odd()/is_even()/is_int()" |
| 508 | .Vb 3 |
| 509 | \& $x->is_odd(); # true if odd, false for even |
| 510 | \& $x->is_even(); # true if even, false for odd |
| 511 | \& $x->is_int(); # true if $x is an integer |
| 512 | .Ve |
| 513 | .PP |
| 514 | The return true when the argument satisfies the condition. \f(CW\*(C`NaN\*(C'\fR, \f(CW\*(C`+inf\*(C'\fR, |
| 515 | \&\f(CW\*(C`\-inf\*(C'\fR are not integers and are neither odd nor even. |
| 516 | .Sh "bcmp" |
| 517 | .IX Subsection "bcmp" |
| 518 | .Vb 1 |
| 519 | \& $x->bcmp($y); |
| 520 | .Ve |
| 521 | .PP |
| 522 | Compares \f(CW$x\fR with \f(CW$y\fR and takes the sign into account. |
| 523 | Returns \-1, 0, 1 or undef. |
| 524 | .Sh "bacmp" |
| 525 | .IX Subsection "bacmp" |
| 526 | .Vb 1 |
| 527 | \& $x->bacmp($y); |
| 528 | .Ve |
| 529 | .PP |
| 530 | Compares \f(CW$x\fR with \f(CW$y\fR while ignoring their. Returns \-1, 0, 1 or undef. |
| 531 | .Sh "sign" |
| 532 | .IX Subsection "sign" |
| 533 | .Vb 1 |
| 534 | \& $x->sign(); |
| 535 | .Ve |
| 536 | .PP |
| 537 | Return the sign, of \f(CW$x\fR, meaning either \f(CW\*(C`+\*(C'\fR, \f(CW\*(C`\-\*(C'\fR, \f(CW\*(C`\-inf\*(C'\fR, \f(CW\*(C`+inf\*(C'\fR or NaN. |
| 538 | .Sh "bcmp" |
| 539 | .IX Subsection "bcmp" |
| 540 | .Vb 1 |
| 541 | \& $x->digit($n); # return the nth digit, counting from right |
| 542 | .Ve |
| 543 | .Sh "bneg" |
| 544 | .IX Subsection "bneg" |
| 545 | .Vb 1 |
| 546 | \& $x->bneg(); |
| 547 | .Ve |
| 548 | .PP |
| 549 | Negate the number, e.g. change the sign between '+' and '\-', or between '+inf' |
| 550 | and '\-inf', respectively. Does nothing for NaN or zero. |
| 551 | .Sh "babs" |
| 552 | .IX Subsection "babs" |
| 553 | .Vb 1 |
| 554 | \& $x->babs(); |
| 555 | .Ve |
| 556 | .PP |
| 557 | Set the number to it's absolute value, e.g. change the sign from '\-' to '+' |
| 558 | and from '\-inf' to '+inf', respectively. Does nothing for NaN or positive |
| 559 | numbers. |
| 560 | .Sh "bnorm" |
| 561 | .IX Subsection "bnorm" |
| 562 | .Vb 1 |
| 563 | \& $x->bnorm(); # normalize (no-op) |
| 564 | .Ve |
| 565 | .Sh "bnot" |
| 566 | .IX Subsection "bnot" |
| 567 | .Vb 1 |
| 568 | \& $x->bnot(); # two's complement (bit wise not) |
| 569 | .Ve |
| 570 | .Sh "binc" |
| 571 | .IX Subsection "binc" |
| 572 | .Vb 1 |
| 573 | \& $x->binc(); # increment x by 1 |
| 574 | .Ve |
| 575 | .Sh "bdec" |
| 576 | .IX Subsection "bdec" |
| 577 | .Vb 1 |
| 578 | \& $x->bdec(); # decrement x by 1 |
| 579 | .Ve |
| 580 | .Sh "badd" |
| 581 | .IX Subsection "badd" |
| 582 | .Vb 1 |
| 583 | \& $x->badd($y); # addition (add $y to $x) |
| 584 | .Ve |
| 585 | .Sh "bsub" |
| 586 | .IX Subsection "bsub" |
| 587 | .Vb 1 |
| 588 | \& $x->bsub($y); # subtraction (subtract $y from $x) |
| 589 | .Ve |
| 590 | .Sh "bmul" |
| 591 | .IX Subsection "bmul" |
| 592 | .Vb 1 |
| 593 | \& $x->bmul($y); # multiplication (multiply $x by $y) |
| 594 | .Ve |
| 595 | .Sh "bdiv" |
| 596 | .IX Subsection "bdiv" |
| 597 | .Vb 2 |
| 598 | \& $x->bdiv($y); # divide, set $x to quotient |
| 599 | \& # return (quo,rem) or quo if scalar |
| 600 | .Ve |
| 601 | .Sh "bmod" |
| 602 | .IX Subsection "bmod" |
| 603 | .Vb 1 |
| 604 | \& $x->bmod($y); # modulus (x % y) |
| 605 | .Ve |
| 606 | .Sh "bmodinv" |
| 607 | .IX Subsection "bmodinv" |
| 608 | .Vb 1 |
| 609 | \& $num->bmodinv($mod); # modular inverse |
| 610 | .Ve |
| 611 | .PP |
| 612 | Returns the inverse of \f(CW$num\fR in the given modulus \f(CW$mod\fR. '\f(CW\*(C`NaN\*(C'\fR' is |
| 613 | returned unless \f(CW$num\fR is relatively prime to \f(CW$mod\fR, i.e. unless |
| 614 | \&\f(CW\*(C`bgcd($num, $mod)==1\*(C'\fR. |
| 615 | .Sh "bmodpow" |
| 616 | .IX Subsection "bmodpow" |
| 617 | .Vb 1 |
| 618 | \& $num->bmodpow($exp,$mod); # modular exponentation ($num**$exp % $mod) |
| 619 | .Ve |
| 620 | .PP |
| 621 | Returns the value of \f(CW$num\fR taken to the power \f(CW$exp\fR in the modulus |
| 622 | \&\f(CW$mod\fR using binary exponentation. \f(CW\*(C`bmodpow\*(C'\fR is far superior to |
| 623 | writing |
| 624 | .PP |
| 625 | .Vb 1 |
| 626 | \& $num ** $exp % $mod |
| 627 | .Ve |
| 628 | .PP |
| 629 | because \f(CW\*(C`bmodpow\*(C'\fR is much faster\*(--it reduces internal variables into |
| 630 | the modulus whenever possible, so it operates on smaller numbers. |
| 631 | .PP |
| 632 | \&\f(CW\*(C`bmodpow\*(C'\fR also supports negative exponents. |
| 633 | .PP |
| 634 | .Vb 1 |
| 635 | \& bmodpow($num, -1, $mod) |
| 636 | .Ve |
| 637 | .PP |
| 638 | is exactly equivalent to |
| 639 | .PP |
| 640 | .Vb 1 |
| 641 | \& bmodinv($num, $mod) |
| 642 | .Ve |
| 643 | .Sh "bpow" |
| 644 | .IX Subsection "bpow" |
| 645 | .Vb 1 |
| 646 | \& $x->bpow($y); # power of arguments (x ** y) |
| 647 | .Ve |
| 648 | .Sh "blsft" |
| 649 | .IX Subsection "blsft" |
| 650 | .Vb 2 |
| 651 | \& $x->blsft($y); # left shift |
| 652 | \& $x->blsft($y,$n); # left shift, by base $n (like 10) |
| 653 | .Ve |
| 654 | .Sh "brsft" |
| 655 | .IX Subsection "brsft" |
| 656 | .Vb 2 |
| 657 | \& $x->brsft($y); # right shift |
| 658 | \& $x->brsft($y,$n); # right shift, by base $n (like 10) |
| 659 | .Ve |
| 660 | .Sh "band" |
| 661 | .IX Subsection "band" |
| 662 | .Vb 1 |
| 663 | \& $x->band($y); # bitwise and |
| 664 | .Ve |
| 665 | .Sh "bior" |
| 666 | .IX Subsection "bior" |
| 667 | .Vb 1 |
| 668 | \& $x->bior($y); # bitwise inclusive or |
| 669 | .Ve |
| 670 | .Sh "bxor" |
| 671 | .IX Subsection "bxor" |
| 672 | .Vb 1 |
| 673 | \& $x->bxor($y); # bitwise exclusive or |
| 674 | .Ve |
| 675 | .Sh "bnot" |
| 676 | .IX Subsection "bnot" |
| 677 | .Vb 1 |
| 678 | \& $x->bnot(); # bitwise not (two's complement) |
| 679 | .Ve |
| 680 | .Sh "bsqrt" |
| 681 | .IX Subsection "bsqrt" |
| 682 | .Vb 1 |
| 683 | \& $x->bsqrt(); # calculate square-root |
| 684 | .Ve |
| 685 | .Sh "bfac" |
| 686 | .IX Subsection "bfac" |
| 687 | .Vb 1 |
| 688 | \& $x->bfac(); # factorial of $x (1*2*3*4*..$x) |
| 689 | .Ve |
| 690 | .Sh "round" |
| 691 | .IX Subsection "round" |
| 692 | .Vb 1 |
| 693 | \& $x->round($A,$P,$round_mode); # round to accuracy or precision using mode $r |
| 694 | .Ve |
| 695 | .Sh "bround" |
| 696 | .IX Subsection "bround" |
| 697 | .Vb 1 |
| 698 | \& $x->bround($N); # accuracy: preserve $N digits |
| 699 | .Ve |
| 700 | .Sh "bfround" |
| 701 | .IX Subsection "bfround" |
| 702 | .Vb 1 |
| 703 | \& $x->bfround($N); # round to $Nth digit, no-op for BigInts |
| 704 | .Ve |
| 705 | .Sh "bfloor" |
| 706 | .IX Subsection "bfloor" |
| 707 | .Vb 1 |
| 708 | \& $x->bfloor(); |
| 709 | .Ve |
| 710 | .PP |
| 711 | Set \f(CW$x\fR to the integer less or equal than \f(CW$x\fR. This is a no-op in BigInt, but |
| 712 | does change \f(CW$x\fR in BigFloat. |
| 713 | .Sh "bceil" |
| 714 | .IX Subsection "bceil" |
| 715 | .Vb 1 |
| 716 | \& $x->bceil(); |
| 717 | .Ve |
| 718 | .PP |
| 719 | Set \f(CW$x\fR to the integer greater or equal than \f(CW$x\fR. This is a no-op in BigInt, but |
| 720 | does change \f(CW$x\fR in BigFloat. |
| 721 | .Sh "bgcd" |
| 722 | .IX Subsection "bgcd" |
| 723 | .Vb 1 |
| 724 | \& bgcd(@values); # greatest common divisor (no OO style) |
| 725 | .Ve |
| 726 | .Sh "blcm" |
| 727 | .IX Subsection "blcm" |
| 728 | .Vb 1 |
| 729 | \& blcm(@values); # lowest common multiplicator (no OO style) |
| 730 | .Ve |
| 731 | .PP |
| 732 | head2 length |
| 733 | .PP |
| 734 | .Vb 2 |
| 735 | \& $x->length(); |
| 736 | \& ($xl,$fl) = $x->length(); |
| 737 | .Ve |
| 738 | .PP |
| 739 | Returns the number of digits in the decimal representation of the number. |
| 740 | In list context, returns the length of the integer and fraction part. For |
| 741 | BigInt's, the length of the fraction part will always be 0. |
| 742 | .Sh "exponent" |
| 743 | .IX Subsection "exponent" |
| 744 | .Vb 1 |
| 745 | \& $x->exponent(); |
| 746 | .Ve |
| 747 | .PP |
| 748 | Return the exponent of \f(CW$x\fR as BigInt. |
| 749 | .Sh "mantissa" |
| 750 | .IX Subsection "mantissa" |
| 751 | .Vb 1 |
| 752 | \& $x->mantissa(); |
| 753 | .Ve |
| 754 | .PP |
| 755 | Return the signed mantissa of \f(CW$x\fR as BigInt. |
| 756 | .Sh "parts" |
| 757 | .IX Subsection "parts" |
| 758 | .Vb 1 |
| 759 | \& $x->parts(); # return (mantissa,exponent) as BigInt |
| 760 | .Ve |
| 761 | .Sh "copy" |
| 762 | .IX Subsection "copy" |
| 763 | .Vb 1 |
| 764 | \& $x->copy(); # make a true copy of $x (unlike $y = $x;) |
| 765 | .Ve |
| 766 | .Sh "as_number" |
| 767 | .IX Subsection "as_number" |
| 768 | .Vb 1 |
| 769 | \& $x->as_number(); # return as BigInt (in BigInt: same as copy()) |
| 770 | .Ve |
| 771 | .Sh "bsrt" |
| 772 | .IX Subsection "bsrt" |
| 773 | .Vb 1 |
| 774 | \& $x->bstr(); # normalized string |
| 775 | .Ve |
| 776 | .Sh "bsstr" |
| 777 | .IX Subsection "bsstr" |
| 778 | .Vb 1 |
| 779 | \& $x->bsstr(); # normalized string in scientific notation |
| 780 | .Ve |
| 781 | .Sh "as_hex" |
| 782 | .IX Subsection "as_hex" |
| 783 | .Vb 1 |
| 784 | \& $x->as_hex(); # as signed hexadecimal string with prefixed 0x |
| 785 | .Ve |
| 786 | .Sh "as_bin" |
| 787 | .IX Subsection "as_bin" |
| 788 | .Vb 1 |
| 789 | \& $x->as_bin(); # as signed binary string with prefixed 0b |
| 790 | .Ve |
| 791 | .SH "ACCURACY and PRECISION" |
| 792 | .IX Header "ACCURACY and PRECISION" |
| 793 | Since version v1.33, Math::BigInt and Math::BigFloat have full support for |
| 794 | accuracy and precision based rounding, both automatically after every |
| 795 | operation as well as manually. |
| 796 | .PP |
| 797 | This section describes the accuracy/precision handling in Math::Big* as it |
| 798 | used to be and as it is now, complete with an explanation of all terms and |
| 799 | abbreviations. |
| 800 | .PP |
| 801 | Not yet implemented things (but with correct description) are marked with '!', |
| 802 | things that need to be answered are marked with '?'. |
| 803 | .PP |
| 804 | In the next paragraph follows a short description of terms used here (because |
| 805 | these may differ from terms used by others people or documentation). |
| 806 | .PP |
| 807 | During the rest of this document, the shortcuts A (for accuracy), P (for |
| 808 | precision), F (fallback) and R (rounding mode) will be used. |
| 809 | .Sh "Precision P" |
| 810 | .IX Subsection "Precision P" |
| 811 | A fixed number of digits before (positive) or after (negative) |
| 812 | the decimal point. For example, 123.45 has a precision of \-2. 0 means an |
| 813 | integer like 123 (or 120). A precision of 2 means two digits to the left |
| 814 | of the decimal point are zero, so 123 with P = 1 becomes 120. Note that |
| 815 | numbers with zeros before the decimal point may have different precisions, |
| 816 | because 1200 can have p = 0, 1 or 2 (depending on what the inital value |
| 817 | was). It could also have p < 0, when the digits after the decimal point |
| 818 | are zero. |
| 819 | .PP |
| 820 | The string output (of floating point numbers) will be padded with zeros: |
| 821 | .PP |
| 822 | .Vb 9 |
| 823 | \& Initial value P A Result String |
| 824 | \& ------------------------------------------------------------ |
| 825 | \& 1234.01 -3 1000 1000 |
| 826 | \& 1234 -2 1200 1200 |
| 827 | \& 1234.5 -1 1230 1230 |
| 828 | \& 1234.001 1 1234 1234.0 |
| 829 | \& 1234.01 0 1234 1234 |
| 830 | \& 1234.01 2 1234.01 1234.01 |
| 831 | \& 1234.01 5 1234.01 1234.01000 |
| 832 | .Ve |
| 833 | .PP |
| 834 | For BigInts, no padding occurs. |
| 835 | .Sh "Accuracy A" |
| 836 | .IX Subsection "Accuracy A" |
| 837 | Number of significant digits. Leading zeros are not counted. A |
| 838 | number may have an accuracy greater than the non-zero digits |
| 839 | when there are zeros in it or trailing zeros. For example, 123.456 has |
| 840 | A of 6, 10203 has 5, 123.0506 has 7, 123.450000 has 8 and 0.000123 has 3. |
| 841 | .PP |
| 842 | The string output (of floating point numbers) will be padded with zeros: |
| 843 | .PP |
| 844 | .Vb 5 |
| 845 | \& Initial value P A Result String |
| 846 | \& ------------------------------------------------------------ |
| 847 | \& 1234.01 3 1230 1230 |
| 848 | \& 1234.01 6 1234.01 1234.01 |
| 849 | \& 1234.1 8 1234.1 1234.1000 |
| 850 | .Ve |
| 851 | .PP |
| 852 | For BigInts, no padding occurs. |
| 853 | .Sh "Fallback F" |
| 854 | .IX Subsection "Fallback F" |
| 855 | When both A and P are undefined, this is used as a fallback accuracy when |
| 856 | dividing numbers. |
| 857 | .Sh "Rounding mode R" |
| 858 | .IX Subsection "Rounding mode R" |
| 859 | When rounding a number, different 'styles' or 'kinds' |
| 860 | of rounding are possible. (Note that random rounding, as in |
| 861 | Math::Round, is not implemented.) |
| 862 | .IP "'trunc'" 2 |
| 863 | .IX Item "'trunc'" |
| 864 | truncation invariably removes all digits following the |
| 865 | rounding place, replacing them with zeros. Thus, 987.65 rounded |
| 866 | to tens (P=1) becomes 980, and rounded to the fourth sigdig |
| 867 | becomes 987.6 (A=4). 123.456 rounded to the second place after the |
| 868 | decimal point (P=\-2) becomes 123.46. |
| 869 | .Sp |
| 870 | All other implemented styles of rounding attempt to round to the |
| 871 | \&\*(L"nearest digit.\*(R" If the digit D immediately to the right of the |
| 872 | rounding place (skipping the decimal point) is greater than 5, the |
| 873 | number is incremented at the rounding place (possibly causing a |
| 874 | cascade of incrementation): e.g. when rounding to units, 0.9 rounds |
| 875 | to 1, and \-19.9 rounds to \-20. If D < 5, the number is similarly |
| 876 | truncated at the rounding place: e.g. when rounding to units, 0.4 |
| 877 | rounds to 0, and \-19.4 rounds to \-19. |
| 878 | .Sp |
| 879 | However the results of other styles of rounding differ if the |
| 880 | digit immediately to the right of the rounding place (skipping the |
| 881 | decimal point) is 5 and if there are no digits, or no digits other |
| 882 | than 0, after that 5. In such cases: |
| 883 | .IP "'even'" 2 |
| 884 | .IX Item "'even'" |
| 885 | rounds the digit at the rounding place to 0, 2, 4, 6, or 8 |
| 886 | if it is not already. E.g., when rounding to the first sigdig, 0.45 |
| 887 | becomes 0.4, \-0.55 becomes \-0.6, but 0.4501 becomes 0.5. |
| 888 | .IP "'odd'" 2 |
| 889 | .IX Item "'odd'" |
| 890 | rounds the digit at the rounding place to 1, 3, 5, 7, or 9 if |
| 891 | it is not already. E.g., when rounding to the first sigdig, 0.45 |
| 892 | becomes 0.5, \-0.55 becomes \-0.5, but 0.5501 becomes 0.6. |
| 893 | .IP "'+inf'" 2 |
| 894 | .IX Item "'+inf'" |
| 895 | round to plus infinity, i.e. always round up. E.g., when |
| 896 | rounding to the first sigdig, 0.45 becomes 0.5, \-0.55 becomes \-0.5, |
| 897 | and 0.4501 also becomes 0.5. |
| 898 | .IP "'\-inf'" 2 |
| 899 | .IX Item "'-inf'" |
| 900 | round to minus infinity, i.e. always round down. E.g., when |
| 901 | rounding to the first sigdig, 0.45 becomes 0.4, \-0.55 becomes \-0.6, |
| 902 | but 0.4501 becomes 0.5. |
| 903 | .IP "'zero'" 2 |
| 904 | .IX Item "'zero'" |
| 905 | round to zero, i.e. positive numbers down, negative ones up. |
| 906 | E.g., when rounding to the first sigdig, 0.45 becomes 0.4, \-0.55 |
| 907 | becomes \-0.5, but 0.4501 becomes 0.5. |
| 908 | .PP |
| 909 | The handling of A & P in \s-1MBI/MBF\s0 (the old core code shipped with Perl |
| 910 | versions <= 5.7.2) is like this: |
| 911 | .IP "Precision" 2 |
| 912 | .IX Item "Precision" |
| 913 | .Vb 3 |
| 914 | \& * ffround($p) is able to round to $p number of digits after the decimal |
| 915 | \& point |
| 916 | \& * otherwise P is unused |
| 917 | .Ve |
| 918 | .IP "Accuracy (significant digits)" 2 |
| 919 | .IX Item "Accuracy (significant digits)" |
| 920 | .Vb 29 |
| 921 | \& * fround($a) rounds to $a significant digits |
| 922 | \& * only fdiv() and fsqrt() take A as (optional) paramater |
| 923 | \& + other operations simply create the same number (fneg etc), or more (fmul) |
| 924 | \& of digits |
| 925 | \& + rounding/truncating is only done when explicitly calling one of fround |
| 926 | \& or ffround, and never for BigInt (not implemented) |
| 927 | \& * fsqrt() simply hands its accuracy argument over to fdiv. |
| 928 | \& * the documentation and the comment in the code indicate two different ways |
| 929 | \& on how fdiv() determines the maximum number of digits it should calculate, |
| 930 | \& and the actual code does yet another thing |
| 931 | \& POD: |
| 932 | \& max($Math::BigFloat::div_scale,length(dividend)+length(divisor)) |
| 933 | \& Comment: |
| 934 | \& result has at most max(scale, length(dividend), length(divisor)) digits |
| 935 | \& Actual code: |
| 936 | \& scale = max(scale, length(dividend)-1,length(divisor)-1); |
| 937 | \& scale += length(divisior) - length(dividend); |
| 938 | \& So for lx = 3, ly = 9, scale = 10, scale will actually be 16 (10+9-3). |
| 939 | \& Actually, the 'difference' added to the scale is calculated from the |
| 940 | \& number of "significant digits" in dividend and divisor, which is derived |
| 941 | \& by looking at the length of the mantissa. Which is wrong, since it includes |
| 942 | \& the + sign (oups) and actually gets 2 for '+100' and 4 for '+101'. Oups |
| 943 | \& again. Thus 124/3 with div_scale=1 will get you '41.3' based on the strange |
| 944 | \& assumption that 124 has 3 significant digits, while 120/7 will get you |
| 945 | \& '17', not '17.1' since 120 is thought to have 2 significant digits. |
| 946 | \& The rounding after the division then uses the remainder and $y to determine |
| 947 | \& wether it must round up or down. |
| 948 | \& ? I have no idea which is the right way. That's why I used a slightly more |
| 949 | \& ? simple scheme and tweaked the few failing testcases to match it. |
| 950 | .Ve |
| 951 | .PP |
| 952 | This is how it works now: |
| 953 | .IP "Setting/Accessing" 2 |
| 954 | .IX Item "Setting/Accessing" |
| 955 | .Vb 17 |
| 956 | \& * You can set the A global via Math::BigInt->accuracy() or |
| 957 | \& Math::BigFloat->accuracy() or whatever class you are using. |
| 958 | \& * You can also set P globally by using Math::SomeClass->precision() likewise. |
| 959 | \& * Globals are classwide, and not inherited by subclasses. |
| 960 | \& * to undefine A, use Math::SomeCLass->accuracy(undef); |
| 961 | \& * to undefine P, use Math::SomeClass->precision(undef); |
| 962 | \& * Setting Math::SomeClass->accuracy() clears automatically |
| 963 | \& Math::SomeClass->precision(), and vice versa. |
| 964 | \& * To be valid, A must be > 0, P can have any value. |
| 965 | \& * If P is negative, this means round to the P'th place to the right of the |
| 966 | \& decimal point; positive values mean to the left of the decimal point. |
| 967 | \& P of 0 means round to integer. |
| 968 | \& * to find out the current global A, take Math::SomeClass->accuracy() |
| 969 | \& * to find out the current global P, take Math::SomeClass->precision() |
| 970 | \& * use $x->accuracy() respective $x->precision() for the local setting of $x. |
| 971 | \& * Please note that $x->accuracy() respecive $x->precision() fall back to the |
| 972 | \& defined globals, when $x's A or P is not set. |
| 973 | .Ve |
| 974 | .IP "Creating numbers" 2 |
| 975 | .IX Item "Creating numbers" |
| 976 | .Vb 12 |
| 977 | \& * When you create a number, you can give it's desired A or P via: |
| 978 | \& $x = Math::BigInt->new($number,$A,$P); |
| 979 | \& * Only one of A or P can be defined, otherwise the result is NaN |
| 980 | \& * If no A or P is give ($x = Math::BigInt->new($number) form), then the |
| 981 | \& globals (if set) will be used. Thus changing the global defaults later on |
| 982 | \& will not change the A or P of previously created numbers (i.e., A and P of |
| 983 | \& $x will be what was in effect when $x was created) |
| 984 | \& * If given undef for A and P, B<no> rounding will occur, and the globals will |
| 985 | \& B<not> be used. This is used by subclasses to create numbers without |
| 986 | \& suffering rounding in the parent. Thus a subclass is able to have it's own |
| 987 | \& globals enforced upon creation of a number by using |
| 988 | \& $x = Math::BigInt->new($number,undef,undef): |
| 989 | .Ve |
| 990 | .Sp |
| 991 | .Vb 2 |
| 992 | \& use Math::Bigint::SomeSubclass; |
| 993 | \& use Math::BigInt; |
| 994 | .Ve |
| 995 | .Sp |
| 996 | .Vb 3 |
| 997 | \& Math::BigInt->accuracy(2); |
| 998 | \& Math::BigInt::SomeSubClass->accuracy(3); |
| 999 | \& $x = Math::BigInt::SomeSubClass->new(1234); |
| 1000 | .Ve |
| 1001 | .Sp |
| 1002 | .Vb 2 |
| 1003 | \& $x is now 1230, and not 1200. A subclass might choose to implement |
| 1004 | \& this otherwise, e.g. falling back to the parent's A and P. |
| 1005 | .Ve |
| 1006 | .IP "Usage" 2 |
| 1007 | .IX Item "Usage" |
| 1008 | .Vb 7 |
| 1009 | \& * If A or P are enabled/defined, they are used to round the result of each |
| 1010 | \& operation according to the rules below |
| 1011 | \& * Negative P is ignored in Math::BigInt, since BigInts never have digits |
| 1012 | \& after the decimal point |
| 1013 | \& * Math::BigFloat uses Math::BigInts internally, but setting A or P inside |
| 1014 | \& Math::BigInt as globals should not tamper with the parts of a BigFloat. |
| 1015 | \& Thus a flag is used to mark all Math::BigFloat numbers as 'never round' |
| 1016 | .Ve |
| 1017 | .IP "Precedence" 2 |
| 1018 | .IX Item "Precedence" |
| 1019 | .Vb 30 |
| 1020 | \& * It only makes sense that a number has only one of A or P at a time. |
| 1021 | \& Since you can set/get both A and P, there is a rule that will practically |
| 1022 | \& enforce only A or P to be in effect at a time, even if both are set. |
| 1023 | \& This is called precedence. |
| 1024 | \& * If two objects are involved in an operation, and one of them has A in |
| 1025 | \& effect, and the other P, this results in an error (NaN). |
| 1026 | \& * A takes precendence over P (Hint: A comes before P). If A is defined, it |
| 1027 | \& is used, otherwise P is used. If neither of them is defined, nothing is |
| 1028 | \& used, i.e. the result will have as many digits as it can (with an |
| 1029 | \& exception for fdiv/fsqrt) and will not be rounded. |
| 1030 | \& * There is another setting for fdiv() (and thus for fsqrt()). If neither of |
| 1031 | \& A or P is defined, fdiv() will use a fallback (F) of $div_scale digits. |
| 1032 | \& If either the dividend's or the divisor's mantissa has more digits than |
| 1033 | \& the value of F, the higher value will be used instead of F. |
| 1034 | \& This is to limit the digits (A) of the result (just consider what would |
| 1035 | \& happen with unlimited A and P in the case of 1/3 :-) |
| 1036 | \& * fdiv will calculate (at least) 4 more digits than required (determined by |
| 1037 | \& A, P or F), and, if F is not used, round the result |
| 1038 | \& (this will still fail in the case of a result like 0.12345000000001 with A |
| 1039 | \& or P of 5, but this can not be helped - or can it?) |
| 1040 | \& * Thus you can have the math done by on Math::Big* class in three modes: |
| 1041 | \& + never round (this is the default): |
| 1042 | \& This is done by setting A and P to undef. No math operation |
| 1043 | \& will round the result, with fdiv() and fsqrt() as exceptions to guard |
| 1044 | \& against overflows. You must explicitely call bround(), bfround() or |
| 1045 | \& round() (the latter with parameters). |
| 1046 | \& Note: Once you have rounded a number, the settings will 'stick' on it |
| 1047 | \& and 'infect' all other numbers engaged in math operations with it, since |
| 1048 | \& local settings have the highest precedence. So, to get SaferRound[tm], |
| 1049 | \& use a copy() before rounding like this: |
| 1050 | .Ve |
| 1051 | .Sp |
| 1052 | .Vb 6 |
| 1053 | \& $x = Math::BigFloat->new(12.34); |
| 1054 | \& $y = Math::BigFloat->new(98.76); |
| 1055 | \& $z = $x * $y; # 1218.6984 |
| 1056 | \& print $x->copy()->fround(3); # 12.3 (but A is now 3!) |
| 1057 | \& $z = $x * $y; # still 1218.6984, without |
| 1058 | \& # copy would have been 1210! |
| 1059 | .Ve |
| 1060 | .Sp |
| 1061 | .Vb 6 |
| 1062 | \& + round after each op: |
| 1063 | \& After each single operation (except for testing like is_zero()), the |
| 1064 | \& method round() is called and the result is rounded appropriately. By |
| 1065 | \& setting proper values for A and P, you can have all-the-same-A or |
| 1066 | \& all-the-same-P modes. For example, Math::Currency might set A to undef, |
| 1067 | \& and P to -2, globally. |
| 1068 | .Ve |
| 1069 | .Sp |
| 1070 | .Vb 2 |
| 1071 | \& ?Maybe an extra option that forbids local A & P settings would be in order, |
| 1072 | \& ?so that intermediate rounding does not 'poison' further math? |
| 1073 | .Ve |
| 1074 | .IP "Overriding globals" 2 |
| 1075 | .IX Item "Overriding globals" |
| 1076 | .Vb 16 |
| 1077 | \& * you will be able to give A, P and R as an argument to all the calculation |
| 1078 | \& routines; the second parameter is A, the third one is P, and the fourth is |
| 1079 | \& R (shift right by one for binary operations like badd). P is used only if |
| 1080 | \& the first parameter (A) is undefined. These three parameters override the |
| 1081 | \& globals in the order detailed as follows, i.e. the first defined value |
| 1082 | \& wins: |
| 1083 | \& (local: per object, global: global default, parameter: argument to sub) |
| 1084 | \& + parameter A |
| 1085 | \& + parameter P |
| 1086 | \& + local A (if defined on both of the operands: smaller one is taken) |
| 1087 | \& + local P (if defined on both of the operands: bigger one is taken) |
| 1088 | \& + global A |
| 1089 | \& + global P |
| 1090 | \& + global F |
| 1091 | \& * fsqrt() will hand its arguments to fdiv(), as it used to, only now for two |
| 1092 | \& arguments (A and P) instead of one |
| 1093 | .Ve |
| 1094 | .IP "Local settings" 2 |
| 1095 | .IX Item "Local settings" |
| 1096 | .Vb 4 |
| 1097 | \& * You can set A and P locally by using $x->accuracy() and $x->precision() |
| 1098 | \& and thus force different A and P for different objects/numbers. |
| 1099 | \& * Setting A or P this way immediately rounds $x to the new value. |
| 1100 | \& * $x->accuracy() clears $x->precision(), and vice versa. |
| 1101 | .Ve |
| 1102 | .IP "Rounding" 2 |
| 1103 | .IX Item "Rounding" |
| 1104 | .Vb 15 |
| 1105 | \& * the rounding routines will use the respective global or local settings. |
| 1106 | \& fround()/bround() is for accuracy rounding, while ffround()/bfround() |
| 1107 | \& is for precision |
| 1108 | \& * the two rounding functions take as the second parameter one of the |
| 1109 | \& following rounding modes (R): |
| 1110 | \& 'even', 'odd', '+inf', '-inf', 'zero', 'trunc' |
| 1111 | \& * you can set and get the global R by using Math::SomeClass->round_mode() |
| 1112 | \& or by setting $Math::SomeClass::round_mode |
| 1113 | \& * after each operation, $result->round() is called, and the result may |
| 1114 | \& eventually be rounded (that is, if A or P were set either locally, |
| 1115 | \& globally or as parameter to the operation) |
| 1116 | \& * to manually round a number, call $x->round($A,$P,$round_mode); |
| 1117 | \& this will round the number by using the appropriate rounding function |
| 1118 | \& and then normalize it. |
| 1119 | \& * rounding modifies the local settings of the number: |
| 1120 | .Ve |
| 1121 | .Sp |
| 1122 | .Vb 3 |
| 1123 | \& $x = Math::BigFloat->new(123.456); |
| 1124 | \& $x->accuracy(5); |
| 1125 | \& $x->bround(4); |
| 1126 | .Ve |
| 1127 | .Sp |
| 1128 | .Vb 2 |
| 1129 | \& Here 4 takes precedence over 5, so 123.5 is the result and $x->accuracy() |
| 1130 | \& will be 4 from now on. |
| 1131 | .Ve |
| 1132 | .IP "Default values" 2 |
| 1133 | .IX Item "Default values" |
| 1134 | .Vb 4 |
| 1135 | \& * R: 'even' |
| 1136 | \& * F: 40 |
| 1137 | \& * A: undef |
| 1138 | \& * P: undef |
| 1139 | .Ve |
| 1140 | .IP "Remarks" 2 |
| 1141 | .IX Item "Remarks" |
| 1142 | .Vb 5 |
| 1143 | \& * The defaults are set up so that the new code gives the same results as |
| 1144 | \& the old code (except in a few cases on fdiv): |
| 1145 | \& + Both A and P are undefined and thus will not be used for rounding |
| 1146 | \& after each operation. |
| 1147 | \& + round() is thus a no-op, unless given extra parameters A and P |
| 1148 | .Ve |
| 1149 | .SH "INTERNALS" |
| 1150 | .IX Header "INTERNALS" |
| 1151 | The actual numbers are stored as unsigned big integers (with seperate sign). |
| 1152 | You should neither care about nor depend on the internal representation; it |
| 1153 | might change without notice. Use only method calls like \f(CW\*(C`$x\->sign();\*(C'\fR |
| 1154 | instead relying on the internal hash keys like in \f(CW\*(C`$x\->{sign};\*(C'\fR. |
| 1155 | .Sh "\s-1MATH\s0 \s-1LIBRARY\s0" |
| 1156 | .IX Subsection "MATH LIBRARY" |
| 1157 | Math with the numbers is done (by default) by a module called |
| 1158 | Math::BigInt::Calc. This is equivalent to saying: |
| 1159 | .PP |
| 1160 | .Vb 1 |
| 1161 | \& use Math::BigInt lib => 'Calc'; |
| 1162 | .Ve |
| 1163 | .PP |
| 1164 | You can change this by using: |
| 1165 | .PP |
| 1166 | .Vb 1 |
| 1167 | \& use Math::BigInt lib => 'BitVect'; |
| 1168 | .Ve |
| 1169 | .PP |
| 1170 | The following would first try to find Math::BigInt::Foo, then |
| 1171 | Math::BigInt::Bar, and when this also fails, revert to Math::BigInt::Calc: |
| 1172 | .PP |
| 1173 | .Vb 1 |
| 1174 | \& use Math::BigInt lib => 'Foo,Math::BigInt::Bar'; |
| 1175 | .Ve |
| 1176 | .PP |
| 1177 | Calc.pm uses as internal format an array of elements of some decimal base |
| 1178 | (usually 1e5 or 1e7) with the least significant digit first, while BitVect.pm |
| 1179 | uses a bit vector of base 2, most significant bit first. Other modules might |
| 1180 | use even different means of representing the numbers. See the respective |
| 1181 | module documentation for further details. |
| 1182 | .Sh "\s-1SIGN\s0" |
| 1183 | .IX Subsection "SIGN" |
| 1184 | The sign is either '+', '\-', 'NaN', '+inf' or '\-inf' and stored seperately. |
| 1185 | .PP |
| 1186 | A sign of 'NaN' is used to represent the result when input arguments are not |
| 1187 | numbers or as a result of 0/0. '+inf' and '\-inf' represent plus respectively |
| 1188 | minus infinity. You will get '+inf' when dividing a positive number by 0, and |
| 1189 | \&'\-inf' when dividing any negative number by 0. |
| 1190 | .Sh "\fImantissa()\fP, \fIexponent()\fP and \fIparts()\fP" |
| 1191 | .IX Subsection "mantissa(), exponent() and parts()" |
| 1192 | \&\f(CW\*(C`mantissa()\*(C'\fR and \f(CW\*(C`exponent()\*(C'\fR return the said parts of the BigInt such |
| 1193 | that: |
| 1194 | .PP |
| 1195 | .Vb 4 |
| 1196 | \& $m = $x->mantissa(); |
| 1197 | \& $e = $x->exponent(); |
| 1198 | \& $y = $m * ( 10 ** $e ); |
| 1199 | \& print "ok\en" if $x == $y; |
| 1200 | .Ve |
| 1201 | .PP |
| 1202 | \&\f(CW\*(C`($m,$e) = $x\->parts()\*(C'\fR is just a shortcut that gives you both of them |
| 1203 | in one go. Both the returned mantissa and exponent have a sign. |
| 1204 | .PP |
| 1205 | Currently, for BigInts \f(CW$e\fR will be always 0, except for NaN, +inf and \-inf, |
| 1206 | where it will be NaN; and for \f(CW$x\fR == 0, where it will be 1 |
| 1207 | (to be compatible with Math::BigFloat's internal representation of a zero as |
| 1208 | \&\f(CW0E1\fR). |
| 1209 | .PP |
| 1210 | \&\f(CW$m\fR will always be a copy of the original number. The relation between \f(CW$e\fR |
| 1211 | and \f(CW$m\fR might change in the future, but will always be equivalent in a |
| 1212 | numerical sense, e.g. \f(CW$m\fR might get minimized. |
| 1213 | .SH "EXAMPLES" |
| 1214 | .IX Header "EXAMPLES" |
| 1215 | .Vb 1 |
| 1216 | \& use Math::BigInt; |
| 1217 | .Ve |
| 1218 | .PP |
| 1219 | .Vb 1 |
| 1220 | \& sub bint { Math::BigInt->new(shift); } |
| 1221 | .Ve |
| 1222 | .PP |
| 1223 | .Vb 15 |
| 1224 | \& $x = Math::BigInt->bstr("1234") # string "1234" |
| 1225 | \& $x = "$x"; # same as bstr() |
| 1226 | \& $x = Math::BigInt->bneg("1234"); # Bigint "-1234" |
| 1227 | \& $x = Math::BigInt->babs("-12345"); # Bigint "12345" |
| 1228 | \& $x = Math::BigInt->bnorm("-0 00"); # BigInt "0" |
| 1229 | \& $x = bint(1) + bint(2); # BigInt "3" |
| 1230 | \& $x = bint(1) + "2"; # ditto (auto-BigIntify of "2") |
| 1231 | \& $x = bint(1); # BigInt "1" |
| 1232 | \& $x = $x + 5 / 2; # BigInt "3" |
| 1233 | \& $x = $x ** 3; # BigInt "27" |
| 1234 | \& $x *= 2; # BigInt "54" |
| 1235 | \& $x = Math::BigInt->new(0); # BigInt "0" |
| 1236 | \& $x--; # BigInt "-1" |
| 1237 | \& $x = Math::BigInt->badd(4,5) # BigInt "9" |
| 1238 | \& print $x->bsstr(); # 9e+0 |
| 1239 | .Ve |
| 1240 | .PP |
| 1241 | Examples for rounding: |
| 1242 | .PP |
| 1243 | .Vb 2 |
| 1244 | \& use Math::BigFloat; |
| 1245 | \& use Test; |
| 1246 | .Ve |
| 1247 | .PP |
| 1248 | .Vb 3 |
| 1249 | \& $x = Math::BigFloat->new(123.4567); |
| 1250 | \& $y = Math::BigFloat->new(123.456789); |
| 1251 | \& Math::BigFloat->accuracy(4); # no more A than 4 |
| 1252 | .Ve |
| 1253 | .PP |
| 1254 | .Vb 9 |
| 1255 | \& ok ($x->copy()->fround(),123.4); # even rounding |
| 1256 | \& print $x->copy()->fround(),"\en"; # 123.4 |
| 1257 | \& Math::BigFloat->round_mode('odd'); # round to odd |
| 1258 | \& print $x->copy()->fround(),"\en"; # 123.5 |
| 1259 | \& Math::BigFloat->accuracy(5); # no more A than 5 |
| 1260 | \& Math::BigFloat->round_mode('odd'); # round to odd |
| 1261 | \& print $x->copy()->fround(),"\en"; # 123.46 |
| 1262 | \& $y = $x->copy()->fround(4),"\en"; # A = 4: 123.4 |
| 1263 | \& print "$y, ",$y->accuracy(),"\en"; # 123.4, 4 |
| 1264 | .Ve |
| 1265 | .PP |
| 1266 | .Vb 4 |
| 1267 | \& Math::BigFloat->accuracy(undef); # A not important now |
| 1268 | \& Math::BigFloat->precision(2); # P important |
| 1269 | \& print $x->copy()->bnorm(),"\en"; # 123.46 |
| 1270 | \& print $x->copy()->fround(),"\en"; # 123.46 |
| 1271 | .Ve |
| 1272 | .PP |
| 1273 | Examples for converting: |
| 1274 | .PP |
| 1275 | .Vb 2 |
| 1276 | \& my $x = Math::BigInt->new('0b1'.'01' x 123); |
| 1277 | \& print "bin: ",$x->as_bin()," hex:",$x->as_hex()," dec: ",$x,"\en"; |
| 1278 | .Ve |
| 1279 | .SH "Autocreating constants" |
| 1280 | .IX Header "Autocreating constants" |
| 1281 | After \f(CW\*(C`use Math::BigInt ':constant'\*(C'\fR all the \fBinteger\fR decimal, hexadecimal |
| 1282 | and binary constants in the given scope are converted to \f(CW\*(C`Math::BigInt\*(C'\fR. |
| 1283 | This conversion happens at compile time. |
| 1284 | .PP |
| 1285 | In particular, |
| 1286 | .PP |
| 1287 | .Vb 1 |
| 1288 | \& perl -MMath::BigInt=:constant -e 'print 2**100,"\en"' |
| 1289 | .Ve |
| 1290 | .PP |
| 1291 | prints the integer value of \f(CW\*(C`2**100\*(C'\fR. Note that without conversion of |
| 1292 | constants the expression 2**100 will be calculated as perl scalar. |
| 1293 | .PP |
| 1294 | Please note that strings and floating point constants are not affected, |
| 1295 | so that |
| 1296 | .PP |
| 1297 | .Vb 1 |
| 1298 | \& use Math::BigInt qw/:constant/; |
| 1299 | .Ve |
| 1300 | .PP |
| 1301 | .Vb 4 |
| 1302 | \& $x = 1234567890123456789012345678901234567890 |
| 1303 | \& + 123456789123456789; |
| 1304 | \& $y = '1234567890123456789012345678901234567890' |
| 1305 | \& + '123456789123456789'; |
| 1306 | .Ve |
| 1307 | .PP |
| 1308 | do not work. You need an explicit Math::BigInt\->\fInew()\fR around one of the |
| 1309 | operands. You should also quote large constants to protect loss of precision: |
| 1310 | .PP |
| 1311 | .Vb 1 |
| 1312 | \& use Math::Bigint; |
| 1313 | .Ve |
| 1314 | .PP |
| 1315 | .Vb 1 |
| 1316 | \& $x = Math::BigInt->new('1234567889123456789123456789123456789'); |
| 1317 | .Ve |
| 1318 | .PP |
| 1319 | Without the quotes Perl would convert the large number to a floating point |
| 1320 | constant at compile time and then hand the result to BigInt, which results in |
| 1321 | an truncated result or a NaN. |
| 1322 | .PP |
| 1323 | This also applies to integers that look like floating point constants: |
| 1324 | .PP |
| 1325 | .Vb 1 |
| 1326 | \& use Math::BigInt ':constant'; |
| 1327 | .Ve |
| 1328 | .PP |
| 1329 | .Vb 2 |
| 1330 | \& print ref(123e2),"\en"; |
| 1331 | \& print ref(123.2e2),"\en"; |
| 1332 | .Ve |
| 1333 | .PP |
| 1334 | will print nothing but newlines. Use either bignum or Math::BigFloat |
| 1335 | to get this to work. |
| 1336 | .SH "PERFORMANCE" |
| 1337 | .IX Header "PERFORMANCE" |
| 1338 | Using the form \f(CW$x\fR += \f(CW$y\fR; etc over \f(CW$x\fR = \f(CW$x\fR + \f(CW$y\fR is faster, since a copy of \f(CW$x\fR |
| 1339 | must be made in the second case. For long numbers, the copy can eat up to 20% |
| 1340 | of the work (in the case of addition/subtraction, less for |
| 1341 | multiplication/division). If \f(CW$y\fR is very small compared to \f(CW$x\fR, the form |
| 1342 | \&\f(CW$x\fR += \f(CW$y\fR is \s-1MUCH\s0 faster than \f(CW$x\fR = \f(CW$x\fR + \f(CW$y\fR since making the copy of \f(CW$x\fR takes |
| 1343 | more time then the actual addition. |
| 1344 | .PP |
| 1345 | With a technique called copy\-on\-write, the cost of copying with overload could |
| 1346 | be minimized or even completely avoided. A test implementation of \s-1COW\s0 did show |
| 1347 | performance gains for overloaded math, but introduced a performance loss due |
| 1348 | to a constant overhead for all other operatons. |
| 1349 | .PP |
| 1350 | The rewritten version of this module is slower on certain operations, like |
| 1351 | \&\fInew()\fR, \fIbstr()\fR and \fInumify()\fR. The reason are that it does now more work and |
| 1352 | handles more cases. The time spent in these operations is usually gained in |
| 1353 | the other operations so that programs on the average should get faster. If |
| 1354 | they don't, please contect the author. |
| 1355 | .PP |
| 1356 | Some operations may be slower for small numbers, but are significantly faster |
| 1357 | for big numbers. Other operations are now constant (O(1), like \fIbneg()\fR, \fIbabs()\fR |
| 1358 | etc), instead of O(N) and thus nearly always take much less time. These |
| 1359 | optimizations were done on purpose. |
| 1360 | .PP |
| 1361 | If you find the Calc module to slow, try to install any of the replacement |
| 1362 | modules and see if they help you. |
| 1363 | .Sh "Alternative math libraries" |
| 1364 | .IX Subsection "Alternative math libraries" |
| 1365 | You can use an alternative library to drive Math::BigInt via: |
| 1366 | .PP |
| 1367 | .Vb 1 |
| 1368 | \& use Math::BigInt lib => 'Module'; |
| 1369 | .Ve |
| 1370 | .PP |
| 1371 | See \*(L"\s-1MATH\s0 \s-1LIBRARY\s0\*(R" for more information. |
| 1372 | .PP |
| 1373 | For more benchmark results see <http://bloodgate.com/perl/benchmarks.html>. |
| 1374 | .Sh "\s-1SUBCLASSING\s0" |
| 1375 | .IX Subsection "SUBCLASSING" |
| 1376 | .SH "Subclassing Math::BigInt" |
| 1377 | .IX Header "Subclassing Math::BigInt" |
| 1378 | The basic design of Math::BigInt allows simple subclasses with very little |
| 1379 | work, as long as a few simple rules are followed: |
| 1380 | .IP "\(bu" 2 |
| 1381 | The public \s-1API\s0 must remain consistent, i.e. if a sub-class is overloading |
| 1382 | addition, the sub-class must use the same name, in this case \fIbadd()\fR. The |
| 1383 | reason for this is that Math::BigInt is optimized to call the object methods |
| 1384 | directly. |
| 1385 | .IP "\(bu" 2 |
| 1386 | The private object hash keys like \f(CW\*(C`$x\-\*(C'\fR{sign}> may not be changed, but |
| 1387 | additional keys can be added, like \f(CW\*(C`$x\-\*(C'\fR{_custom}>. |
| 1388 | .IP "\(bu" 2 |
| 1389 | Accessor functions are available for all existing object hash keys and should |
| 1390 | be used instead of directly accessing the internal hash keys. The reason for |
| 1391 | this is that Math::BigInt itself has a pluggable interface which permits it |
| 1392 | to support different storage methods. |
| 1393 | .PP |
| 1394 | More complex sub-classes may have to replicate more of the logic internal of |
| 1395 | Math::BigInt if they need to change more basic behaviors. A subclass that |
| 1396 | needs to merely change the output only needs to overload \f(CW\*(C`bstr()\*(C'\fR. |
| 1397 | .PP |
| 1398 | All other object methods and overloaded functions can be directly inherited |
| 1399 | from the parent class. |
| 1400 | .PP |
| 1401 | At the very minimum, any subclass will need to provide it's own \f(CW\*(C`new()\*(C'\fR and can |
| 1402 | store additional hash keys in the object. There are also some package globals |
| 1403 | that must be defined, e.g.: |
| 1404 | .PP |
| 1405 | .Vb 5 |
| 1406 | \& # Globals |
| 1407 | \& $accuracy = undef; |
| 1408 | \& $precision = -2; # round to 2 decimal places |
| 1409 | \& $round_mode = 'even'; |
| 1410 | \& $div_scale = 40; |
| 1411 | .Ve |
| 1412 | .PP |
| 1413 | Additionally, you might want to provide the following two globals to allow |
| 1414 | auto-upgrading and auto-downgrading to work correctly: |
| 1415 | .PP |
| 1416 | .Vb 2 |
| 1417 | \& $upgrade = undef; |
| 1418 | \& $downgrade = undef; |
| 1419 | .Ve |
| 1420 | .PP |
| 1421 | This allows Math::BigInt to correctly retrieve package globals from the |
| 1422 | subclass, like \f(CW$SubClass::precision\fR. See t/Math/BigInt/Subclass.pm or |
| 1423 | t/Math/BigFloat/SubClass.pm completely functional subclass examples. |
| 1424 | .PP |
| 1425 | Don't forget to |
| 1426 | .PP |
| 1427 | .Vb 1 |
| 1428 | \& use overload; |
| 1429 | .Ve |
| 1430 | .PP |
| 1431 | in your subclass to automatically inherit the overloading from the parent. If |
| 1432 | you like, you can change part of the overloading, look at Math::String for an |
| 1433 | example. |
| 1434 | .SH "UPGRADING" |
| 1435 | .IX Header "UPGRADING" |
| 1436 | When used like this: |
| 1437 | .PP |
| 1438 | .Vb 1 |
| 1439 | \& use Math::BigInt upgrade => 'Foo::Bar'; |
| 1440 | .Ve |
| 1441 | .PP |
| 1442 | certain operations will 'upgrade' their calculation and thus the result to |
| 1443 | the class Foo::Bar. Usually this is used in conjunction with Math::BigFloat: |
| 1444 | .PP |
| 1445 | .Vb 1 |
| 1446 | \& use Math::BigInt upgrade => 'Math::BigFloat'; |
| 1447 | .Ve |
| 1448 | .PP |
| 1449 | As a shortcut, you can use the module \f(CW\*(C`bignum\*(C'\fR: |
| 1450 | .PP |
| 1451 | .Vb 1 |
| 1452 | \& use bignum; |
| 1453 | .Ve |
| 1454 | .PP |
| 1455 | Also good for oneliners: |
| 1456 | .PP |
| 1457 | .Vb 1 |
| 1458 | \& perl -Mbignum -le 'print 2 ** 255' |
| 1459 | .Ve |
| 1460 | .PP |
| 1461 | This makes it possible to mix arguments of different classes (as in 2.5 + 2) |
| 1462 | as well es preserve accuracy (as in \fIsqrt\fR\|(3)). |
| 1463 | .PP |
| 1464 | Beware: This feature is not fully implemented yet. |
| 1465 | .Sh "Auto-upgrade" |
| 1466 | .IX Subsection "Auto-upgrade" |
| 1467 | The following methods upgrade themselves unconditionally; that is if upgrade |
| 1468 | is in effect, they will always hand up their work: |
| 1469 | .IP "\fIbsqrt()\fR" 2 |
| 1470 | .IX Item "bsqrt()" |
| 1471 | .PD 0 |
| 1472 | .IP "\fIdiv()\fR" 2 |
| 1473 | .IX Item "div()" |
| 1474 | .IP "\fIblog()\fR" 2 |
| 1475 | .IX Item "blog()" |
| 1476 | .PD |
| 1477 | .PP |
| 1478 | Beware: This list is not complete. |
| 1479 | .PP |
| 1480 | All other methods upgrade themselves only when one (or all) of their |
| 1481 | arguments are of the class mentioned in \f(CW$upgrade\fR (This might change in later |
| 1482 | versions to a more sophisticated scheme): |
| 1483 | .SH "BUGS" |
| 1484 | .IX Header "BUGS" |
| 1485 | .IP "Out of Memory!" 2 |
| 1486 | .IX Item "Out of Memory!" |
| 1487 | Under Perl prior to 5.6.0 having an \f(CW\*(C`use Math::BigInt ':constant';\*(C'\fR and |
| 1488 | \&\f(CW\*(C`eval()\*(C'\fR in your code will crash with \*(L"Out of memory\*(R". This is probably an |
| 1489 | overload/exporter bug. You can workaround by not having \f(CW\*(C`eval()\*(C'\fR |
| 1490 | and ':constant' at the same time or upgrade your Perl to a newer version. |
| 1491 | .IP "Fails to load Calc on Perl prior 5.6.0" 2 |
| 1492 | .IX Item "Fails to load Calc on Perl prior 5.6.0" |
| 1493 | Since eval(' use ...') can not be used in conjunction with ':constant', BigInt |
| 1494 | will fall back to eval { require ... } when loading the math lib on Perls |
| 1495 | prior to 5.6.0. This simple replaces '::' with '/' and thus might fail on |
| 1496 | filesystems using a different seperator. |
| 1497 | .SH "CAVEATS" |
| 1498 | .IX Header "CAVEATS" |
| 1499 | Some things might not work as you expect them. Below is documented what is |
| 1500 | known to be troublesome: |
| 1501 | .IP "stringify, \fIbstr()\fR, \fIbsstr()\fR and 'cmp'" 1 |
| 1502 | .IX Item "stringify, bstr(), bsstr() and 'cmp'" |
| 1503 | Both stringify and \fIbstr()\fR now drop the leading '+'. The old code would return |
| 1504 | \&'+3', the new returns '3'. This is to be consistent with Perl and to make |
| 1505 | cmp (especially with overloading) to work as you expect. It also solves |
| 1506 | problems with Test.pm, it's \fIok()\fR uses 'eq' internally. |
| 1507 | .Sp |
| 1508 | Mark said, when asked about to drop the '+' altogether, or make only cmp work: |
| 1509 | .Sp |
| 1510 | .Vb 4 |
| 1511 | \& I agree (with the first alternative), don't add the '+' on positive |
| 1512 | \& numbers. It's not as important anymore with the new internal |
| 1513 | \& form for numbers. It made doing things like abs and neg easier, |
| 1514 | \& but those have to be done differently now anyway. |
| 1515 | .Ve |
| 1516 | .Sp |
| 1517 | So, the following examples will now work all as expected: |
| 1518 | .Sp |
| 1519 | .Vb 3 |
| 1520 | \& use Test; |
| 1521 | \& BEGIN { plan tests => 1 } |
| 1522 | \& use Math::BigInt; |
| 1523 | .Ve |
| 1524 | .Sp |
| 1525 | .Vb 2 |
| 1526 | \& my $x = new Math::BigInt 3*3; |
| 1527 | \& my $y = new Math::BigInt 3*3; |
| 1528 | .Ve |
| 1529 | .Sp |
| 1530 | .Vb 4 |
| 1531 | \& ok ($x,3*3); |
| 1532 | \& print "$x eq 9" if $x eq $y; |
| 1533 | \& print "$x eq 9" if $x eq '9'; |
| 1534 | \& print "$x eq 9" if $x eq 3*3; |
| 1535 | .Ve |
| 1536 | .Sp |
| 1537 | Additionally, the following still works: |
| 1538 | .Sp |
| 1539 | .Vb 3 |
| 1540 | \& print "$x == 9" if $x == $y; |
| 1541 | \& print "$x == 9" if $x == 9; |
| 1542 | \& print "$x == 9" if $x == 3*3; |
| 1543 | .Ve |
| 1544 | .Sp |
| 1545 | There is now a \f(CW\*(C`bsstr()\*(C'\fR method to get the string in scientific notation aka |
| 1546 | \&\f(CW1e+2\fR instead of \f(CW100\fR. Be advised that overloaded 'eq' always uses \fIbstr()\fR |
| 1547 | for comparisation, but Perl will represent some numbers as 100 and others |
| 1548 | as 1e+308. If in doubt, convert both arguments to Math::BigInt before doing eq: |
| 1549 | .Sp |
| 1550 | .Vb 3 |
| 1551 | \& use Test; |
| 1552 | \& BEGIN { plan tests => 3 } |
| 1553 | \& use Math::BigInt; |
| 1554 | .Ve |
| 1555 | .Sp |
| 1556 | .Vb 5 |
| 1557 | \& $x = Math::BigInt->new('1e56'); $y = 1e56; |
| 1558 | \& ok ($x,$y); # will fail |
| 1559 | \& ok ($x->bsstr(),$y); # okay |
| 1560 | \& $y = Math::BigInt->new($y); |
| 1561 | \& ok ($x,$y); # okay |
| 1562 | .Ve |
| 1563 | .Sp |
| 1564 | Alternatively, simple use <=> for comparisations, that will get it always |
| 1565 | right. There is not yet a way to get a number automatically represented as |
| 1566 | a string that matches exactly the way Perl represents it. |
| 1567 | .IP "\fIint()\fR" 1 |
| 1568 | .IX Item "int()" |
| 1569 | \&\f(CW\*(C`int()\*(C'\fR will return (at least for Perl v5.7.1 and up) another BigInt, not a |
| 1570 | Perl scalar: |
| 1571 | .Sp |
| 1572 | .Vb 4 |
| 1573 | \& $x = Math::BigInt->new(123); |
| 1574 | \& $y = int($x); # BigInt 123 |
| 1575 | \& $x = Math::BigFloat->new(123.45); |
| 1576 | \& $y = int($x); # BigInt 123 |
| 1577 | .Ve |
| 1578 | .Sp |
| 1579 | In all Perl versions you can use \f(CW\*(C`as_number()\*(C'\fR for the same effect: |
| 1580 | .Sp |
| 1581 | .Vb 2 |
| 1582 | \& $x = Math::BigFloat->new(123.45); |
| 1583 | \& $y = $x->as_number(); # BigInt 123 |
| 1584 | .Ve |
| 1585 | .Sp |
| 1586 | This also works for other subclasses, like Math::String. |
| 1587 | .Sp |
| 1588 | It is yet unlcear whether overloaded \fIint()\fR should return a scalar or a BigInt. |
| 1589 | .IP "length" 1 |
| 1590 | .IX Item "length" |
| 1591 | The following will probably not do what you expect: |
| 1592 | .Sp |
| 1593 | .Vb 2 |
| 1594 | \& $c = Math::BigInt->new(123); |
| 1595 | \& print $c->length(),"\en"; # prints 30 |
| 1596 | .Ve |
| 1597 | .Sp |
| 1598 | It prints both the number of digits in the number and in the fraction part |
| 1599 | since print calls \f(CW\*(C`length()\*(C'\fR in list context. Use something like: |
| 1600 | .Sp |
| 1601 | .Vb 1 |
| 1602 | \& print scalar $c->length(),"\en"; # prints 3 |
| 1603 | .Ve |
| 1604 | .IP "bdiv" 1 |
| 1605 | .IX Item "bdiv" |
| 1606 | The following will probably not do what you expect: |
| 1607 | .Sp |
| 1608 | .Vb 1 |
| 1609 | \& print $c->bdiv(10000),"\en"; |
| 1610 | .Ve |
| 1611 | .Sp |
| 1612 | It prints both quotient and remainder since print calls \f(CW\*(C`bdiv()\*(C'\fR in list |
| 1613 | context. Also, \f(CW\*(C`bdiv()\*(C'\fR will modify \f(CW$c\fR, so be carefull. You probably want |
| 1614 | to use |
| 1615 | .Sp |
| 1616 | .Vb 2 |
| 1617 | \& print $c / 10000,"\en"; |
| 1618 | \& print scalar $c->bdiv(10000),"\en"; # or if you want to modify $c |
| 1619 | .Ve |
| 1620 | .Sp |
| 1621 | instead. |
| 1622 | .Sp |
| 1623 | The quotient is always the greatest integer less than or equal to the |
| 1624 | real-valued quotient of the two operands, and the remainder (when it is |
| 1625 | nonzero) always has the same sign as the second operand; so, for |
| 1626 | example, |
| 1627 | .Sp |
| 1628 | .Vb 6 |
| 1629 | \& 1 / 4 => ( 0, 1) |
| 1630 | \& 1 / -4 => (-1,-3) |
| 1631 | \& -3 / 4 => (-1, 1) |
| 1632 | \& -3 / -4 => ( 0,-3) |
| 1633 | \& -11 / 2 => (-5,1) |
| 1634 | \& 11 /-2 => (-5,-1) |
| 1635 | .Ve |
| 1636 | .Sp |
| 1637 | As a consequence, the behavior of the operator % agrees with the |
| 1638 | behavior of Perl's built-in % operator (as documented in the perlop |
| 1639 | manpage), and the equation |
| 1640 | .Sp |
| 1641 | .Vb 1 |
| 1642 | \& $x == ($x / $y) * $y + ($x % $y) |
| 1643 | .Ve |
| 1644 | .Sp |
| 1645 | holds true for any \f(CW$x\fR and \f(CW$y\fR, which justifies calling the two return |
| 1646 | values of \fIbdiv()\fR the quotient and remainder. The only exception to this rule |
| 1647 | are when \f(CW$y\fR == 0 and \f(CW$x\fR is negative, then the remainder will also be |
| 1648 | negative. See below under \*(L"infinity handling\*(R" for the reasoning behing this. |
| 1649 | .Sp |
| 1650 | Perl's 'use integer;' changes the behaviour of % and / for scalars, but will |
| 1651 | not change BigInt's way to do things. This is because under 'use integer' Perl |
| 1652 | will do what the underlying C thinks is right and this is different for each |
| 1653 | system. If you need BigInt's behaving exactly like Perl's 'use integer', bug |
| 1654 | the author to implement it ;) |
| 1655 | .IP "infinity handling" 1 |
| 1656 | .IX Item "infinity handling" |
| 1657 | Here are some examples that explain the reasons why certain results occur while |
| 1658 | handling infinity: |
| 1659 | .Sp |
| 1660 | The following table shows the result of the division and the remainder, so that |
| 1661 | the equation above holds true. Some \*(L"ordinary\*(R" cases are strewn in to show more |
| 1662 | clearly the reasoning: |
| 1663 | .Sp |
| 1664 | .Vb 23 |
| 1665 | \& A / B = C, R so that C * B + R = A |
| 1666 | \& ========================================================= |
| 1667 | \& 5 / 8 = 0, 5 0 * 8 + 5 = 5 |
| 1668 | \& 0 / 8 = 0, 0 0 * 8 + 0 = 0 |
| 1669 | \& 0 / inf = 0, 0 0 * inf + 0 = 0 |
| 1670 | \& 0 /-inf = 0, 0 0 * -inf + 0 = 0 |
| 1671 | \& 5 / inf = 0, 5 0 * inf + 5 = 5 |
| 1672 | \& 5 /-inf = 0, 5 0 * -inf + 5 = 5 |
| 1673 | \& -5/ inf = 0, -5 0 * inf + -5 = -5 |
| 1674 | \& -5/-inf = 0, -5 0 * -inf + -5 = -5 |
| 1675 | \& inf/ 5 = inf, 0 inf * 5 + 0 = inf |
| 1676 | \& -inf/ 5 = -inf, 0 -inf * 5 + 0 = -inf |
| 1677 | \& inf/ -5 = -inf, 0 -inf * -5 + 0 = inf |
| 1678 | \& -inf/ -5 = inf, 0 inf * -5 + 0 = -inf |
| 1679 | \& 5/ 5 = 1, 0 1 * 5 + 0 = 5 |
| 1680 | \& -5/ -5 = 1, 0 1 * -5 + 0 = -5 |
| 1681 | \& inf/ inf = 1, 0 1 * inf + 0 = inf |
| 1682 | \& -inf/-inf = 1, 0 1 * -inf + 0 = -inf |
| 1683 | \& inf/-inf = -1, 0 -1 * -inf + 0 = inf |
| 1684 | \& -inf/ inf = -1, 0 1 * -inf + 0 = -inf |
| 1685 | \& 8/ 0 = inf, 8 inf * 0 + 8 = 8 |
| 1686 | \& inf/ 0 = inf, inf inf * 0 + inf = inf |
| 1687 | \& 0/ 0 = NaN |
| 1688 | .Ve |
| 1689 | .Sp |
| 1690 | These cases below violate the \*(L"remainder has the sign of the second of the two |
| 1691 | arguments\*(R", since they wouldn't match up otherwise. |
| 1692 | .Sp |
| 1693 | .Vb 4 |
| 1694 | \& A / B = C, R so that C * B + R = A |
| 1695 | \& ======================================================== |
| 1696 | \& -inf/ 0 = -inf, -inf -inf * 0 + inf = -inf |
| 1697 | \& -8/ 0 = -inf, -8 -inf * 0 + 8 = -8 |
| 1698 | .Ve |
| 1699 | .IP "Modifying and =" 1 |
| 1700 | .IX Item "Modifying and =" |
| 1701 | Beware of: |
| 1702 | .Sp |
| 1703 | .Vb 2 |
| 1704 | \& $x = Math::BigFloat->new(5); |
| 1705 | \& $y = $x; |
| 1706 | .Ve |
| 1707 | .Sp |
| 1708 | It will not do what you think, e.g. making a copy of \f(CW$x\fR. Instead it just makes |
| 1709 | a second reference to the \fBsame\fR object and stores it in \f(CW$y\fR. Thus anything |
| 1710 | that modifies \f(CW$x\fR (except overloaded operators) will modify \f(CW$y\fR, and vice versa. |
| 1711 | Or in other words, \f(CW\*(C`=\*(C'\fR is only safe if you modify your BigInts only via |
| 1712 | overloaded math. As soon as you use a method call it breaks: |
| 1713 | .Sp |
| 1714 | .Vb 2 |
| 1715 | \& $x->bmul(2); |
| 1716 | \& print "$x, $y\en"; # prints '10, 10' |
| 1717 | .Ve |
| 1718 | .Sp |
| 1719 | If you want a true copy of \f(CW$x\fR, use: |
| 1720 | .Sp |
| 1721 | .Vb 1 |
| 1722 | \& $y = $x->copy(); |
| 1723 | .Ve |
| 1724 | .Sp |
| 1725 | You can also chain the calls like this, this will make first a copy and then |
| 1726 | multiply it by 2: |
| 1727 | .Sp |
| 1728 | .Vb 1 |
| 1729 | \& $y = $x->copy()->bmul(2); |
| 1730 | .Ve |
| 1731 | .Sp |
| 1732 | See also the documentation for overload.pm regarding \f(CW\*(C`=\*(C'\fR. |
| 1733 | .IP "bpow" 1 |
| 1734 | .IX Item "bpow" |
| 1735 | \&\f(CW\*(C`bpow()\*(C'\fR (and the rounding functions) now modifies the first argument and |
| 1736 | returns it, unlike the old code which left it alone and only returned the |
| 1737 | result. This is to be consistent with \f(CW\*(C`badd()\*(C'\fR etc. The first three will |
| 1738 | modify \f(CW$x\fR, the last one won't: |
| 1739 | .Sp |
| 1740 | .Vb 4 |
| 1741 | \& print bpow($x,$i),"\en"; # modify $x |
| 1742 | \& print $x->bpow($i),"\en"; # ditto |
| 1743 | \& print $x **= $i,"\en"; # the same |
| 1744 | \& print $x ** $i,"\en"; # leave $x alone |
| 1745 | .Ve |
| 1746 | .Sp |
| 1747 | The form \f(CW\*(C`$x **= $y\*(C'\fR is faster than \f(CW\*(C`$x = $x ** $y;\*(C'\fR, though. |
| 1748 | .IP "Overloading \-$x" 1 |
| 1749 | .IX Item "Overloading -$x" |
| 1750 | The following: |
| 1751 | .Sp |
| 1752 | .Vb 1 |
| 1753 | \& $x = -$x; |
| 1754 | .Ve |
| 1755 | .Sp |
| 1756 | is slower than |
| 1757 | .Sp |
| 1758 | .Vb 1 |
| 1759 | \& $x->bneg(); |
| 1760 | .Ve |
| 1761 | .Sp |
| 1762 | since overload calls \f(CW\*(C`sub($x,0,1);\*(C'\fR instead of \f(CW\*(C`neg($x)\*(C'\fR. The first variant |
| 1763 | needs to preserve \f(CW$x\fR since it does not know that it later will get overwritten. |
| 1764 | This makes a copy of \f(CW$x\fR and takes O(N), but \f(CW$x\fR\->\fIbneg()\fR is O(1). |
| 1765 | .Sp |
| 1766 | With Copy\-On\-Write, this issue would be gone, but C\-o-W is not implemented |
| 1767 | since it is slower for all other things. |
| 1768 | .IP "Mixing different object types" 1 |
| 1769 | .IX Item "Mixing different object types" |
| 1770 | In Perl you will get a floating point value if you do one of the following: |
| 1771 | .Sp |
| 1772 | .Vb 3 |
| 1773 | \& $float = 5.0 + 2; |
| 1774 | \& $float = 2 + 5.0; |
| 1775 | \& $float = 5 / 2; |
| 1776 | .Ve |
| 1777 | .Sp |
| 1778 | With overloaded math, only the first two variants will result in a BigFloat: |
| 1779 | .Sp |
| 1780 | .Vb 2 |
| 1781 | \& use Math::BigInt; |
| 1782 | \& use Math::BigFloat; |
| 1783 | .Ve |
| 1784 | .Sp |
| 1785 | .Vb 3 |
| 1786 | \& $mbf = Math::BigFloat->new(5); |
| 1787 | \& $mbi2 = Math::BigInteger->new(5); |
| 1788 | \& $mbi = Math::BigInteger->new(2); |
| 1789 | .Ve |
| 1790 | .Sp |
| 1791 | .Vb 6 |
| 1792 | \& # what actually gets called: |
| 1793 | \& $float = $mbf + $mbi; # $mbf->badd() |
| 1794 | \& $float = $mbf / $mbi; # $mbf->bdiv() |
| 1795 | \& $integer = $mbi + $mbf; # $mbi->badd() |
| 1796 | \& $integer = $mbi2 / $mbi; # $mbi2->bdiv() |
| 1797 | \& $integer = $mbi2 / $mbf; # $mbi2->bdiv() |
| 1798 | .Ve |
| 1799 | .Sp |
| 1800 | This is because math with overloaded operators follows the first (dominating) |
| 1801 | operand, and the operation of that is called and returns thus the result. So, |
| 1802 | \&\fIMath::BigInt::bdiv()\fR will always return a Math::BigInt, regardless whether |
| 1803 | the result should be a Math::BigFloat or the second operant is one. |
| 1804 | .Sp |
| 1805 | To get a Math::BigFloat you either need to call the operation manually, |
| 1806 | make sure the operands are already of the proper type or casted to that type |
| 1807 | via Math::BigFloat\->\fInew()\fR: |
| 1808 | .Sp |
| 1809 | .Vb 1 |
| 1810 | \& $float = Math::BigFloat->new($mbi2) / $mbi; # = 2.5 |
| 1811 | .Ve |
| 1812 | .Sp |
| 1813 | Beware of simple \*(L"casting\*(R" the entire expression, this would only convert |
| 1814 | the already computed result: |
| 1815 | .Sp |
| 1816 | .Vb 1 |
| 1817 | \& $float = Math::BigFloat->new($mbi2 / $mbi); # = 2.0 thus wrong! |
| 1818 | .Ve |
| 1819 | .Sp |
| 1820 | Beware also of the order of more complicated expressions like: |
| 1821 | .Sp |
| 1822 | .Vb 2 |
| 1823 | \& $integer = ($mbi2 + $mbi) / $mbf; # int / float => int |
| 1824 | \& $integer = $mbi2 / Math::BigFloat->new($mbi); # ditto |
| 1825 | .Ve |
| 1826 | .Sp |
| 1827 | If in doubt, break the expression into simpler terms, or cast all operands |
| 1828 | to the desired resulting type. |
| 1829 | .Sp |
| 1830 | Scalar values are a bit different, since: |
| 1831 | .Sp |
| 1832 | .Vb 2 |
| 1833 | \& $float = 2 + $mbf; |
| 1834 | \& $float = $mbf + 2; |
| 1835 | .Ve |
| 1836 | .Sp |
| 1837 | will both result in the proper type due to the way the overloaded math works. |
| 1838 | .Sp |
| 1839 | This section also applies to other overloaded math packages, like Math::String. |
| 1840 | .Sp |
| 1841 | One solution to you problem might be autoupgrading. |
| 1842 | .IP "\fIbsqrt()\fR" 1 |
| 1843 | .IX Item "bsqrt()" |
| 1844 | \&\f(CW\*(C`bsqrt()\*(C'\fR works only good if the result is a big integer, e.g. the square |
| 1845 | root of 144 is 12, but from 12 the square root is 3, regardless of rounding |
| 1846 | mode. |
| 1847 | .Sp |
| 1848 | If you want a better approximation of the square root, then use: |
| 1849 | .Sp |
| 1850 | .Vb 4 |
| 1851 | \& $x = Math::BigFloat->new(12); |
| 1852 | \& Math::BigFloat->precision(0); |
| 1853 | \& Math::BigFloat->round_mode('even'); |
| 1854 | \& print $x->copy->bsqrt(),"\en"; # 4 |
| 1855 | .Ve |
| 1856 | .Sp |
| 1857 | .Vb 3 |
| 1858 | \& Math::BigFloat->precision(2); |
| 1859 | \& print $x->bsqrt(),"\en"; # 3.46 |
| 1860 | \& print $x->bsqrt(3),"\en"; # 3.464 |
| 1861 | .Ve |
| 1862 | .IP "\fIbrsft()\fR" 1 |
| 1863 | .IX Item "brsft()" |
| 1864 | For negative numbers in base see also brsft. |
| 1865 | .SH "LICENSE" |
| 1866 | .IX Header "LICENSE" |
| 1867 | This program is free software; you may redistribute it and/or modify it under |
| 1868 | the same terms as Perl itself. |
| 1869 | .SH "SEE ALSO" |
| 1870 | .IX Header "SEE ALSO" |
| 1871 | Math::BigFloat and Math::Big as well as Math::BigInt::BitVect, |
| 1872 | Math::BigInt::Pari and Math::BigInt::GMP. |
| 1873 | .PP |
| 1874 | The package at |
| 1875 | <http://search.cpan.org/search?mode=module&query=Math%3A%3ABigInt> contains |
| 1876 | more documentation including a full version history, testcases, empty |
| 1877 | subclass files and benchmarks. |
| 1878 | .SH "AUTHORS" |
| 1879 | .IX Header "AUTHORS" |
| 1880 | Original code by Mark Biggar, overloaded interface by Ilya Zakharevich. |
| 1881 | Completely rewritten by Tels http://bloodgate.com in late 2000, 2001. |