Author mark.dickinson
Recipients AjithRamachandran, lemburg, mark.dickinson, rhettinger, serhiy.storchaka, stutzbach
Date 2021-06-09.12:28:08
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
> Perhaps because it is so easy to write x**(1/3), and if you want a real root of negative argument, it is -(-x)**(1/3).

I consider `x**(1/3)` to be a trap and a bug magnet: many people won't realise that the correct spelling for their situation is actually `def f(x): return -((-x)**(1/3)) if x < 0 else x**(1/3)` and the failure mode of `x**(1/3)` for negative `x` seems suboptimal, in that it happily returns a complex number rather than raising a `ValueError` with a clear message indicating what was wrong. I've seen a good number of Stack Overflow questions from users confused about this exact thing.

And not only is that correct spelling unwieldy, but it _still_ doesn't do the right thing for `-0.0`, and if you care about getting that corner case right then the right spelling becomes even more unwieldy.

Of course, even with `math.cbrt` in the standard library people will still fall into the `x**(1/3)` trap, but at least we then have a decent replacement to point them to.
Date User Action Args
2021-06-09 12:28:08mark.dickinsonsetrecipients: + mark.dickinson, lemburg, rhettinger, stutzbach, serhiy.storchaka, AjithRamachandran
2021-06-09 12:28:08mark.dickinsonsetmessageid: <>
2021-06-09 12:28:08mark.dickinsonlinkissue44357 messages
2021-06-09 12:28:08mark.dickinsoncreate