Make Swift Assume Degrees for Trigonometry Calculations

后端 未结 7 2435
抹茶落季
抹茶落季 2020-12-06 14:01

Is it possible to change a setting, property, etc in Swift for iOS so that it assumes degrees for trigonometry calculations rather than radians?

For example si

7条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-06 14:13

    Adding an extension to identify the kind of value clearly would be a appropriate way to handle such a thing:

    import Darwin // needed to get M_PI
    extension Double {
      public var degrees: Double { return self * M_PI / 180 }
      public var ㎭: Double { return self * 180 / M_PI }
    }
    

    Pop that into a playground and see how you get the results you expect:

    sin(90.degrees)  --> 1.0
    1.㎭  -->  57.2957795130823
    1.㎭.degrees --> 1.0
    (M_PI / 3).㎭  -->  60.0
    

提交回复
热议问题