Don't like ads? PRO users don't see any ads ;-)
Guest

squareroot timing

By: a guest on Mar 25th, 2012  |  syntax: Smalltalk  |  size: 0.49 KB  |  hits: 41  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. err :=1e-1000.
  2. origTestNum := 2.0.
  3. origTestNum := origTestNum asFraction.
  4. origTestNum abs < 1  ifTrue: [testNum := 1/origTestNum]
  5.                                                 ifFalse: [testNum := origTestNum].
  6.  
  7. [a := testNum.
  8. p :=a*a.
  9. [p-testNum >= err] whileTrue:
  10. [
  11.     b := (a+(testNum/a))/2.
  12.     a := b.
  13.     p := a*a.
  14. ]. origTestNum abs < 1 ifTrue: [sqrt :=  1/a] ifFalse: [sqrt := a]] timeToRun.
  15.  
  16. =>  8 miliseconds
  17. (sqrt raisedTo: 2) asScaledDecimal:50
  18.  
  19. => 2.00000000000000000000000000000000000000000000000000s50