How REST function in Numbers works in Swift

时间:2017-08-05 12:12:10

标签: swift apple-numbers

In Apple Numbers the MOD function differs from Swift (in the German version it is REST.

In Numbers:

4,37937=MOD(−1,90373;6,2831)

versus

In swift 3:

let rem1: Double = -1.90373
let rem = rem1.truncatingRemainder(dividingBy: 6.28318530717959)
print(rem)

Prints: -1.90373

What I am doing wrong?

2 个答案:

答案 0 :(得分:1)

我找到了解决方案:

let rem1: Double = -1.90373
let rem = rem1 - 6.28318530717959 * floor(rem1 / 6.28318530717959)
print(rem)

会像Apples Numbers MOD那样做。

答案 1 :(得分:0)

a%b 执行以下操作并返回余数

a = (b x some multiplier) + remainder

某个乘数是b中适合a的最大倍数。

e.g。一些整数常量[0 ...]

documentation提供以下内容作为示例

  

将-9和4插入等式中得出:

     

-9 =(4×-2)+ -1

     

给出余数值-1。

     

对于b的负值,忽略b的符号。这意味着一个   %b和%-b总是给出相同的答案。