Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sin(π()) != 0 #148

Closed
baryon opened this issue Feb 9, 2018 · 2 comments
Closed

sin(π()) != 0 #148

baryon opened this issue Feb 9, 2018 · 2 comments

Comments

@baryon
Copy link

baryon commented Feb 9, 2018

the result of sin(π) is not zero.
sin(π()) = 1.22464679914735e-16

@davedelong
Copy link
Owner

Sadly, this is a duplicate of issue #99. There’s not much I can do about it without a whole new math library (see issue #111).

@Roman-Kerimov
Copy link

Roman-Kerimov commented Mar 19, 2019

This can be fixed so:

extension Double {
    func isDivisible(by value: Double) -> Bool {
        let decimalNumberFormatter = NumberFormatter.init()
        decimalNumberFormatter.numberStyle = .decimal
        return decimalNumberFormatter.string(from: .init(value: self/value))?.contains(decimalNumberFormatter.decimalSeparator) == false
    }
}

func sin(_ value: Double) -> Double {
    return value.isDivisible(by: .pi) ? 0 : Foundation.sin(value)
}
    
func cos(_ value: Double) -> Double {
    return (.pi/2+value).isDivisible(by: .pi) ? 0 : Foundation.cos(value)
}
    
func tan(_ value: Double) -> Double {
    return value.isDivisible(by: .pi) ? 0 : (.pi/2+value).isDivisible(by: .pi) ? .infinity : Foundation.tan(value)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants