Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

What does the error 'Illegal division by zero' mean?

by freak
on May 01, 2004 at 20:29 UTC ( #349686=perlquestion: print w/ replies, xml ) Need Help??
freak has asked for the wisdom of the Perl Monks concerning the following question:

here is the code :

#!/usr/bin/perl use Chemistry::Mok; $code = '/CS/g{ $n++; $l += $match->bond_map(0)->length } END { printf "Average C-S bond length: %.3f\n", $l/$n; }'; my $mok = Chemistry::Mok->new($code); $mok->run({ format => mdlmol }, glob("*.mol"));

here is the error :

Illegal division by zero at (eval 15) line 4. END failed--call queue aborted.

NOW, what does it mean?
and how to fix it ?

20040501 Edit by Corion: Added formatting

2004-05-02 Edit by jdporter: Changed title from 'What does this error mean ?'

2004-05-02 Edit by jdporter: Changed title from 'What does this error mean ?'

Comment on What does the error 'Illegal division by zero' mean?
Select or Download Code
Re: What does the error 'Illegal division by zero' mean?
by sgifford (Prior) on May 01, 2004 at 20:42 UTC
    Well, it looks like when the code in $code is evaluated, the length method returns 0, so $n is never incremented, so it's undef when the END block is run. For the purpose of the division, that undef is converted to 0, and there you are---division by 0.

    The easiest way to fix this is to check if $n is 0, and if so don't divide by it. The hard part is figuring out what you want to do instead. :)

Re: What does the error 'Illegal division by zero' mean?
by Theo (Priest) on May 02, 2004 at 04:04 UTC
    In addition to $n never being initialized, what is setting $1?
    In my very limited experience, that variable is set by parens () in a regexp, but I see no regexp.
    What happens if you 'use warning'?

    -Theo-
    (so many nodes and so little time ... )

      That's not a $1, that's an $l.
Re: What does the error 'Illegal division by zero' mean?
by davido (Archbishop) on May 02, 2004 at 05:58 UTC
    The first line of defense against difficult to understand error messages is to put near the top of your script the following line:
    use diagnostics;

    And when you do that, using your existing script, the terse error message will become the following more verbose and descriptive message:

    Illegal division by zero at (eval 15) line 4 (#1) (F) You tried to divide a number by 0. Either something was wrong in your logic, or you need to put a conditional in to guard against meaningless input. Uncaught exception from user code: Illegal division by zero at (eval 15) line 4.

    To take advantage of the best Perl has to offer in error messages, warnings, and diagnostics, be sure that your script, at least while under development, starts out with:

    use strict; use warnings; use diagnostics;


    Dave

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (6)
As of 2014-08-30 13:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (293 votes), past polls