Swift 4.2 Xcode 10β2
Background
I'm in the process of putting together an astronomy related API to handle the maths in an app I'm working one. The intention was to use the Measurement
class to handle the input and output of angles and distances. In this way, the user could enter data using their preferred units (e.g degrees or radians, miles or kilometers or any combination) and get output in the same units.
This appeared to work really well until I tested the results. On a relatively simple calculation like the distance from the centre of the Earth at a given latitude I was getting errors in the order of two kilometers.
It took a couple of hours (because I assumed it was rounding errors in the calculations) but I eventually traced the problem back to the Measurment
class itself. The conversion factor between degrees and radians is inaccurate. It should be 180.0 / Double.pi
(57.29577951308232...) but is 57.9528.
Code example
print(UnitAngle.radians.converter.baseUnitValue(fromValue: 1.0)) // Inaccurate.
print(180.0 / Double.pi) // Accurate.
Questions
So, two (ok three) questions:
- Is this a bug? It must be a bug. It's far too inaccurate.
- Is it possible to set the conversion factor to a different value (or subclass
Measurement
to achieve the same). - Should I roll my own Measurement class or just abandon the idea of unit conversion and stick to SI units internally? (Would would be sad because, otherwise, it does exactly (sic) what I want.)