Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: 0**0

by Courage (Parson)
on Jan 17, 2003 at 07:33 UTC ( [id://227624]=note: print w/replies, xml ) Need Help??


in reply to 0**0

0**0 relied on underlying C library for elder perls, which is mostly 0 but could be domain error.

Currently, as I see in source code, file "pp.c", integer power is implemented in C, and it happen to result in "1", either by accident or intentionally.

Here is code excerpt from perl-5.8.0 to demonstrate my point:

/* now we have integer ** positive integer. foo & (foo - 1) is zero only for a power of 2. */ if (!(baseuv & (baseuv - 1))) { /* We are raising power-of-2 to postive integer. The logic here will work for any base (even non +-integer bases) but it can be less accurate than pow (base,power) or exp (power * log (base)) wh +en the intermediate values start to spill out of the m +antissa. With powers of 2 we know this can't happen. And powers of 2 are the favourite thing for per +l programmers to notice ** not doing what they me +an. */ NV result = 1.0; NV base = baseuok ? baseuv : -(NV)baseuv; int n = 0; /* The logic is this. x ** n === x ** m1 * x ** m2 where n = m1 + m2 so as 42 is 32 + 8 + 2 x ** 42 can be written as x ** 32 * x ** 8 * x ** 2 I can calculate x ** 2, x ** 4, x ** 8 etc triv +ially: x ** 2n is x ** n * x ** n So I loop round, squaring x each time (x, x ** 2, x ** 4, x ** 8) and multiply the re +sult by the x-value whenever that bit is set in the +power. To finish as soon as possible I zero bits in th +e power when I've done them, so that power becomes zero + when I clear the last bit (no more to do), and the l +oop terminates. */

Courage, the Cowardly Dog

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-04-24 22:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found