### Cube root of negative number is nan

Apologies if this has been brought up before. I’m doing a series of calculations in my patch and stumbled across this problem. Upon further testing, it seems to only be caused by negative numbers. The example I’ve pasted is the cube root of -8 and should give -2 as the answer. However, it’s just giving me nan…

<code>-- Pasted Max Patch, click to expand. --Copyallof the following text. Then, in Max, selectNew From Clipboard.----------begin_max5_patcher---------- 377.3ocwS9sSCBCEF+Z3onowKzDFR4OaLuymCmwzAms0EnPncanK6cW5AXST WxTWh2zjy24zS+N+Z6daK57hZPQIOPdhXYs21xBkLBVcwVzbdcRFWgkQyAkh uDnNs4zPsF0kbYu1hBoVIdCL5LeWuNY4lbgLCzXe7OIVrQ2qx5TakzuVBsVi RIO2kpjqSVIjKeoBRzsYYQgtQNDVfeTygQh7LqrXWuiaRjhdrX95Q9AS+nOk 7b7LnOVI3YTShC11lEm+HPFE+uwi3HjG9iYFRD3iQmkGStt7PB6Z56WvATWV QJK1c6MKXyl4z3r6Cbu6mfndXTVAJPp4ZQgbvTyZm53Ilo1qa43LeUI6Te7M FCOJeuyg1vKCs3loYB4m+JhVvnOj2phMUI8F93kH4jIRAkVHQBMnpvAUsRjl BXA87NUn3yy.bD79169K2QgWjiZ9LRX+BG0hLdY4VnR00TzLMOFWWTYBG6fg BYaH1QZErUzWersoaGreG.UQlrN -----------end_max5_patcher-----------</code>

If it’s of any use. I’m running Max 6.1.2 (32-bit) on an iMac, mid 2011, OSX Lion 10.7.5

you must think of [expr] as an algebraic object just like [sqrt].

to get the imaginary root of negative numbers, try this:

[expr (pow(((abs($f1)*($f10)))\,(1./3.)))]

-110

Couldn’t get your example to work but you reminded me that I could just use [abs] haha. Will paste the solution for anybody who wants to see. Cheers

<code>-- Pasted Max Patch, click to expand. --Copyallof the following text. Then, in Max, selectNew From Clipboard.----------begin_max5_patcher---------- 434.3ocwTErTCBCD8L7UDy3AclVjPrzVu42g0wIsjpoCMgII0h1w+cCa.qUE kwohWHrurYyiGuc2EFfmqJ4FL5JzMnffcgAA.TEPPcb.dMqbQNy.ogWyMF18 b7.+dVdoEvGlD0fI2rVHy4V3.I0fKURqQ7LuBijDEWCq1XcYZepf6IAFitce Y76B0gTiVvrKdPHu+NMeg0eFZ7DW8PITXYTb0Sh682pjHCnnZ9pgDJ9cDRxV C2K9Zsfkiq13kvvpGC5nbH4ack8SpwICQwGG8XYthY+MhBcJnCSgEZRzHmBE 2hnP5EQgWVnQEpsmc5RxrYCHQwWPiN+KUIRO3ZpEnwDXgLF7PsnPIzK6EIhM 2zlwgzyFG+xkzuSVROthRqyVl7uMZwqBSR22D01jkNNXANJNWH+3XW31qvOT fLpM5EMeAMMqn8LHiarBIyJTx2mjiMHxaI8fHKiC62HNYBCadNGne7W9qpq7 AZM9YBcHq+KIDsCzIs2XSZGXyGkvtyGuahUT7HWapKJPEWi0JktJLc.DJj9P vri07GEM4CsQXl148sNi+Fs24VNI04ac2yKgulDebKD -----------end_max5_patcher-----------</code>

ok this is really weird.

the new forum software does not let me write the correct code, it shows something different from what was originally in my clipboard and the edit formular.

the reason are obviously the "greater than" and "smaller than" symbols, which are recognized as html tag when used in this order.

[code] [expr (pow(((abs($f1)*($f10))),(1./3.)))] [/code]

[expr (pow(((abs($f1)*($f1smaller0) + ($f1)*($f1greater0))),(1./3.)))]

-110

the last line is the correct expression when you replace the operators in max. ;)

the last line is the correct expression when you replace the operators in max. ;)

I’m getting a positive number out instead of a negative one with your new expression. The patch I pasted earlier works though.

Just realise that my method will give a negative output for a positive input.

Here’s the new patch:

