[squeak-dev] The Inbox: Kernel-nice.1462.mcz

commits at source.squeak.org commits at source.squeak.org
Tue Apr 26 11:24:28 UTC 2022


Nicolas Cellier uploaded a new version of Kernel to project The Inbox:
http://source.squeak.org/inbox/Kernel-nice.1462.mcz

==================== Summary ====================

Name: Kernel-nice.1462
Author: nice
Time: 26 April 2022, 1:24:22.90052 pm
UUID: e7c0b097-55c5-8e43-9231-9164aa4a8b4e
Ancestors: Kernel-nice.1461

Detect case of overflow in intermediate computations of Complex arithmetic (* and /), and fallback to more secured algorithm, with appropriate scaling and careful order of operations.

Rationale: the strategy consisting in letting us - programmers - choose our punition, always use slower and less accurate divideSecureBy:, or take the risk of using a faster but potentially flawed #/ is too heavy for our shoulders. This is too high a tribute to prematured optimization. A better strategy is to provide arithmetic operations that will deliver correct results when possible, and let us develop ou own FastComplex specialization if we really want to live at risks for a few CPU cycles.

An effort is made here to make the additional cost bearable.

Additional cost induced by handling of exceptional cases does not count, because replacing an incorrect result with a correct one is priceless.

Additional cost induced by detection of those conditions in non exceptional case is not that high:
- isFinite is very cheap for exact arithmetic,
- and just 1 op and 1 comparison for Float

Note that in #/ implementation, we cannot test case of null denom in precondition because denom might be non null in exact arithmetic, but later vanishing due to underflow in automatic asFloat conversion. We thus have to protect with ZeroDivide handling.

Again, a FloatComplex specialization could deal with this kind of optimization if really necessary.

=============== Diff against Kernel-nice.1461 ===============

Item was changed:
  ----- Method: Complex>>* (in category 'arithmetic') -----
  * anObject
  	"Answer the result of multiplying the receiver by aNumber."
+ 	| a b c d x y newReal newImaginary |
- 	| a b c d newReal newImaginary |
  	anObject isComplex
  		ifTrue:
  			[a := self real.
  			b := self imaginary.
  			c := anObject real.
  			d := anObject imaginary.
  			newReal := (a * c) - (b * d).
  			newImaginary := (a * d) + (b * c).
+ 			((newReal isFinite not or: [newImaginary isFinite not]) and: [self isZero not and: [anObject isZero not]])
+ 				ifTrue:
+ 					["intermediate computations do overflow, but the product may be finite, retry with scaling"
+ 					x := a abs max: b abs.
+ 					y := c abs max: d abs.
+ 					a := a / x.
+ 					b := b / x.
+ 					c := c / y.
+ 					d := d / y.
+ 					newReal := (a * c) - (b * d) * x * y.
+ 					newImaginary := (a * d) + (b * c) * x * y].
  			^ Complex real: newReal imaginary: newImaginary]
  		ifFalse:
  			[^ anObject adaptToComplex: self andSend: #*]!

Item was changed:
  ----- Method: Complex>>/ (in category 'arithmetic') -----
  / anObject
  	"Answer the result of dividing receiver by aNumber"
+ 	| a b c d newReal newImaginary denom |
- 	| a b c d newReal newImaginary |
  	anObject isComplex ifTrue:
  		[a := self real.
  		b := self imaginary.
  		c := anObject real.
  		d := anObject imaginary.
+ 		denom := c squared + d squared.
+ 		[newReal := ((a * c) + (b * d)) / denom.
+ 		newImaginary := ((b * c) - (a * d)) / denom]
+ 			on: ZeroDivide do:
+ 				[:exc |
+ 				"This might be a case of underflow - resume with a value that will result in a secured retry"
+ 				exc resume: Float infinity].
+ 		(denom isFinite and: [newReal isFinite and: [newImaginary isFinite]])
+ 			ifFalse:
+ 				["case of overflow, retry securedly"
+ 				^self divideSecureBy: anObject].
- 		newReal := ((a * c) + (b * d)) / ((c * c) + (d * d)).
- 		newImaginary := ((b * c) - (a * d)) / ((c * c) + (d * d)).
  		^ Complex real: newReal imaginary: newImaginary].
  	^ anObject adaptToComplex: self andSend: #/.!



More information about the Squeak-dev mailing list