#DOCUMENTATION by cg
class: ArithmeticValue class
comment/format in:
#domainErrorSignal
#imaginaryResultSignal
#infiniteResultSignal
#overflowSignal
#underflowSignal
#unorderedSignal
changed: #trapImaginary:
--- a/ArithmeticValue.st Fri Sep 22 10:29:37 2017 +0200
+++ b/ArithmeticValue.st Fri Sep 22 10:31:47 2017 +0200
@@ -92,26 +92,46 @@
!
domainErrorSignal
- "return the signal which is raised on math errors
- (such as log of 0 etc.)"
+ "return the signal which is raised on some math errors,
+ when the argument is outside the legal domain.
+ (such as arcSin of 2 etc.)"
^ DomainError
+
+ "
+ 2 arcSin
+ 0 log - raises infiniteResult, which is a child of DomainError
+ "
+
+ "Modified (comment): / 22-09-2017 / 09:50:41 / cg"
!
imaginaryResultSignal
"return the signal which is raised when an imaginary result would be created
- (such as when taking the sqrt of a negative number)"
+ (such as when taking the sqrt of a negative number).
+ This error can be handled by wrapping the computation inside a trapImaginary
+ block; then, a complex result is generated."
^ ImaginaryResultError
+
+ "
+ -2 sqrt
+
+ Complex trapImaginary:[ -2 sqrt ]
+ "
+
+ "Modified (comment): / 22-09-2017 / 09:53:58 / cg"
!
infiniteResultSignal
- "return the signal which is raised when an infinite result would be created
+ "return the signal which is raised when an infinite result would be created.
+ This is a subclass of DomainError.
(such as when taking the logarithm of zero)"
^ InfiniteResultError
"Created: / 03-07-2017 / 15:46:02 / cg"
+ "Modified (comment): / 22-09-2017 / 09:49:15 / cg"
!
operationNotPossibleSignal
@@ -119,9 +139,18 @@
!
overflowSignal
- "return the signal which is raised on overflow conditions (in floats)"
+ "return the signal which is raised on overflow conditions (in floats).
+ Attention: currently not raised on all architectures; some return NaN"
^ OverflowError
+
+ "
+ 10e300 * 10e30
+
+ 10q300 * 10e30
+ "
+
+ "Modified (comment): / 22-09-2017 / 09:58:49 / cg"
!
rangeErrorSignal
@@ -135,9 +164,16 @@
!
underflowSignal
- "return the signal which is raised on underflow conditions (in floats)"
+ "return the signal which is raised on underflow conditions (in floats)
+ Attention: currently not raised on all architectures; some return zero"
^ UnderflowError
+
+ "
+ 10e-300 / 10e30
+ "
+
+ "Modified (comment): / 22-09-2017 / 09:58:57 / cg"
!
unorderedSignal
@@ -145,6 +181,13 @@
for which no ordering is defined (for example: complex numbers)"
^ UnorderedNumbersError
+
+ "
+ (0 + 3i) > (0 + 4i)
+ 3i > 4i
+ "
+
+ "Modified (comment): / 22-09-2017 / 09:57:28 / cg"
! !
!ArithmeticValue class methodsFor:'class initialization'!
@@ -265,7 +308,7 @@
trapImaginary:aBlock
"evaluate aBlock;
- if any DomainError occurs inside, which would return an imaginary result,
+ if any ImaginaryResult occurs inside, which would return an imaginary result,
(eg. square root of negative number),
convert the result to a complex number and proceed.
@@ -278,6 +321,7 @@
msgSend := ex parameter.
selector := msgSend selector.
rcvr := msgSend receiver.
+
(selector == #sqrt or: [selector == #sqrtTruncated]) ifTrue: [
(rcvr isNumber and:[rcvr isReal]) ifTrue:[
ex proceedWith:(rcvr abs perform:selector) i
@@ -292,8 +336,13 @@
] ifFalse:[
ex proceedWith:(rcvr abs asComplex sqrt floor) i
].
+ ] ifFalse: [
+ (selector == #nthRoot:) ifTrue: [
+ ex proceedWith:(rcvr abs nthRoot:msgSend argument) i
+ ]
]
].
+
ex reject
] do:
aBlock
@@ -324,7 +373,7 @@
]
"
- "Modified: / 03-07-2017 / 15:53:49 / cg"
+ "Modified: / 22-09-2017 / 10:18:14 / cg"
!
trapInfinity:aBlock
@@ -1685,6 +1734,7 @@
^ self * self
! !
+
!ArithmeticValue methodsFor:'queries'!
respondsToArithmetic