Hey, cool problem, nysus... I'll throw out my code, first. Be forewarned, though -- I'm nowhere near the pro you asked for.

#!/usr/bin/perl -w use strict; sub sqroot { my $maxdiff = 1e-18; my ($value, $num) = (1, shift @_); my $i = ''; # Added $num *= -1 and $i = 'i' if ($num < 0); # Added my $oldvalue = 0; while (abs($oldvalue - $value) > $maxdiff) { $oldvalue = $value; $value = $num / $value; $value = ($value + $oldvalue) / 2; } # return $value; # (old) return $value . $i; } my $num; do { print "Please enter a number: "; # changed from "positive number" chomp ($num = <STDIN>); # } until ($num =~ /^\d*[.\d]\d*$/); # (old regex) } until ($num =~ /^-?\d+\.?\d*$/); my $sqrt = sqroot $num; print "Square root of $num: $sqrt\n";

Now, for a few comments:

  1. IMO, your code was broken down perhaps a little too much. Of course, depending on what you'll want to do in the future, you may want to do that. But I'd suggest a name such as diff instead of get_accuracy, for two reasons: it is shorter (which I know can be a bad thing in some cases), and it's more descriptive of its function. You're only returning the difference between the two numbers, and I'd prefer something that I'd be able to easily recognize later on, so I could steal it and put it in another program.
  2. Also, I moved the print statement out of the subs... once again, it's only my opinion, but I think those things should be outside of the sub (If I call "sqroot", I don't expect it to print anything out. "print_sqrt" on the other hand...)
  3. You'll notice I also have a different regex and a do-until loop... the loop style is just personal preference, but I feel the regex is better, as it allows the user to enter in just the number (e.g. "10" vs. "10.0"), however, it may not be the most efficent. I'd like a little feedback from those who know more than I on that, actually...

Once again, considering that optimial is a relative term, you're free to call my ideas ridiculous, if you like. :) Once again, though, cool problem... made me think a good bit. (I'll probably still be thinking about it tomorrow...)

Update: I just couldn't leave well enough alone. Above are modifications to the original script that allow processing of negative numbers, as well as an improved (?) regex. Also changed $maxdiff as per ariels' suggestion.

His Royal Cheeziness


In reply to Re: Square Root algorithm by CheeseLord
in thread Square Root algorithm by nysus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.