Warning about truncatingRemainder

it may not deliver what you expect. For example:

if you are using NASA/JPL/Standish equations of heliocentric planetary motion, it requires:

"3. Modulus the mean anomaly (M) so that -180deg<=M<=+180deg and then obtain the eccentric anomaly, E, from the solution of Kepler's equation..."

If you attempt to use the Swift "Modulo operator, %" you'll get the Xcode directive to use truncatingRemainder instead. Which IS better than % because of this note in "The Swift Programming Language (Swift 5.3)":

"“NOTE

The remainder operator (%) is also known as a modulo operator in other languages. However, its behavior in Swift for negative numbers means that, strictly speaking, it’s a remainder rather than a modulo operation.”

Excerpt From The Swift Programming Language (Swift 5.3) Apple Inc. https://books.apple.com/us/book/the-swift-programming-language-swift-5-5/id881256329 This material may be protected by copyright.

But you should try truncatingRemainder in your playground! Let: 0deg = 12 O'clock, 180/-180deg = 6 O'clock, -90deg = 9 O'clock and +90deg = 3 O'clock

Now run this on your playground:

let M    = -181.25
var M180 = M
while M180 >  180.0 { M180 -= 360.0 }
while M180 < -180.0 { M180 += 360.0 }
print("M = \(M)\t\tM180 = \(M180)\t\tM.truncatingRemainder(dividingBy: 180.0) = \(M.truncatingRemainder(dividingBy: 180.0))\t\tM.truncatingRemainder(dividingBy: 360.0) = \(M.truncatingRemainder(dividingBy: 360.0))")

and you'll get this result:

M = -181.25        M180 = 178.75 M.truncatingRemainder(dividingBy: 180.0) = -1.25 M.truncatingRemainder(dividingBy: 360.0) = -181.25

Thus, truncatingRemainder(dividingBy: 180.0) puts your planet 180deg away from where it should be, while M180 keeps the planets moving without quantum leaps.