SmallInteger.st
changeset 927 8d8edf9df0ae
parent 829 fc386319f41c
child 1036 d79dc9e4c6f5
equal deleted inserted replaced
926:101239898989 927:8d8edf9df0ae
   371 	double val = (double)_intVal(self) - _floatVal(aNumber);
   371 	double val = (double)_intVal(self) - _floatVal(aNumber);
   372 
   372 
   373 	_qMKFLOAT(newFloat, val, SENDER);
   373 	_qMKFLOAT(newFloat, val, SENDER);
   374 	RETURN ( newFloat );
   374 	RETURN ( newFloat );
   375     }
   375     }
   376 %}
   376 %}.
   377 .
       
   378     ^ aNumber differenceFromInteger:self
   377     ^ aNumber differenceFromInteger:self
   379 !
   378 !
   380 
   379 
   381 / aNumber
   380 / aNumber
   382     "return the quotient of the receivers value and the arguments value"
   381     "return the quotient of the receivers value and the arguments value"
   417 		_qMKFLOAT(newFloat, val, SENDER);
   416 		_qMKFLOAT(newFloat, val, SENDER);
   418 		RETURN ( newFloat );
   417 		RETURN ( newFloat );
   419 	    }
   418 	    }
   420 	}
   419 	}
   421     }
   420     }
   422 %}
   421 %}.
   423 .
       
   424     aNumber isInteger ifTrue:[
   422     aNumber isInteger ifTrue:[
   425 	aNumber == 0 ifTrue:[
   423 	aNumber == 0 ifTrue:[
   426 	    ^ DivisionByZeroSignal raise.
   424 	    ^ DivisionByZeroSignal raise.
   427 	].
   425 	].
   428 	^ Fraction numerator:self denominator:aNumber
   426 	^ Fraction numerator:self denominator:aNumber
   429     ].
   427     ].
   430     ^ aNumber quotientFromInteger:self
   428     ^ aNumber quotientFromInteger:self
       
   429 
       
   430     "
       
   431      8 / 4
       
   432      9 / 4
       
   433      9 // 4
       
   434      9 quo:4
       
   435 
       
   436      -8 / 4
       
   437      -9 / 4
       
   438      -9 // 4
       
   439      -9 quo:4
       
   440     "
   431 !
   441 !
   432 
   442 
   433 // aNumber
   443 // aNumber
   434     "return the integer part of the quotient of the receivers value
   444     "return the integer part of the quotient of the receivers value
   435      and the arguments value"
   445      and the arguments value.
   436 
   446      Be careful with negative results: 9 // 4 = 2, 
   437 %{  /* NOCONTEXT */
   447      while -9 // 4 = -3. 
   438     INT val;
   448      See #quo: which returns -2 in the latter."
       
   449 
       
   450 %{  /* NOCONTEXT */
       
   451     INT val, rslt;
   439 
   452 
   440     if (__isSmallInteger(aNumber)) {
   453     if (__isSmallInteger(aNumber)) {
   441 	val = _intVal(aNumber);
   454 	val = _intVal(aNumber);
   442 	if (val != 0) {
   455 	if (val != 0) {
   443 	    RETURN ( _MKSMALLINT(_intVal(self) / val) );
   456 	    rslt = _intVal(self) / val;
       
   457 	    if (rslt < 0) {
       
   458 		if (_intVal(self) % val)
       
   459 		    rslt--;
       
   460 	    }
       
   461 	    RETURN ( _MKSMALLINT(rslt) );
   444 	}
   462 	}
   445     } else {
   463     } else {
   446 	if (__isFraction(aNumber)) {
   464 	if (__isFraction(aNumber)) {
   447 	    OBJ t;
   465 	    OBJ t;
   448 	    INT num, den;
   466 	    INT num, den;
   456 		    RETURN ( _MKSMALLINT(_intVal(self) * den / num ));
   474 		    RETURN ( _MKSMALLINT(_intVal(self) * den / num ));
   457 		}
   475 		}
   458 	    }
   476 	    }
   459 	}
   477 	}
   460     }
   478     }
   461 %}
   479 %}.
   462 .
       
   463     (aNumber = 0) ifTrue:[
   480     (aNumber = 0) ifTrue:[
   464 	^ DivisionByZeroSignal raise.
   481 	^ DivisionByZeroSignal raise.
   465     ].
   482     ].
   466     ^ self retry:#// coercing:aNumber
   483     ^ self retry:#// coercing:aNumber
       
   484 
       
   485     "
       
   486      9 // 4    => 2
       
   487      -9 // 4   => -3
       
   488      9 // -4   => -3
       
   489      -9 // -4  => 2
       
   490 
       
   491      9 quo:4   => 2
       
   492      -9 quo:4  => -2
       
   493      9 quo:-4  => -2
       
   494      -9 quo:-4 => 2
       
   495     "
       
   496 !
       
   497 
       
   498 quo:aNumber
       
   499     "return the integer part of the quotient of the receivers value
       
   500      and the arguments value. 
       
   501      For positive results, this is the same as #//,
       
   502      for negative results, the remainder is ignored.
       
   503      I.e.: '9 // 4 = 2' and '-9 // 4 = -3'
       
   504      in contrast: '9 quo: 4 = 2' and '-9 quo: 4 = -2'"
       
   505 
       
   506 %{  /* NOCONTEXT */
       
   507     INT val;
       
   508 
       
   509     if (__isSmallInteger(aNumber)) {
       
   510 	val = _intVal(aNumber);
       
   511 	if (val != 0) {
       
   512 	    RETURN ( _MKSMALLINT(_intVal(self) / val) );
       
   513 	}
       
   514     } else {
       
   515 	if (__isFraction(aNumber)) {
       
   516 	    OBJ t;
       
   517 	    INT num, den;
       
   518 
       
   519 	    t = _FractionInstPtr(aNumber)->f_numerator;
       
   520 	    if (__isSmallInteger(t)) {
       
   521 		num = _intVal(t);
       
   522 		t = _FractionInstPtr(aNumber)->f_denominator;
       
   523 		if (__isSmallInteger(t)) {
       
   524 		    den = _intVal(t);
       
   525 		    RETURN ( _MKSMALLINT(_intVal(self) * den / num ));
       
   526 		}
       
   527 	    }
       
   528 	}
       
   529     }
       
   530 %}.
       
   531     (aNumber = 0) ifTrue:[
       
   532 	^ DivisionByZeroSignal raise.
       
   533     ].
       
   534     ^ self retry:#quo: coercing:aNumber
       
   535 
       
   536     "
       
   537      9 // 4
       
   538      -9 // 4
       
   539      9 quo:4
       
   540      -9 quo:4
       
   541     "
   467 !
   542 !
   468 
   543 
   469 \\ aNumber
   544 \\ aNumber
   470     "return the integer rest of the receivers value
   545     "return the integer rest of the receivers value
   471      divided by the arguments value"
   546      divided by the arguments value.
       
   547      This is not always the same as the result as obtained from #rem:"
   472 
   548 
   473 %{  /* NOCONTEXT */
   549 %{  /* NOCONTEXT */
   474     INT mySelf, val;
   550     INT mySelf, val;
   475 
   551 
   476     if (__isSmallInteger(aNumber)) {
   552     if (__isSmallInteger(aNumber)) {
   482 		RETURN ( _MKSMALLINT(-(mySelf % -val)) );
   558 		RETURN ( _MKSMALLINT(-(mySelf % -val)) );
   483 	    }
   559 	    }
   484 	    RETURN ( _MKSMALLINT(mySelf % val) );
   560 	    RETURN ( _MKSMALLINT(mySelf % val) );
   485 	}
   561 	}
   486     }
   562     }
   487 %}
   563 %}.
   488 .
       
   489     (aNumber = 0) ifTrue:[
   564     (aNumber = 0) ifTrue:[
   490 	^ DivisionByZeroSignal raise.
   565 	^ DivisionByZeroSignal raise.
   491     ].
   566     ].
   492     ^ self retry:#\\ coercing:aNumber
   567     ^ self retry:#\\ coercing:aNumber
       
   568 
       
   569     "
       
   570      9 \\ 4
       
   571      -9 \\ 4
       
   572      9 rem:4
       
   573      -9 rem:4
       
   574     "
   493 !
   575 !
   494 
   576 
   495 abs
   577 abs
   496     "return the absolute value of the receiver
   578     "return the absolute value of the receiver
   497      reimplemented here for speed"
   579      reimplemented here for speed"
   586 
   668 
   587     /* anding the tags doesn't change it */
   669     /* anding the tags doesn't change it */
   588     if (__isSmallInteger(anInteger)) {
   670     if (__isSmallInteger(anInteger)) {
   589 	RETURN ( ((OBJ) ((INT)self & (INT)anInteger)) );
   671 	RETURN ( ((OBJ) ((INT)self & (INT)anInteger)) );
   590     }
   672     }
   591 %}
   673 %}.
   592 .
   674     ^ self retry:#bitAnd: coercing:anInteger
   593     ^ self retry:#bitAnd coercing:anInteger
       
   594 
   675 
   595     "(2r001010100 bitAnd:2r00001111) radixPrintStringRadix:2"
   676     "(2r001010100 bitAnd:2r00001111) radixPrintStringRadix:2"
   596 !
   677 !
   597 
   678 
   598 bitAt:index
   679 bitAt:index
   628 
   709 
   629     /* oring the tags doesn't change it */
   710     /* oring the tags doesn't change it */
   630     if (__isSmallInteger(anInteger)) {
   711     if (__isSmallInteger(anInteger)) {
   631 	RETURN ( ((OBJ) ((INT)self | (INT)anInteger)) );
   712 	RETURN ( ((OBJ) ((INT)self | (INT)anInteger)) );
   632     }
   713     }
   633 %}
   714 %}.
   634 .
   715     ^ self retry:#bitOr: coercing:anInteger
   635     ^ self retry:#bitOr coercing:anInteger
       
   636 
   716 
   637     "(2r000000100 bitOr:2r00000011) radixPrintStringRadix:2"
   717     "(2r000000100 bitOr:2r00000011) radixPrintStringRadix:2"
   638 !
   718 !
   639 
   719 
   640 bitShift:shiftCount
   720 bitShift:shiftCount
   723     if (__isSmallInteger(aMask)) {
   803     if (__isSmallInteger(aMask)) {
   724 	RETURN ( ((INT)self & ((INT)aMask & ~TAG_MASK)) ? true : false );
   804 	RETURN ( ((INT)self & ((INT)aMask & ~TAG_MASK)) ? true : false );
   725     }
   805     }
   726 %}
   806 %}
   727 .
   807 .
   728     ^ self retry:#bitTest coercing:aMask
   808     ^ self retry:#bitTest: coercing:aMask
   729 !
   809 !
   730 
   810 
   731 bitXor:anInteger
   811 bitXor:anInteger
   732     "return the bitwise-exclusive-or of the receiver and the argument, anInteger"
   812     "return the bitwise-exclusive-or of the receiver and the argument, anInteger"
   733 
   813 
   737     if (__isSmallInteger(anInteger)) {
   817     if (__isSmallInteger(anInteger)) {
   738 	RETURN ( (OBJ)( ((INT)self ^ (INT)anInteger) | TAG_INT) );
   818 	RETURN ( (OBJ)( ((INT)self ^ (INT)anInteger) | TAG_INT) );
   739     }
   819     }
   740 %}
   820 %}
   741 .
   821 .
   742     ^ self retry:#bitXor coercing:anInteger
   822     ^ self retry:#bitXor: coercing:anInteger
   743 !
   823 !
   744 
   824 
   745 highBit
   825 highBit
   746     "return the bitIndex of the highest bit set. The returned bitIndex
   826     "return the bitIndex of the highest bit set. The returned bitIndex
   747      starts at 1 for the least significant bit. Returns -1 if no bit is set."
   827      starts at 1 for the least significant bit. Returns -1 if no bit is set."
  1828     "123 printfPrintString:'%%x -> %x'"
  1908     "123 printfPrintString:'%%x -> %x'"
  1829     "123 printfPrintString:'%%4x -> %4x'"
  1909     "123 printfPrintString:'%%4x -> %4x'"
  1830     "123 printfPrintString:'%%04x -> %04x'"
  1910     "123 printfPrintString:'%%04x -> %04x'"
  1831 ! !
  1911 ! !
  1832 
  1912 
       
  1913 !SmallInteger methodsFor:'private'!
       
  1914 
       
  1915 sign:aNumber
       
  1916     "private: for protocol completeness with LargeIntegers"
       
  1917 
       
  1918     |absVal|
       
  1919 
       
  1920     absVal := self abs.
       
  1921     aNumber < 0 ifTrue:[
       
  1922 	^ absVal negated
       
  1923     ].
       
  1924     aNumber == 0 ifTrue:[^ 0].
       
  1925     ^ absVal
       
  1926 
       
  1927     "
       
  1928      -4 sign:-1   
       
  1929      -4 sign:0    
       
  1930      -4 sign:1    
       
  1931      -4 sign:-1   
       
  1932      -4 sign:0    
       
  1933      -4 sign:1    
       
  1934     "
       
  1935 ! !
       
  1936 
  1833 !SmallInteger methodsFor:'testing'!
  1937 !SmallInteger methodsFor:'testing'!
  1834 
  1938 
  1835 between:min and:max
  1939 between:min and:max
  1836     "return true if the receiver is less than or equal to the argument max
  1940     "return true if the receiver is less than or equal to the argument max
  1837      and greater than or equal to the argument min.
  1941      and greater than or equal to the argument min.
  1948 ! !
  2052 ! !
  1949 
  2053 
  1950 !SmallInteger class methodsFor:'documentation'!
  2054 !SmallInteger class methodsFor:'documentation'!
  1951 
  2055 
  1952 version
  2056 version
  1953     ^ '$Header: /cvs/stx/stx/libbasic/SmallInteger.st,v 1.43 1996-01-04 01:23:38 cg Exp $'
  2057     ^ '$Header: /cvs/stx/stx/libbasic/SmallInteger.st,v 1.44 1996-02-02 16:20:51 cg Exp $'
  1954 ! !
  2058 ! !