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
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.