2

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?

SteffenH
  • 29
  • 6

2 Answers2

1

I found the solution:

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

will do the same like Apples Numbers MOD is doing.

SteffenH
  • 29
  • 6
0

a % b performs the following and returns remainder

a = (b x some multiplier) + remainder

some multiplier is the largest number of multiples of b that will fit inside a.

e.g. some integer constant [0...]

The documentation provides the following as an example

Inserting -9 and 4 into the equation yields:

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

giving a remainder value of -1.

The sign of b is ignored for negative values of b. This means that a % b and a % -b always give the same answer.

n3wb
  • 1,037
  • 5
  • 12
  • 20