
squareroot timing
By: a guest on
Mar 25th, 2012 | syntax:
Smalltalk | size: 0.49 KB | hits: 41 | expires: Never
err :=1e-1000.
origTestNum := 2.0.
origTestNum := origTestNum asFraction.
origTestNum abs < 1 ifTrue: [testNum := 1/origTestNum]
ifFalse: [testNum := origTestNum].
[a := testNum.
p :=a*a.
[p-testNum >= err] whileTrue:
[
b := (a+(testNum/a))/2.
a := b.
p := a*a.
]. origTestNum abs < 1 ifTrue: [sqrt := 1/a] ifFalse: [sqrt := a]] timeToRun.
=> 8 miliseconds
(sqrt raisedTo: 2) asScaledDecimal:50
=> 2.00000000000000000000000000000000000000000000000000s50