/usr/share/gnu-smalltalk/kernel/Integer.st is in gnu-smalltalk-common 3.2.4-2.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 | "======================================================================
|
| Integer Method Definitions
|
|
======================================================================"
"======================================================================
|
| Copyright 1988,92,94,95,99,2000,2001,2002,2003,2005,2006,2008,2009,2010
| Free Software Foundation, Inc.
| Written by Steve Byrne.
|
| This file is part of the GNU Smalltalk class library.
|
| The GNU Smalltalk class library is free software; you can redistribute it
| and/or modify it under the terms of the GNU Lesser General Public License
| as published by the Free Software Foundation; either version 2.1, or (at
| your option) any later version.
|
| The GNU Smalltalk class library is distributed in the hope that it will be
| useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
| General Public License for more details.
|
| You should have received a copy of the GNU Lesser General Public License
| along with the GNU Smalltalk class library; see the file COPYING.LIB.
| If not, write to the Free Software Foundation, 59 Temple Place - Suite
| 330, Boston, MA 02110-1301, USA.
|
======================================================================"
Number subclass: Integer [
<import: CSymbols>
<category: 'Language-Data types'>
<comment: 'I am the abstract integer class of the GNU Smalltalk system. My
subclasses'' instances can represent signed integers of various
sizes (a subclass is picked according to the size), with varying
efficiency.'>
Integer class >> coerce: aNumber [
"Answer aNumber converted to a kind of Integer"
<category: 'converting'>
^aNumber truncated
]
hash [
"Answer an hash value for the receiver"
<category: 'basic'>
^self
]
timesRepeat: aBlock [
"Evaluate aBlock a number of times equal to the receiver's value.
Compiled in-line for no argument aBlocks without temporaries, and
therefore not overridable."
<category: 'iterators'>
1 to: self do: [:each | aBlock value]
]
digitAt: index [
"Answer the index-th base-256 digit of the receiver (byte), expressed
in two's complement"
<category: 'bit operators'>
^(self bitShift: 8 - (8 * index)) bitAnd: 255
]
bitAt: index [
"Answer the index-th bit of the receiver (the LSB has an index
of 1)"
<category: 'bit operators'>
^(self bitShift: (index - 1) negated) bitAnd: 1
]
bitAt: index put: value [
"Answer an integer which is identical to the receiver,
possibly with the exception of the index-th bit of the
receiver (the LSB having an index of 1), which assumes
a value equal to the low-order bit of the second parameter."
<category: 'bit operators'>
| bit |
bit := (value bitAnd: 1) bitXor: (self bitAt: index).
bit := bit bitShift: index - 1.
^self bitXor: bit
]
bitInvert [
"Return the 1's complement of the bits of the receiver"
<category: 'bit operators'>
^self bitXor: -1
]
bitClear: aMask [
"Answer an Integer equal to the receiver, except that all the bits
that are set in aMask are cleared."
<category: 'bit operators'>
^(self bitOr: aMask) bitXor: aMask
]
allMask: anInteger [
"True if all 1 bits in anInteger are 1 in the receiver"
<category: 'bit operators'>
^(self bitAnd: anInteger) = anInteger
]
anyMask: anInteger [
"True if any 1 bits in anInteger are 1 in the receiver"
<category: 'bit operators'>
^(self bitAnd: anInteger) ~= 0
]
clearBit: index [
"Clear the index-th bit of the receiver and answer a new Integer"
<category: 'bit operators'>
| bit |
bit := 1 bitShift: index - 1.
^(self bitOr: bit) bitXor: bit
]
noMask: anInteger [
"Answer true if no 1 bits in anInteger are 1 in the receiver."
<category: 'bit operators'>
^(self bitAnd: anInteger) = 0
]
lowBit [
"Return the index of the lowest order 1 bit of the receiver."
<category: 'bit operators'>
self subclassResponsibility
]
highBit [
"Return the index of the highest order 1 bit of the receiver."
<category: 'bit operators'>
self subclassResponsibility
]
isBitSet: index [
"Answer whether the index-th bit of the receiver is set"
<category: 'bit operators'>
^((self bitShift: (index - 1) negated) bitAnd: 1) == 1
]
setBit: index [
"Set the index-th bit of the receiver and answer a new Integer"
<category: 'bit operators'>
^self bitOr: (1 bitShift: index - 1)
]
binomial: anInteger [
"Compute the number of combinations of anInteger objects among
a number of objects given by the receiver."
<category: 'math methods'>
| n k mask gcd maxNum step num den stepNum stepDen |
(self < 0 or: [anInteger < 0 or: [anInteger > self]])
ifTrue: [^self arithmeticError: 'binomial coefficient with invalid arguments'].
"The easy one."
k := anInteger + anInteger > self ifTrue: [self - anInteger] ifFalse: [anInteger].
k = 0 ifTrue: [^1].
"The number of SmallInteger factors we computed so far"
step := 1.
"Two stacks holding intermediate factors."
num := OrderedCollection new.
den := OrderedCollection new.
"The next factors to be multiplied are k and n."
n := self.
[stepNum := stepDen := 1.
[maxNum := SmallInteger largest // n.
[stepNum <= maxNum] whileTrue:
[stepNum := stepNum * n.
stepDen := stepDen * k.
k = 1
ifTrue:
["We're finishing, empty the stack and then simplify the
remaining common factors."
gcd := stepNum gcd: stepDen.
stepNum := stepNum divExact: gcd.
stepDen := stepDen divExact: gcd.
num size timesRepeat:
[stepNum := stepNum * num removeLast.
stepDen := stepDen * den removeLast].
^stepNum // stepDen].
n := n - 1.
k := k - 1].
(gcd := stepNum gcd: stepDen) > 1 and:
[stepNum := stepNum divExact: gcd.
stepDen := stepDen divExact: gcd.
"The numerators and denominators have been simplified, try
to add some more factors."
stepNum <= maxNum]]
whileTrue.
"Pop factors from the stack and combine them. The number of factors
we pop is equal to the order of the lowest bit set.
That is, on the first iteration we push a size 1 LargeInteger;
on the second iteration we pop it and make a size 2 LargeInteger;
on the third iteration we push another size 1 LargeInteger;
on the fourth iteration we pop it and make a size 2 LargeInteger;
we then combine it with the other similarly sized integer and
make a size 4 LargeInteger; and so on.
For the denominator the balancing is probably worse, since we
decide when to stop multiplying based on the numerator's magnitude,
but it is not a serious problem."
mask := step bitXor: step - 1.
[mask = 1] whileFalse:
[stepNum := stepNum * num removeLast.
stepDen := stepDen * den removeLast.
mask := mask bitShift: -1].
gcd := stepNum gcd: stepDen.
num addLast: (stepNum divExact: gcd).
den addLast: (stepDen divExact: gcd).
step := step + 1]
repeat
]
factorial [
"Return the receiver's factorial."
<category: 'math methods'>
| mask k n a b max stack |
self < 0 ifTrue: [^self arithmeticError: 'factorial of a negative number'].
self < 2 ifTrue: [^1].
"The number of SmallInteger factors we computed so far"
k := 1.
"The next factor to be multiplied."
n := self.
"The stack holding intermediate factors."
stack := OrderedCollection new.
[a := n - 1.
b := n.
max := SmallInteger largest // n.
[n := n - 2.
n < 2
ifTrue:
["Done, empty the stack and combine all the factors."
a := a * b.
stack size timesRepeat: [a := a * stack removeLast].
^a].
b < max]
whileTrue:
[a := a * (n - 1).
b := b * n].
"Compose the two SmallInteger factors"
a := a * b.
"Pop factors from the stack and combine them. The number of factors
we pop is equal to the order of the lowest bit set.
That is, on the first iteration we push a size 1 LargeInteger;
on the second iteration we pop it and make a size 2 LargeInteger;
on the third iteration we push another size 1 LargeInteger;
on the fourth iteration we pop it and make a size 2 LargeInteger;
we then combine it with the other similarly sized integer and
make a size 4 LargeInteger; and so on."
mask := k bitXor: k - 1.
[mask = 1] whileFalse:
[a := a * stack removeLast.
mask := mask bitShift: -1].
stack addLast: a.
k := k + 1]
repeat
]
estimatedLog [
"Answer an estimate of (self abs floorLog: 10)"
<category: 'math methods'>
^(self highBit asFloatD / FloatD log10Base2) ceiling
]
floorLog: radix [
"Answer (self log: radix) floor. Optimized to answer an integer."
<category: 'math methods'>
| me answer |
self < self zero
ifTrue:
[^self arithmeticError: 'cannot extract logarithm of a negative number'].
radix <= radix unity
ifTrue:
[radix <= radix zero
ifTrue: [^self arithmeticError: 'base of a logarithm cannot be negative'].
radix = radix unity
ifTrue: [^self arithmeticError: 'base of a logarithm cannot be 1'].
^(self ceilingLog: radix reciprocal) negated].
radix isInteger ifFalse: [^(radix coerce: self) floorLog: radix].
me := self.
answer := 0.
[me >= radix] whileTrue:
[me := me // radix.
answer := answer + 1].
^answer
]
ceilingLog: radix [
"Answer (self log: radix) ceiling. Optimized to answer an integer."
<category: 'math methods'>
| me answer |
self < self zero
ifTrue:
[^self arithmeticError: 'cannot extract logarithm of a negative number'].
radix <= radix unity
ifTrue:
[radix <= radix zero
ifTrue: [^self arithmeticError: 'base of a logarithm cannot be negative'].
radix = radix unity
ifTrue: [^self arithmeticError: 'base of a logarithm cannot be 1'].
^(self floorLog: radix reciprocal) negated].
radix isInteger ifFalse: [^(radix coerce: self) ceilingLog: radix].
me := self.
answer := 1.
[me > radix] whileTrue:
[me := me // radix.
answer := answer + 1].
^answer
]
gcd: anInteger [
"Return the greatest common divisor (Euclid's algorithm) between the
receiver and anInteger"
<category: 'math methods'>
| a b remainder |
self negative | anInteger negative ifTrue: [^self abs gcd: anInteger abs].
self < anInteger
ifTrue:
[a := anInteger.
b := self]
ifFalse:
[a := self.
b := anInteger].
[b = 0] whileFalse:
[remainder := a \\ b.
a := b.
b := remainder].
^a
]
lcm: anInteger [
"Return the least common multiple between the receiver and anInteger"
<category: 'math methods'>
^((self divExact: (self gcd: anInteger)) * anInteger) abs
]
even [
"Return whether the receiver is even"
<category: 'math methods'>
^(self bitAnd: 1) = 0
]
odd [
"Return whether the receiver is odd"
<category: 'math methods'>
^(self bitAnd: 1) ~= 0
]
asCharacter [
"Return self as a Character or UnicodeCharacter object."
<category: 'converting'>
^Character codePoint: self
]
coerce: aNumber [
"Coerce aNumber to the receiver's class."
<category: 'converting'>
^aNumber truncated
]
ceiling [
"Return the receiver - it's already truncated"
<category: 'converting'>
^self
]
floor [
"Return the receiver - it's already truncated"
<category: 'converting'>
^self
]
truncated [
"Return the receiver - it's already truncated"
<category: 'converting'>
^self
]
rounded [
"Return the receiver - it's already truncated"
<category: 'converting'>
^self
]
asScaledDecimal: n [
"Answer the receiver, converted to a ScaledDecimal object.
The scale is forced to be 0."
<category: 'converting'>
^ScaledDecimal newFromNumber: self asFraction scale: 0
]
asFraction [
"Return the receiver converted to a fraction"
<category: 'converting'>
^Fraction numerator: self denominator: 1
]
isLiteralObject [
"Answer whether the receiver is expressible as a Smalltalk literal."
<category: 'printing'>
^true
]
storeLiteralOn: aStream [
"Store on aStream some Smalltalk code which compiles to the receiver"
<category: 'printing'>
self storeOn: aStream
]
printOn: aStream base: b [
"Print on aStream the base b representation of the receiver"
<category: 'printing'>
aStream nextPutAll: (self printString: b)
]
storeOn: aStream base: b [
"Print on aStream Smalltalk code compiling to the receiver,
represented in base b"
<category: 'printing'>
aStream nextPutAll: (self printStringRadix: b)
]
radix: baseInteger [
"Return the base baseInteger representation of the receiver, with BBr in
front of it. This method is deprecated, use #printStringRadix:
instead."
<category: 'printing'>
^self printStringRadix: baseInteger
]
printStringRadix: baseInteger [
"Return the base baseInteger representation of the receiver, with BBr in
front of it"
<category: 'printing'>
| sign num string size radixSize |
sign := self < self zero.
num := sign ifFalse: [self] ifTrue: [self negated].
radixSize := (baseInteger floorLog: 10) + 1.
size := (num floorLog: baseInteger) + radixSize + 2.
sign ifTrue: [size := size + 1].
string := String new: size.
num replace: string withStringBase: baseInteger.
string
replaceFrom: 1
to: radixSize
with: (baseInteger printString: 10)
startingAt: 1.
string at: radixSize + 1 put: $r.
sign ifTrue: [string at: radixSize + 2 put: $-].
^string
]
printOn: aStream paddedWith: padding to: size [
"Print on aStream the base 10 representation of the receiver,
padded if necessary to size characters with copies of padding."
<category: 'printing'>
self printOn: aStream paddedWith: padding to: size base: 10
]
printPaddedWith: padding to: size base: baseInteger [
"Return the base baseInteger representation of the receiver, padded if
necessary to size characters with copies of padding."
<category: 'printing'>
^self printPaddedWith: padding to: size base: 10
]
printOn: aStream paddedWith: padding to: size base: baseInteger [
"Print on aStream the base b representation of the receiver,
padded if necessary to size characters with copies of padding."
<category: 'printing'>
| num string extra reqSize |
self < self zero
ifFalse: [num := self. extra := 0]
ifTrue: [num := self negated. extra := 1. aStream nextPut: $-].
reqSize := (num floorLog: baseInteger) + 1.
aStream next: (size - reqSize - extra max: 0) put: padding.
string := String new: reqSize.
num replace: string withStringBase: baseInteger.
aStream nextPutAll: string
]
printPaddedWith: padding to: size base: baseInteger [
"Return the base baseInteger representation of the receiver, padded if
necessary to size characters with copies of padding."
<category: 'printing'>
| num string padFirst reqSize |
self < self zero
ifFalse: [num := self. padFirst := 1]
ifTrue: [num := self negated. padFirst := 2].
reqSize := (num floorLog: baseInteger) + padFirst.
string := String new: (reqSize max: size).
padFirst = 2 ifTrue: [string at: 1 put: $-].
string
replaceFrom: padFirst
to: string size - reqSize + padFirst - 1
withObject: padding.
num replace: string withStringBase: baseInteger.
^string
]
printString: baseInteger [
"Return the base baseInteger representation of the receiver"
<category: 'printing'>
| num string |
^self < self zero
ifFalse:
[string := String new: (self floorLog: baseInteger) + 1.
self replace: string withStringBase: baseInteger]
ifTrue:
[num := self negated.
string := String new: (num floorLog: baseInteger) + 2.
string at: 1 put: $-.
num replace: string withStringBase: baseInteger]
]
displayString [
"Return the base 10 representation of the receiver"
<category: 'printing'>
^self printString: 10
]
printString [
"Return the base 10 representation of the receiver"
<category: 'printing'>
^self printString: 10
]
displayOn: aStream [
"Print on aStream the base 10 representation of the receiver"
<category: 'printing'>
aStream nextPutAll: (self printString: 10)
]
printOn: aStream [
"Print on aStream the base 10 representation of the receiver"
<category: 'printing'>
aStream nextPutAll: (self printString: 10)
]
storeString [
"Return the base 10 representation of the receiver"
<category: 'storing'>
^self printString: 10
]
storeOn: aStream [
"Print on aStream the base 10 representation of the receiver"
<category: 'storing'>
aStream nextPutAll: (self printString: 10)
]
replace: string withStringBase: b [
"Put in str the reversed base b representation of the receiver
(which is > 0)"
<category: 'private'>
| num where |
num := self.
where := string size.
[string at: where put: (Character digitValue: num \\ b).
where := where - 1.
(num := num // b) > 0]
whileTrue.
^string
]
isRational [
"Answer whether the receiver is rational - true"
<category: 'testing functionality'>
^true
]
isInteger [
<category: 'testing functionality'>
^true
]
numerator [
<category: 'accessing'>
^self
]
denominator [
<category: 'accessing'>
^1
]
]
|