'From Squeak3.10beta of 22 July 2007 [latest update: #7159] on 18 June 2008 at 1:16:37 am'!
"Change Set: Integer-sqrtTruncated-Test-M7099
Date: 18 June 2008
Author: nice
(x sqrtTrunctaed) is better than (x sqrt truncated) if these conditions are met:
- computation is fast
- result is not subject to rounding errors (Float inexact arithmetic)
These are some SUnit TestCase"!
!IntegerTest methodsFor: 'testing - arithmetic' stamp: 'nice 6/18/2008 00:31'!
testSqrtRounded
1 to: 40000 do: [:i |
| inexactFloatTest exactIntegerTest |
"We could use that test if we could trust Floating point inexact arithmetic
And we can as long as integers are short (<53 bits)"
inexactFloatTest := i sqrtRounded = i asFloat sqrt rounded.
"But we prefer to assert this one in case the inexactFloatTest fails"
exactIntegerTest := i < (i sqrtTruncated + 2 reciprocal) squared
ifTrue: [i sqrtTruncated = i sqrtRounded]
ifFalse: [i sqrtTruncated + 1 = i sqrtRounded].
self assert: (inexactFloatTest or: [exactIntegerTest])].! !
!IntegerTest methodsFor: 'testing - arithmetic' stamp: 'nice 6/18/2008 00:25'!
testSqrtTruncated
1 to: 40000 do: [:i |
| x inexactFloatTest exactIntegerTest |
"We could use that test if we could trust Floating point inexact arithmetic
And we can as long as integers are short (<53 bits)"
inexactFloatTest := i sqrtTruncated = i asFloat sqrt truncated.
"But we prefer to assert this one in case the inexactFloatTest fails"
x := i sqrtTruncated.
exactIntegerTest := (x squared) <= i and: [(x+1) squared > i].
self assert: (inexactFloatTest or: [exactIntegerTest])].! !