'Information Java % symbol to convert in swit
I found in SO a following formula wrote in Java,
double longitudeResult = (lonRad + a + 3 * PI) % (2 * PI) - PI;
I have zero experiance on Java, what that percentage mean??
is that a divided? how to convert in swift?
this my code in swift but it is wrong.. I don't understand if that is a divide multiply or what?
let lonResult = (lonRad + a + 3 * Double.pi) / (2 * Double.pi) - Double.pi // IS wrong
Solution 1:[1]
It's 'modulo'. It is the same as integer division, except, it resolves to the remainder and throws away the result.
int x = 10 / 3;
assert x == 3;
int y = 10 % 3;
assert y == 1;
x is 3, because 3 fits into 10... 3 times; the remainder (1) is thrown away.
y is 1 because 3 fits into 10.... 3 times, which is thrown away; the remainder (1) is returned.
Modulo is used a lot in cryptography, though in this case its used in geometry calculations. The 'point' is the same:
Turn a 'number line' (a.k.a. a Dimension) from an infinitely sized flat line into a circle: If you want to do math in a world where, say, 2? is simply the largest number, there is nothing larger than it; any attempt to add to 2? just loops around to -2?, as if the numbers line was a circle: Then modulo is how to do that.
That's exactly the point of this 'math' and it's also why crypto uses it so much. Crypto works by doing repetitive complex operations a lot but to keep the sizes of numbers that fall out manageable, they're done on a looped numbers line.
Incidentally, swift has the exact same operation (swift has 'modulo') using the exact same symbol (%). In fact, most programming languages do.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | rzwitserloot |
