Bug 69293 - Error taking Cube Root of Negative Number
Summary: Error taking Cube Root of Negative Number
Status: RESOLVED FIXED
Alias: None
Product: LibreOffice
Classification: Unclassified
Component: Calc (show other bugs)
Version:
(earliest affected)
Inherited From OOo
Hardware: Other All
: medium normal
Assignee: supremearyal
URL:
Whiteboard: target:4.5.0
Keywords:
Depends on:
Blocks:
 
Reported: 2013-09-12 21:41 UTC by Mike Kaganski
Modified: 2022-04-11 11:40 UTC (History)
2 users (show)

See Also:
Crash report or crash signature:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Mike Kaganski 2013-09-12 21:41:31 UTC
This is a long-standing problem inherited from OOo. I cannot find a dupe for it in this issue tracker, so here it goes.
See https://issues.apache.org/ooo/show_bug.cgi?id=37129 for discussion.

Quoting the abovementioned issue:
"It looks like calc is unable to evaluate odd roots (a number raised to the power of 1/3,1/5,1/7,...etc).  For instance if you type in as a formula -8^(1/3), calc returns #VALUE when the answer should be -2.  It looks like any negative number you raise to a fractional exponent can't be evaluated. For even roots this should be expected since imaginary values would result, however odd roots should result in real values."

"It leads to incompatibility with MS Excel and thus, to difficulties in migration.

The general math rule says that "For odd values of n, every negative number has a real negative nth root" (http://en.wikipedia.org/wiki/Nth_root). And it's not correct to spaculate that "(-8)^(1/3)=(-8)^(2/6)=((-8)^2))^(1/6)=64^(1/6)=2". This speculation is a sort of sophism that masks the fact that there are more than one nth roots. When you make any transformations of powers you must take into account the possible loss of some roots or introduction of new roots. The correct result of "sqrt(x)" should be a multitude of two (possibly complex) numbers, not a single number. As we cannot return such a result in a spreadsheet we agree to see only positive answer, but it doesn't mean that we don't deserve to get correct negative real number as a cube root of a negative number.

To be specific: x != sqrt(x^2), because, strictly speaking, sqrt(x^2) = {x, -x}. So (-8)^(1/3) = (-8)^(2/6) [because you calculate 2/6 _before_ you calculate the exponent - brackets specify the order] != ((-8)^2))^(1/6), because here in the last case the base is raised to 2nd power and then the 6th root is taken, and the extra "2" brings new roots here (positive ones).

[Workaround] for those who want to find the cube root:
SIGN(X)*ABS(X)^(1/3)"

Still present in LO 4.1.2.1 under Win7x64.
Comment 1 tommy27 2013-09-26 19:15:25 UTC
set status to NEW because of confirmation in old OOo bug report cited in previous comment.
Comment 2 Commit Notification 2015-02-13 10:58:33 UTC
Supreme Aryal committed a patch related to this issue.
It has been pushed to "master":

http://cgit.freedesktop.org/libreoffice/core/commit/?id=b0fdf6cf11ee46d46925e9cd9fa2768a1b49bb64

Compute n-th root of negative numbers where n is odd. (tdf#69293)

It will be available in 4.5.0.

The patch should be included in the daily builds available at
http://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
http://wiki.documentfoundation.org/Testing_Daily_Builds
Affected users are encouraged to test the fix and report feedback.
Comment 3 Jean-Baptiste Faure 2021-08-12 15:18:54 UTC
The definition of (-8)^(1/3) is not the cubic root of -8, it is exp((1/3)*ln(-8)) which is not defined as ln(-8) is not defined. Making a special evaluation of a^b when b = 1/n with n equals to an odd integer, produces inconsistency with the mathematical definition of a^b.

> And it's not correct to speculate that "(-8)^(1/3)=(-8)^(2/6)=((-8)^2))^(1/6)=64^(1/6)=2".
 
I disagree: this "speculation" proves that the exponentiation function with 1/n exponent, matches the n-root only for positive numbers. You can not accept the theorem a^(b*c) = (a^b)^c in case a > 0 and refuses it in case a < 0 if you accept this case. 

Additionally : (-8)^(1/3) = (-8)^(2/6) = ((-8)^(1/6))^2 which is impossible because a negative number does not have n-root for n even.

So using exponentiation function with a negative base is inconsistent with commutativity and associativity of multiplication and thus is a non-sense. I am sorry but Calc was correct before you introduce the same bug as in Excel.

If we really need to compute n-root for n odd integer it would be better to define a function nroot(x,n) defined as x^(1/n) if x > 0 and -((-x)^(1/n)) if x < 0

If we really need to reproduce this MS-Excel bug, we should isolate it in some optional compatibility package, so that users who want do correct mathematics or do not import xlsx files are not affected by this bug.

Best regards. JBF