generics - How to use sin(_ : ) with a FloatingPoint value in Swift 3 -


 import foundation  public func sine <t: floatingpoint   > (_ x: t  ) -> t{     return sin(x)  }  // error: cannot invoke 'sin' argument list of type '(t)' 

is there way around this? many thanks.

you can make sin method accepts floatingpoint type follow:

import uikit  func sin<t: floatingpoint>(_ x: t) -> t {     switch x {     case let x double:         return sin(x) as? t ?? 0     case let x cgfloat:         return sin(x) as? t ?? 0     case let x float:         return sin(x) as? t ?? 0     default:         return 0 t     } } 

another option adding method or computed property extension floatingpoint type follow:

extension floatingpoint {     var sin: self {         switch self {         case let x double:             return uikit.sin(x) as? self ?? 0         case let x cgfloat:             return uikit.sin(x) as? self ?? 0         case let x float:             return uikit.sin(x) as? self ?? 0         default:             return 0 self         }     } } 

Comments