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

Re: How to get better exponentiation?

by syphilis (Archbishop)
on Jan 23, 2022 at 01:15 UTC ( [id://11140731]=note: print w/replies, xml ) Need Help??


in reply to How to get better exponentiation?

So, what’s the quickest/simplest/correct way to get Perl to act like the calculator app that comes with Windows, and give -2 as the cube root of -8?

Update: The below is based upon my observations of the Windows calculator on Windows 7.
Some further reading indicates that things might be different on Windows 10.

I think that the calculator app, when calculating x ** y for x < 0 proceeds roughly as follows:
if (x < 0) { return x ** y if y is an integer; calculate n = 1 / y; return nth root of x if n is an odd integer; die "Invalid input", } else { return x ** y; }
As you can see, in addition to needing an exponentiation function, that method also requires a function that calculates the nth root.
If you use that method and make use of POSIX::cbrt(), you should see that it DWYMs for the case -8 ** (1 / 3)

It might also be that the windows calculator is performing decimal arithmetic - and that could also produce results that differ slightly from perl in some cases.
Does anyone know for sure the number base that's used by the windows calculator ?

Cheers,
Rob

Log In?
Username:
Password:

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

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

    No recent polls found