Created
August 10, 2013 21:19
-
-
Save jimweirich/6202183 to your computer and use it in GitHub Desktop.
Square Root function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
Sqrt = ->(n) { | |
guess = n / 2.0 | |
loop do | |
if (guess**2 - n) < 0.0001 | |
return guess | |
end | |
guess = (guess + n/guess) / 2.0 | |
end | |
} | |
require 'rspec/given' | |
require 'given/fuzzy_shortcuts' | |
describe "sqrt" do | |
Then { Sqrt.(100) == 10.±(0.0001) } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment