简体   繁体   English

如何在Ruby中将数字提高到1 / n的幂?

[英]How to raise a number to a power of 1/n in Ruby?

I just want to raise a number to the power of 1/n. 我只想将数字提高到1 / n的幂。 If I do this way: 2**(1/7), I alway get 1, regardless of n; 如果我这样做:2 **(1/7),无论n,我总是得到1; and this only ocurrs when the dividen is 1. 并且仅在除数为1时出现。

Thanks in advance. 提前致谢。

Run 1/7 in a console and see what you get. 在控制台中运行1/7 ,看看会得到什么。

Then try 1.0 / 7.0 , to avoid "integer division." 然后尝试1.0 / 7.0 ,以避免“整数除法”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM