本文簡要介紹ruby語言中 Math.sqrt
的用法。
用法
sqrt(x) → Float
返回 x
的非負平方根。
域:[0, INFINITY)
共域:[0,無窮大)
0.upto(10) {|x|
p [x, Math.sqrt(x), Math.sqrt(x)**2]
}
#=> [0, 0.0, 0.0]
# [1, 1.0, 1.0]
# [2, 1.4142135623731, 2.0]
# [3, 1.73205080756888, 3.0]
# [4, 2.0, 4.0]
# [5, 2.23606797749979, 5.0]
# [6, 2.44948974278318, 6.0]
# [7, 2.64575131106459, 7.0]
# [8, 2.82842712474619, 8.0]
# [9, 3.0, 9.0]
# [10, 3.16227766016838, 10.0]
請注意,浮點運算的有限精度可能會導致令人驚訝的結果:
Math.sqrt(10**46).to_i #=> 99999999999999991611392 (!)
另請參見 BigDecimal#sqrt
和 Integer.sqrt
。
相關用法
- Ruby Math.sinh用法及代碼示例
- Ruby Math.sin用法及代碼示例
- Ruby Math.acosh用法及代碼示例
- Ruby Math.asinh用法及代碼示例
- Ruby Math.cbrt用法及代碼示例
- Ruby Math.log10用法及代碼示例
- Ruby Math.atan用法及代碼示例
- Ruby Math.asin用法及代碼示例
- Ruby Math.gamma用法及代碼示例
- Ruby Math.erf用法及代碼示例
- Ruby Math.cosh用法及代碼示例
- Ruby Math.tan用法及代碼示例
- Ruby Math.erfc用法及代碼示例
- Ruby Math.tanh用法及代碼示例
- Ruby Math.lgamma用法及代碼示例
- Ruby Math.frexp用法及代碼示例
- Ruby Math.exp用法及代碼示例
- Ruby Math.ldexp用法及代碼示例
- Ruby Math.acos用法及代碼示例
- Ruby Math.cos用法及代碼示例
- Ruby Math.atan2用法及代碼示例
- Ruby Math.atanh用法及代碼示例
- Ruby Math.log2用法及代碼示例
- Ruby Math.hypot用法及代碼示例
- Ruby Math.log用法及代碼示例
注:本文由純淨天空篩選整理自ruby-lang.org大神的英文原創作品 Math.sqrt。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。