Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

After the discussion a few weeks back about zero and the quirks it introduces in mathematics, I realized there were some other mathematical points that could be brought up with respect to Perl and programming... and (hopefully) we won't need Math degrees to understand these. :-)

Exponentiation (0^0)

I forget exactly what I was doing to get to the point where I was doing something in Perl that used "0 ** 0" (it was probably another obfu...), but after I realized that the results didn't agree with what I thought they should be, I tried the expression (modified for the various platforms, of course) in a few different places:

  • In MS Excel, trying the formula '=0^0' will return a #NUM! error.
  • With the Windows Calculator, "0, x^y, 0" results in an answer of 1.
  • With two personal calculators, the expression results in an error.
  • In C, math.h's "pow(0,0)" returns 1.
  • In Python (not that it matters at all ;-), "0 ** 0" returns 1.
  • In Java, Math.pow(0,0) also returns 1.
  • And with Perl...
     
    % perl -le 'print 0**0'
    1
    

My point is that I'm getting a consistent result of 1 with the computer languages I can try out... but from what I know of Mathematics, that just ain't right. And to make things more troubling for my psyche, I'm getting answers that support me from my calculators... and I've got two Microsoft products telling me different things. But that's no surprise.

For those who are wondering what the answer should be... well, until I played around with all this, I was pretty sure the answer was undefined. And I'm still pretty sure of that, but now I'm trying to figure out just how these operations are defined in the various languages to get these results.

So my question here is: What's going on?!? Is this another case of programming math gone wrong? And if all these languages are willing to choke on division by zero, why won't they choke on this? Speaking of that subject...

Division by zero

In addition, I have another point which fits in with this discussion of mathematical murkiness. In virtually (?) all programming languages, "x / 0" will result in some sort of Very Bad Thing happening. In C, the program will die. In Java, an ArithmeticException is thrown. In Perl, the script will also die unless you do something eval. (I'm sorry, that's a horrible pun -- please don't throw moldy cheese at me.)

My point here is that, given Perl's undef value, it sounds a lot nicer to say something like:

funkshun( EXPR ) if defined ( EXPR );

... than it is to do this:

eval EXPR; funkshun( EXPR ) unless ($@);

So why can't dividing by zero result in an undefined value, as mathematics would have us believe? Division by zero would still result in a few warnings under -w if this undefined value was used later on, correct? I guess what I'm really saying is that I don't think that this should be the (near-)fatal problem that it is at the moment, as it really doesn't fit with (what I perceive as) Perl's "enough rope to hang yourself" idea...

... of course, that's just my opinion. It's likely quite incorrect. ;-) Your thoughts?

His Royal Cheeziness


In reply to More Fun with Zero! by CheeseLord

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2024-04-18 02:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found