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??
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

In reply to Re: How to get better exponentiation? by syphilis
in thread How to get better exponentiation? by Athanasius

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 admiring the Monastery: (3)
As of 2024-03-28 17:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found