# [Pkg] The Trunk: Kernel-nice.1213.mcz

commits at source.squeak.org commits at source.squeak.org
Tue Feb 12 22:01:57 UTC 2019

```Nicolas Cellier uploaded a new version of Kernel to project The Trunk:
http://source.squeak.org/trunk/Kernel-nice.1213.mcz

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

Name: Kernel-nice.1213
Author: nice
Time: 8 February 2019, 11:24:45.054968 pm
UUID: d7a472c9-49dd-4b1d-b73a-13b9c7c8d4c5
Ancestors: Kernel-tonyg.1212

Provide a logarithm in base 2: log2.
This (x log2) has an additional property that (x log: 2) has not: it is exact for exact powers of 2.

While implementing LogarithmicInterval, I recently wanted to use log: 2, and this was getting as bad as in Python. See https://stackoverflow.com/questions/931995/inaccurate-logarithm-in-python/54537441#54537441

I don't know if this feature is really worth it, but whether it ends up integrated or not, at least it demonstrates how easy it is to patch Squeak.

=============== Diff against Kernel-tonyg.1212 ===============

+ ----- Method: Float>>log2 (in category 'mathematical functions') -----
+ log2
+ 	Arrange to answer exact result in case of exact power of 2."
+
+ 	^ self significand ln / Ln2 + self exponent!

+ ----- Method: Fraction>>log2 (in category 'mathematical functions') -----
+ log2
+ 	"This function is defined because super log might overflow."
+ 	| res |
+ 	self <= 0 ifTrue: [DomainError signal: 'log2 is only defined for x > 0'].
+ 	"Test self < 1 before converting to float in order to avoid precision loss due to gradual underflow."
+ 	numerator < denominator ifTrue: [^self reciprocal log2 negated].
+ 	res := super log2.
+ 	res isFinite ifTrue: [^res].
+ 	^numerator log2 - denominator log2!

+ ----- Method: Integer>>log2 (in category 'mathematical functions') -----
+ log2
+ 	self > 0 ifTrue: [^super log2].
+ 	^DomainError signal: 'log2 is only defined for x > 0'!

+ ----- Method: LargeNegativeInteger>>log2 (in category 'mathematical functions') -----
+ log2
+ 	^DomainError signal: 'log2 is only defined for x > 0'!

+ ----- Method: LargePositiveInteger>>log2 (in category 'mathematical functions') -----
+ log2
+ 	"This function is defined because super log2 might overflow."
+ 	| res h |
+ 	res := super log2.
+ 	res isFinite ifTrue: [^res].
+ 	h := self highBit.
+ 	^h + (self / (1 << h)) asFloat log2!

+ ----- Method: Number>>log2 (in category 'mathematical functions') -----
+ log2
+
+ 	^self asFloat log2!

Item was changed:
----- Method: Number>>log: (in category 'mathematical functions') -----
log: aNumber

+ 	aNumber = 2 ifTrue: [^self log2].
^self ln / aNumber ln!