'From Cuis 1.0 of 6 March 2009 [latest update: #5989] on 13 March 2009 at 9:48:50 am'!

!Number methodsFor: 'truncation and round off' stamp: 'yo 8/22/2008 16:48'!
fractionPart

	^ self asFloat fractionPart.
! !

!Number methodsFor: 'truncation and round off' stamp: 'yo 8/22/2008 16:48'!
integerPart
	^ self asFloat integerPart! !

!Number methodsFor: 'mathematical'!
raisedToInteger: anInteger 
	"Answer the receiver raised to the power anInteger where the argument 
	must be a kind of Integer. This is a special case of raisedTo:."
	(anInteger isInteger)
		ifFalse: [^self error: 'raisedToInteger: only works for integral arguments'].
	anInteger = 0 ifTrue: [^1].
	anInteger = 1 ifTrue: [^self].
	anInteger > 1 
		ifTrue: [^(self * self raisedToInteger: anInteger // 2)
					* (self raisedToInteger: anInteger \\ 2)].
	^(self raisedToInteger: anInteger negated) reciprocal! !

!Number methodsFor: 'mathematical'!
reciprocal
	"Answer 1 divided by the receiver. Create an error notification if the 
	receiver is 0."

	self = 0
		ifTrue: [^self error: 'zero has no reciprocal']
		ifFalse: [^1 / self]! !


!Float methodsFor: 'mathematical'!
reciprocal
	^ 1.0 / self! !

!Float methodsFor: 'mathematical'!
sqrt
	"Answer the square root of the receiver.
	 Optional. See Object documentation whatIsAPrimitive."

	| exp guess eps delta |
	<primitive: 55>

	"Newton-Raphson"
	self <= 0.0 ifTrue: [
		self = 0.0
			ifTrue: [^ 0.0]
			ifFalse: [^ self error: 'sqrt is invalid for x < 0']].

	"first guess is half the exponent"
	exp _ self exponent // 2.
	guess _ self timesTwoPower: (0 - exp).

	"get eps value"
	eps _ guess * Epsilon.
	eps _ eps * eps.
	delta _ (self - (guess * guess)) / (guess * 2.0).
	[(delta * delta) > eps] whileTrue: [
		guess _ guess + delta.
		delta _ (self - (guess * guess)) / (guess * 2.0)].
	^ guess! !


!Fraction methodsFor: 'mathematical'!
reciprocal 
	"Refer to the comment in Number|reciprocal."

	numerator = 0 ifTrue: [self error: '0 has no reciprocal'].
	numerator = 1 ifTrue: [^denominator].
	numerator = -1 ifTrue: [^denominator negated].
	^Fraction numerator: denominator denominator: numerator! !


!Integer methodsFor: 'mathematical'!
// aNumber

	| q |
	aNumber = 0 ifTrue: [^self error: 'division by 0'].
	self = 0 ifTrue: [^0].
	q _ self quo: aNumber 
	"Refer to the comment in Number|//.".
	(q negative
		ifTrue: [q * aNumber ~= self]
		ifFalse: [q = 0 and: [self negative ~= aNumber negative]])
		ifTrue: [^q - 1"Truncate towards minus infinity"]
		ifFalse: [^q]! !


!Number class methodsFor: 'instance creation' stamp: 'jmv 3/13/2009 09:48'!
readRemainderOf: integerPart from: aStream base: base withSign: sign 
	"Read optional fractional part and exponent, and return the final result"
	| value fraction fracpos peekChar |

	value := integerPart.
	(aStream peekFor: $.)
		ifTrue: ["<integer>.<fraction>"
			(aStream atEnd not and: [aStream peek digitValue between: 0 and: base - 1])
				ifTrue: 
					[fracpos := aStream position.
					fraction := Integer readFrom: aStream base: base.
					fraction := fraction asFloat / (base raisedTo: aStream position - fracpos).
					value := value asFloat + fraction]
				ifFalse: 
					["oops - just <integer>."
					aStream skip: -1.
					"un-gobble the period"
					^ value * sign"Number readFrom: '3r-22.2'"]].
	peekChar := aStream peek.
	"<number>(e|d|q)<exponent>>"
	peekChar = $e | (peekChar = $d) | (peekChar = $q)
		ifTrue: 
			[aStream next.
			value := value * (base raisedTo: (Integer readFrom: aStream))].
	(value isFloat
		and: [value = 0.0 and: [sign = -1]])
		ifTrue: [^ Float negativeZero]
		ifFalse: [^ value * sign]! !

Integer class removeSelector: #one!
Fraction class removeSelector: #one!
Float class removeSelector: #one!
Integer removeSelector: #adaptToScaledDecimal:andSend:!
Integer removeSelector: #asScaledDecimal:!
Integer removeSelector: #printOn:base:showRadix:!
Integer removeSelector: #printPaddedWith:to:!
Integer removeSelector: #printPaddedWith:to:base:!
Integer removeSelector: #printStringRadix:!
Fraction removeSelector: #adaptToScaledDecimal:andSend:!
Float removeSelector: #adaptToScaledDecimal:andSend:!
Float removeSelector: #printPaddedWith:to:!
Number removeSelector: #adaptToScaledDecimal:andSend:!
Number removeSelector: #asFloatD!
Number removeSelector: #asFloatE!
Number removeSelector: #asFloatQ!
Number removeSelector: #asScaledDecimal:!
Character class removeSelector: #codePoint:!
Character removeSelector: #codePoint!
BlockContext removeSelector: #argumentCount!
Smalltalk removeClassNamed: #RuleDate!
Smalltalk removeClassNamed: #RuleIndexDate!
Smalltalk removeClassNamed: #RuleSelectionCodeDate!
Smalltalk removeClassNamed: #ScaledDecimal!
