Why does math.Pow returns NaN when it should return an actual number?

blov · · 297 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p><a href="https://play.golang.org/p/f6cREuQaAv">Here</a>.</p> <p>Why does this equation returns NaN, when it should return −4,929420801 (according to my calculator).</p> <hr/>**评论:**<br/><br/>ibishvintilli: <pre><p>Apparently the result should be a complex number and should be handled with the mapth/cmplx package.</p> <p>fmt.Println(cmplx.Pow(-2910.579503363819, 0.2))</p> <p>result was: (3.987985200072245+2.897440848908454i)</p> <p>The result was the same as in wolfram alpha: <a href="https://www.wolframalpha.com/input/?i=(-2910.579503363819)%5E0.2" rel="nofollow">https://www.wolframalpha.com/input/?i=(-2910.579503363819)%5E0.2</a></p></pre>Swedophone: <pre><p>Actually Wolfram alpha lists all five 5th roots. One is &#34;-4.9294208005523940 + 0.×10<sup>-16</sup> i&#34;.</p></pre>KillerMcFluffy: <pre><p>(-2910.579503363819) ^ .02 = 1.17063569 + 0.0736501551 i according to Google </p> <p>You&#39;re essentially taking the (50th) root of a negative number, so you get an imaginary answer...... math.Pow returns a float64 so it&#39;s NaN</p></pre>KillerMcFluffy: <pre><p>you are doing this: -(2910.579503363819<sup>0.2)</sup> = -4.92942080055 on your calculator</p></pre>kerakk19: <pre><p>I&#39;m doing something like this: (-2910.579503363819)<sup>0.2</sup></p> <p>And the result is: −4,929420801</p> <p>And google shows: -4.9294208 <a href="https://www.google.pl/search?q=(-2910.579503363819)%5E0.2&amp;oq=(-2910.579503363819)%5E0.2&amp;aqs=chrome..69i57.2563j0j9&amp;client=ubuntu&amp;sourceid=chrome&amp;ie=UTF-8" rel="nofollow">Link</a></p></pre>KillerMcFluffy: <pre><p>sorry, my bad! I copied wrong.... technically both -4.9294.... and (3.987...+2.8974...i) are 5th roots of -2910.579...</p> <p>guessing that special case (that <a href="/u/davedev" rel="nofollow">/u/davedev</a> pointed out) exists because floating point math with fractional exponents isn&#39;t very precise, but not positive.</p></pre>markuspeloquin: <pre><p>.2 isn&#39;t representable with a base 2 floating point. The only way you can define exponentiation of a negative number to a non-integer is to convert the exponent to a rational number. If the exponent&#39;s denominator is positive, you get an imaginary number. But in base 2, denominators are <em>always</em> positive. Something like this can only be done via symbolic math.</p></pre>daveddev: <pre><p>It appears to be a special case.</p> <p><a href="https://golang.org/pkg/math/#Pow" rel="nofollow">https://golang.org/pkg/math/#Pow</a></p></pre>_0dd1sh: <pre><p>This is correct. To quote the last special case -</p> <blockquote> <p>Pow(x, y) = NaN for finite x &lt; 0 and finite non-integer y</p> </blockquote> <p>OP&#39;s x is less than zero, and his y is a non-integer.</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

297 次点击  
加入收藏 微博
0 回复
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传