Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: [Study]: Searching for square roots

by roboticus (Chancellor)
on Nov 14, 2006 at 13:59 UTC ( [id://583964]=note: print w/replies, xml ) Need Help??


in reply to [Study]: Searching for square roots

Remove those semicolons after the if statements--the then clause is always being executed.

UPDATE: Gah! Immediately upon pressing the "create" button, I realized it was totally wrong. Please ignore this post...

UPDATE 2: L~R pointed out that removing the node contents isn't cricket. So I'm sticking in my original comment (as best as I can remember it). The only thing I can say in my defense is that I was embarrassed by such a boneheaded mistake. Ah, well, heavy coding in C++ for a few weeks certainly warps your perception of syntax. ;^)

--roboticus

Replies are listed 'Best First'.
Re^2: [Study]: Searching for square roots
by Hofmator (Curate) on Nov 14, 2006 at 14:17 UTC
    Nothing is totally wrong :)

    The only thing that you did not consider is that x*x < x for x < 1. That's why your code doesn't work for input < 1.

    Apart from that a couple of small comments concerning your code.

    • Don't use prototypes for your subroutines unless you know exactly what you are doing.
    • Don't use the variables $a and $b, they are special and used for sort.
    • Instead of writing my ($x, $y, $z) = (shift, shift, shift); simply write my ($x, $y, $z) = @_;.

    -- Hofmator

    Code written by Hofmator and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Re^2: [Study]: Searching for square roots
by Limbic~Region (Chancellor) on Nov 14, 2006 at 21:21 UTC
    roboticus,
    Removing a node's content is generally frowned upon regardless of correctness. It is best to strike what is wrong or add an update then to remove it entirely. People do learn from other's mistakes.

    FWIW, here is my take without having looked any other solutions to include monsieur_champs'.

    print sqrt($_), "\t", mysqrt($_, .0000001), "\n" for qw/.25 .50 .75 1 +10 50 100 1000 31415926/; sub mysqrt { my ($tgt, $err, $try, $len) = @_; return 1 if $tgt == 1; if (! defined $try) { ($len, $try) = $tgt < 1 ? ((1 - $tgt) / 2, $tgt + (1 - $tgt) / 2) : ($tgt / 2, $tgt / 2); return mysqrt($tgt, $err, $try, $len); } my $dif = $tgt - ($try * $try); return $try if abs($dif) < $err; $len /= 2; $try = $dif > 0 ? $try + $len : $try - $len; return mysqrt($tgt, $err, $try, $len); }
    If it isn't obvious, it is a binary search.

    Update: The adjustment for values < 1 can be mathematically simplified. Additionally, a much faster converging algorithm would be $try = (($tgt / $try) + $try) / 2.

    Cheers - L~R

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2024-04-19 21:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found