Mathematical Functions in Swift

Mathematical functions in Swift

As other noted you have several options. If you want only mathematical functions. You can import only Darwin.

import Darwin

If you want mathematical functions and other standard classes and functions. You can import Foundation.

import Foundation

If you want everything and also classes for user interface, it depends if your playground is for OS X or iOS.

For OS X, you need import Cocoa.

import Cocoa

For iOS, you need import UIKit.

import UIKit

You can easily discover your playground platform by opening File Inspector (⌥⌘1).

Playground Settings - Platform

How does Swift deal with constant values into mathematical functions?

The compiler is likely to inline the call, and once the call is inlined, it's also likely to perform constant-folding in order to do the calculation at compile-time.

So yes, with optimizations enabled, it's reasonably likely that radian(360) will be compiled into 6.283184. But the only way to verify this is to actually look at the resulting assembly of an optimized build.

If you want to make this even more likely, you can stick the undocumented @inline(__always) attribute on your function. That will force it to always inline the function. But this is probably unnecessary.

Wanting to generate a random maths function using swift?

One way to do this is to correspond a number with each operator:

let operator = Int.random(in: 0..<4)
let number1 = Int.random(in: 0..<10)
let number2 = Int.random(in: 0..<10)
switch operator {
case 0: question = "\(number1) + \(number2)"
case 1: question = "\(number1) - \(number2)"
case 2: question = "\(number1) / \(number2)"
case 3: question = "\(number1) * \(number2)"
}

Seeing that this is intended for little kids, you can be a little smart when you generate questions. Using a simple approach like the above might result in questions like "5 / 3" which might confuse some kids who does not know about decimal numbers. So you should probably put the logic to generate each type of question into their own cases.

For subtraction questions, you can check which number is larger first. Then arrange them appropriately to avoid the result being a negative number.

For division questions, you can generate two numbers, and produce a question with the product of the two numbers being divided by one of the numbers. This way you guarantee an integer solution. Something like this:

let number1 = Int.random(in: 0..<10)
let number2 = Int.random(in: 0..<10)
let product = number1 * number2
question = "\(product) / \(number1)"

Want to use cos^-1 in Swift

acos() and other mathematical functions from the BSD library can be used directly from Swift. Example:

import Darwin // or Foundation

let x = 0.0
let angle = acos(x)

print(angle) // 1.5707963267948966 (radians)
print(angle * 180.0 / .pi) // 90.0 (degrees)

In a future version of Swift these functions are also available as static functions on the floating point types, compare SE-0246 Generic Math(s) Functions

let x = 0.0
let angle = Double.acos(x)

print(angle) // 1.5707963267948966 (radians)
print(angle * 180.0 / .pi) // 90.0 (degrees)

Swift - playground: mathematical functions ambiguous use of tan

You have already found a solution, but as your question was

Why is this not working?

I'll try to add an explanation for the problem.

Like most mathematical functions, tan() is overloaded for the various
floating point types:

public func tan(x: Float) -> Float
public func tan(_: Double) -> Double
public func tan(x: CGFloat) -> CGFloat

All these floating point types conform to the IntegerLiteralConvertible
protocol, which means that 2 can be interpreted as a
Float, Double, or CGFloat. Therefore, in

let mytan = tan(2)

the compiler cannot decide which one to use:


error: ambiguous use of 'tan'
let mytan = tan(2)
^
Darwin.tan:2:13: note: found this candidate
public func tan(x: Float) -> Float
^
Darwin.tan:1:13: note: found this candidate
public func tan(_: Double) -> Double
^
CoreGraphics.tan:2:13: note: found this candidate
public func tan(x: CGFloat) -> CGFloat

On the other hand, a floating point literal like 2.0 is by default
interpreted as a Double, and that is why

let mytan = tan(2.0)

compiles.



Related Topics



Leave a reply



Submit