實例方法
remainder(dividing
remainder(dividingBy:)
返回此值除以給定值的餘數。
聲明
func remainder(dividingBy other: Self) -> Self
返回值
該值的餘數除以 other
。
參數
other
除此值時要使用的值。
詳述
對於兩個有限值 x
和 y
,將 x
除以 y
的餘數 r
滿足 x == y * q + r
,其中 q
是最接近 x / y
的整數。如果x / y
恰好在兩個整數之間,則選擇q
為偶數。請注意,q
是 not
x / y
以浮點算術計算的,並且 q
可能無法以任何可用的整數類型表示。
以下示例計算 8.625 除以 0.75 的餘數:
let x = 8.625
print(x / 0.75)
// Prints "11.5"
let q = (x / 0.75).rounded(.toNearestOrEven)
// q == 12.0
let r = x.remainder(dividingBy: 0.75)
// r == -0.375
let x1 = 0.75 * q + r
// x1 == 8.625
如果此值和 other
是有限數,則餘數在封閉範圍內 -abs(other / 2)...abs(other / 2)
。 remainder(dividingBy:)
方法總是準確的。此方法實現由 IEEE 754 specification 定義的餘數運算。
可用版本
iOS 8.0+, iPadOS 8.0+, macOS 10.10+, Mac Catalyst 13.0+, tvOS 9.0+, watchOS 2.0+
相關用法
- Swift Float rounded(_:)用法及代碼示例
- Swift Float radix用法及代碼示例
- Swift Float random(in:using:)用法及代碼示例
- Swift Float random(in:)用法及代碼示例
- Swift Float round(_:)用法及代碼示例
- Swift Float -(_:)用法及代碼示例
- Swift Float ...(_:_:)用法及代碼示例
- Swift Float init(integerLiteral:)用法及代碼示例
- Swift Float init(_:)用法及代碼示例
- Swift Float ..<(_:)用法及代碼示例
- Swift Float init(signOf:magnitudeOf:)用法及代碼示例
- Swift Float +(_:_:)用法及代碼示例
- Swift Float formTruncatingRemainder(dividingBy:)用法及代碼示例
- Swift Float isEqual(to:)用法及代碼示例
- Swift Float /(_:_:)用法及代碼示例
- Swift Float exponent用法及代碼示例
- Swift Float minimum(_:_:)用法及代碼示例
- Swift Float init(floatLiteral:)用法及代碼示例
- Swift Float init(sign:exponent:significand:)用法及代碼示例
- Swift Float maximum(_:_:)用法及代碼示例
- Swift Float magnitude用法及代碼示例
- Swift Float negate()用法及代碼示例
- Swift Float -(_:_:)用法及代碼示例
- Swift Float init(exactly:)用法及代碼示例
- Swift Float exponentBitCount用法及代碼示例
注:本文由純淨天空篩選整理自apple.com大神的英文原創作品 Float remainder(dividingBy:)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。