Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: [Study]: Searching for square roots

by rhesa (Vicar)
on Nov 14, 2006 at 15:25 UTC ( [id://583981]=note: print w/replies, xml ) Need Help??


in reply to [Study]: Searching for square roots

Yep, your initial guess doesn't work for $x < 1. I suggest just starting out with 1 instead:
sub sqrt ($) { my $x = shift; return sqrt_search( $x, 0, $x, 1, 50); } # Tests sub test { my $n = shift; print "SQRT $n: ", &sqrt( $n ), ' ?= ', eval { CORE::sqrt( $n ) } +, $/, $/; } test( $_ ) for( 16, 9, 4, 2, 0.5, -1, 0, 0.001 );
Output:
SQRT 16: 4.0000114440918 ?= 4 SQRT 9: 3 ?= 3 SQRT 4: 1.99998474121094 ?= 2 SQRT 2: 1.4141845703125 ?= 1.4142135623731 SQRT 0.5: 0.7071533203125 ?= 0.707106781186548 SQRT -1: ?= SQRT 0: 0.0078125 ?= 0 SQRT 0.001: 0.03125 ?= 0.0316227766016838

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://583981]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (3)
As of 2024-04-19 01:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found