Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

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

00

Defining 00=1 is a good idea, because it makes the easy things easy and the hard things possible. For instance, the binomial theorem says
(a+b)n = ∑k=0n Ck,n akbn-k,
where Ck,n=n!/(k!(n-k)!) is the binomial coefficient.

Only to you, it doesn't. Because when n=0, a=0 and b!=0, the left hand side is defined but the right hand side isn't. Of course, if you agreed that 00=1 then the right hand side would be defined and you'd have equality.

Or take the exponential function:

exp(x) = ∑n=0 xn/n!
(and similar expansions for sin and cos, not to mention any other Taylor expansion). This requires you to believe that 0!=00=1, or you'll have trouble reading the first term (which always uses 0!), and if x=0 you'll also have a 00 there.

Or believing that am+n = aman (when a=0, m=-n). It all requires you to believe that 00=1, or spend the rest of your life writing down pointless special cases.

Why is /0 an error?

So if 00=1 for convenience, why do I refuse to accept a more "convenient" behaviour of division by zero -- return undef instead of an error?

Because the error is more convenient! First, note that there's no "convenient" value to return for x/0, ever. Which is why CheeseLord wants to get back the non-numeric undef.

Which might be nice for $a=$b/$c. Except that then you have to test for defined($a) afterwards, where previously you'd test for $c==0 beforehand.

Next, there's the problem of implicit conversion of undef to 0 in numeric expressions. We certainly don't want 5+17/0 == 5 (and if you still think we do, do you also want 1/0 < 1/10?)

So we'd have to do something more clever about undef in numerical operations, say having all results undef in the presence of an undef operand. Apart from probably being slower (a well-worn excuse, and not particularly convincing by now), you still have to test your return value to see if it's undef. And you get significantly less information about the precise source of the undef (although with Perl6 attributes, perhaps you could get more information in such a case).

How does

And, of course, such "propogated undef" semantics break every existing line of Perl...

So here's a challenge for anyone (CheeseLord or otherwise) who'd like to have ! defined(x/0) in Perl, along with "propogated undef": show some code that is clearer to express with these semantics than in the present case.


In reply to Re: More Fun with Zero! by ariels
in thread 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 having a coffee break in the Monastery: (8)
As of 2024-04-23 12:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found