本文简要介绍ruby语言中 Integer.sqrt
的用法。
用法
sqrt(numeric) → integer
返回非负整数 n
的整数平方根,它是小于或等于 numeric
平方根的最大非负整数。
Integer.sqrt(0) # => 0
Integer.sqrt(1) # => 1
Integer.sqrt(24) # => 4
Integer.sqrt(25) # => 5
Integer.sqrt(10**400) # => 10**200
如果 numeric
不是 Integer,则将其转换为 Integer:
Integer.sqrt(Complex(4, 0)) # => 2
Integer.sqrt(Rational(4, 1)) # => 2
Integer.sqrt(4.0) # => 2
Integer.sqrt(3.14159) # => 1
该方法等价于Math.sqrt(numeric).floor
,只是由于浮点运算精度有限,后一种代码的结果可能与真实值不同。
Integer.sqrt(10**46) # => 100000000000000000000000
Math.sqrt(10**46).floor # => 99999999999999991611392
如果 numeric
为负数,则引发异常。
相关用法
- Ruby Integer.self >=用法及代码示例
- Ruby Integer.self >>用法及代码示例
- Ruby Integer.self ** numeric用法及代码示例
- Ruby Integer.self <=>用法及代码示例
- Ruby Integer.self % other用法及代码示例
- Ruby Integer.self / numeric用法及代码示例
- Ruby Integer.succ用法及代码示例
- Ruby Integer.self * numeric用法及代码示例
- Ruby Integer.self < other用法及代码示例
- Ruby Integer.self & other用法及代码示例
- Ruby Integer.self ^ other用法及代码示例
- Ruby Integer.self >用法及代码示例
- Ruby Integer.self | other用法及代码示例
- Ruby Integer.self[offset]用法及代码示例
- Ruby Integer.self ==用法及代码示例
- Ruby Integer.size用法及代码示例
- Ruby Integer.self <=用法及代码示例
- Ruby Integer.self << count用法及代码示例
- Ruby Integer.self - numeric用法及代码示例
- Ruby Integer.self + numeric用法及代码示例
- Ruby Integer.nobits?用法及代码示例
- Ruby Integer.next用法及代码示例
- Ruby Integer.truncate用法及代码示例
- Ruby Integer.bit_length用法及代码示例
- Ruby Integer.floor用法及代码示例
注:本文由纯净天空筛选整理自ruby-lang.org大神的英文原创作品 Integer.sqrt。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。