Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

perl to protoype algorithms

by adamcrussell (Hermit)
on Jun 12, 2002 at 17:55 UTC ( [id://173942]=perlquestion: print w/replies, xml ) Need Help??

adamcrussell has asked for the wisdom of the Perl Monks concerning the following question:

Although most of the work I do on a day to day basis is in java I occasionally use perl to hack out a quick algorithm or to generate some test data. This has always worked fine but noticed today that something *not good* was happening. This perl code
$c=$w%24; if($c==0){$c=24;}#nobody says 0 o'clock instead of 12 o'clock do they? + >:) $r=ceil($w/24); $n=(($c-1)*16)+$r;
will take a $w=1 and give me a $n=1, $w=25 results in a $n=2

The problem is that when I implement the java analogue of the perl code:

int c=w%24; double r=Math.ceil(w/24); if(c==0){c=24;} Double d=new Double(((c-1)*16)+r);//This line and the next line are ne +cessary because ceil() returns a double int n=d.intValue();
a w=1 result in an n=0, w=25 results in an n=1 .

Why is this? Also, in the larger scope of things, is prototyping algorithms in perl to be implemented in another language a stupid idea likley to have me meet more trouble down the road?

Edit by dws for typos and formatting

Replies are listed 'Best First'.
Re: perl to protoype algorithms
by Juerd (Abbot) on Jun 12, 2002 at 19:19 UTC

    Warning: rant about style ahead

    $c=$w%24; if($c==0){$c=24;} $r=ceil($w/24); $n=(($c-1)*16)+$r;

    Maybe if you used _words_ for variable names, more people would understand your code. I didn't even try to figure out what $c, $w, $r and $n are. This is for all programming, but for prototyping it is very, very important if you want to survive. Not everything is a 4-line script.

    Whitespace also helps a lot:

    $c=$w%24; $c = $w % 24; if($c==0){$c=24;} $c ||= 24; $r=ceil($w/24); $r = ceil($w / 24); $n=(($c-1)*16)+$r; $n = (($c - 1) * 16) + $r;
    The second example is easier to read, imho. Note that I used ||=.

    - Yes, I reinvent wheels.
    - Spam: Visit eurotraQ.
    

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: perl to protoype algortithms
by c-era (Curate) on Jun 12, 2002 at 18:47 UTC
    First, are you meaning to call a sub routine? $r=c($w/24); If you are, you need to post it. When I change that line to $r=($w/24); I get the same answer as java.

    As for prototyping in another language, I tend to avoid it. Each language will have some features/bugs that don't exist in the language you are prototyping in, which can either cause problems in the conversion, or not let you make use of the features of that language.

      For some very annoying reason I cannot update this node. The line you mention is, in fact , a typo and should read
      $r=ceil($w/24)
      The ceil function is provided by the POSIX module.
Re: perl to protoype algorithms
by jsprat (Curate) on Jun 12, 2002 at 20:00 UTC
    To answer your question, prototype and test the algorithm in as many languages as you are willing to debug. Personally, I'd prototype in whatever language I was going to implement in. While good algorithms are language agnostic, debugging is not. If you use a good algorithm, it will look mostly the same regardless of whether you write the code in C, Perl or Java - but there will be differences in implementation (like casting in a strongly typed language).

    In your snippet, perl does exactly what I would expect - the algorithm is fine. It's the java implementation that fails.

    Take a look at the following snippet, and see if it produces the results you expect with different values of w.

    public class HelloWorld { public static void main(String[] args) { int w=Integer.parseInt(args[0]); System.out.println("No cast: " + Math.ceil (w / 24)); System.out.println("Divide, then cast: " + Math.ceil ((double) (w +/ 24))); System.out.println("Cast, then divide: " + Math.ceil ((double) w / + 24)); } }

    Maybe this thread should be posted at http://javajunkies.org ;-)

      There's a place called JavaJunkies - Wow! Thanks for the link!

      Tongue firmly in cheek

Log In?
Username:
Password:

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

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

    No recent polls found