本文簡要介紹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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