<code>-- Pasted Max Patch, click to expand. --Copyallof the following text. Then, in Max, selectNew From Clipboard.----------begin_max5_patcher---------- 552.3ocwV1saaCBEG+Z6mBOztXSx0yf+HI6hIsmi0oJRLIkJavBHMdqpu6Ce LowoMIyKyy6FPb3vg+7yGN3m78PKkMLMJ3yAeKvy6IeOOvTqAO2XOTEsYUIU CtgpXZMcCCE1Mmg0X.6y2aQrshKJYFvchy3Zovn4+j0ZCShhclkaMVOM+nl0 IADJ36GBS2rPbvNq0Typ64hM2oXqLcqAOOyFuf7z11DRTVn0TT7KAhW.5St7 gaVf5IGAsB1UzWUbZIpchm88aaB+KQAI5+FKx.JPRWz1kE21dNXLabgwF8Nt UODzKpSYiogotiInKKgnGeIrvEl9mt2hivyiDxEPRdVGEvP9wBWVxIQRx0bv Erc109ljfuDDONoA.W9iyDHwvMgEyNbqfDe5SMdbSDNCOd2MiEPVWJoWCRbY BDBd.LYZfBqoVETK28g2uFe6sgVg8ojnOdRJgmrJo3rDnCC4NmiPoSBfnK0m KsAOUoMNpP.bjlbInjOLn.KEUxEu9oWX6aseLozxspU6OGtGxBNHfBl1vETC WJ54S9Q9bOunfI5WDtfqaqKChO9jewFS0fmL0jX2I7uQMyNxm+kpAiG.blN4 jN.0jLYpIYHINSWlCdfv4RpohWTKsOY6tXSfJR1hGccyRgeOkz++OFO8mO.8 mdkzrqjEst9QlR6BIHDaY7Gjp1g4gvPtnaHTREoXOx26O7POhprUXM1xqaUc kGalmaKNZ2mm8+0zWmsS -----------end_max5_patcher-----------</code>

oops, my fault, of course it must be negative:

[expr -(pow(((abs($f1)*($f1smaller0) + ($f1)*($f1greater0)))\,(1./3.)))]

our communication is heavily distorted because i see new posts (including my own) always 3 minutes after they have been posted.

for example i saw you mentioning your "patch above" before the post with the patch was actually there. wordpress does not seem to queue post.

Yours now outputs a negative when there’s a positive input. I forgot to mention that I’m using positive and negative numbers here, sorry. My solution works but is clearly a bit messy.

First of all, pow(x,1.0/3.0) is not a cube root, nor is 1.0/3.0 exactly one third. The latter is a binary approximation of one third, just like 0.333333 is an approximation of one third. Close, but not the same thing.

pow(x,1/3) will give a result very close to the cube root for positive numbers, generally accurate to about 20-some-odd decimal places. But to make the calculation, pow() has to take the natural logarithm of *x*. (The pow() function works generally by taking the natural logarithm of the first parameter, multiplying that by the second parameter, and raising *e* to that value for the result. Just like how you learned logarithms in, oh, about ninth grade math). The natural logarithm of a negative value is undefined, so of course pow() returns NaN.

The straightforward solution can be something like this:

<code>-- Pasted Max Patch, click to expand. --Copyallof the following text. Then, in Max, selectNew From Clipboard.----------begin_max5_patcher---------- 439.3ocyTtsSCCCCF951mhnHtXf5JI8vXBIPhmCFBks4xBpKopI6.Ls2cZca gMXCMfBraZjcri+yWsyJWG5P8RvPIWRtk33rx0wAcU5vo11gNUrbTpvfgQUv B8vGodUaYgkVz8YDleiS0ro5Y1TvhYvq8V4x9TFTUNZRpVTj7c06mIrilHUO beNLxVERTOelGgyivk9A9wdj.lO60bjiwhWHntwMUOQqrJwTrJzaxkhzMzkT 0HqfMB2HeFCmG3yJ8t10s7i2OCIxDxII7quhQrS.EgSfTCP5x+hX5SIT.mUx lKp.UXX4x9HTzWjP71iPSAiQ7.7AD0MvuMoAOFwPL1uDyp5Z1ML3+esK6EF8 aSVTO6fDIDmb1GJBN5lbfkY4jL8hNhglNEyPmNXfG2mcdQ68osOipGdh4e1v SX6O7fkglJUu+AXTok92lbF8r7QM2q5m7HuI1wfwJUBqTq1Hlh97hWdZhYhb 7X.2l0jkzHFlB3Ec2+AOT0Db.pIZqX9MUSzAnl3+L1Ddfpgcz7mJ7aplplZQ V1bH2TejnPJF7eTmWZ1yCMkpJS7Do4vbYS78cKOs0tu.VwZ8tC -----------end_max5_patcher-----------</code>

For full details on pow() how reacts in unpleasant circumstances, consult the man page, for instance here. The key clause is:

"If x is a finite value less than 0, and y is a finite noninteger, a domain error occurs, and a NaN is returned."

Forums > MaxMSP