take the sine of a number in swift

To take the sine of a number in Swift, you can use the built-in sin() function from the Foundation module. Here's an example:

main.swift
import Foundation

let angle: Double = 45 // in degrees
let radians = angle * Double.pi / 180 // convert to radians
let sine = sin(radians)

print(sine) // Output: 0.7071067811865475
183 chars
8 lines

In this example, we first import the Foundation module. Then we define an angle in degrees (in this case, 45), and convert it to radians using the formula radians = angle * pi / 180. Finally, we call the sin() function with the angle in radians as its argument, and save the result to a constant called sine. We then print the result to the console.

related categories

gistlibby LogSnag