truncatingRemainder vs remainder in Swift

后端 未结 2 1357
Happy的楠姐
Happy的楠姐 2021-02-08 05:53

Swift\'s substitutes for the % symbol for floating point numbers:


func truncatingRemainder(dividingBy other: Self) -> Self 
相关标签:
2条回答
  • 2021-02-08 06:33

    truncatingRemainder computes the remainder of the "truncating division", and remainder computes the remainder of the "rounding division".

    Example (from the API reference):

    let x = 8.625
    let y = 0.75
    

    Truncating division and remainder:

    let q1 = (x/y).rounded(.towardZero)
    let r1 = x.truncatingRemainder(dividingBy: y)
    
    print(q1, r1)      // 11.0 0.375
    print(q1 * y + r1) // 8.625
    

    Rounding division and remainder:

    let q2 = (x/y).rounded(.toNearestOrEven)
    let r2 = x.remainder(dividingBy: y)
    
    print(q2, r2)      // 12.0 -0.375
    print(q2 * y + r2) // 8.625
    

    So in any case, the remainder rem of a division x by y is

    rem = x - quot * y
    

    where quot is "a rounded quotient" of the division x by y.

    For truncatingRemainder, quot is the quotient rounded towards zero, and for remainder, quot is the quotient rounded towards the nearest integer.

    The result of truncatingRemainder has always the same sign as the dividend, this need not be the case for remainder.

    If both x and y are exactly representable as an integer then the result of

    x.truncatingRemainder(dividingBy: y)
    

    is the same as

    Int(x) % Int(y)
    

    with the integer remainder operator %.

    0 讨论(0)
  • 2021-02-08 06:39

    This looks like a job for...playgrounds!

    let thing: Float = 8126.84652
    let truncating = thing.truncatingRemainder(dividingBy: 10) // value is 6.84668, analog to %
    let nonTruncating = thing.remainder(dividingBy: 10) // -3.15332
    

    As you can see, the plain remainder is allowed to go negative, (or rather, subtracts until the value is <=0) whereas the truncating remainder will remain positive. (stops when the value is >=0)

    0 讨论(0)
提交回复
热议问题