DDMathParser icon indicating copy to clipboard operation
DDMathParser copied to clipboard

sin(pi) != 0

Open Roman-Kerimov opened this issue 6 years ago • 3 comments

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)
}

Roman-Kerimov avatar Mar 26 '19 18:03 Roman-Kerimov

@Roman-Kerimov Was this you? If so, please get in touch if you're interested in involving in a project. I can't find your contact details anywhere. My apologies to the repository maintainer for an off-topic comment.

p-i- avatar May 26 '19 09:05 p-i-

@p-i- Yes it's me. I added mail to my profile.

Roman-Kerimov avatar May 26 '19 16:05 Roman-Kerimov

@Roman-Kerimov how safe is this approach? It works fine in my testing but using NumberFormatter for this seems sketchy.

aheze avatar Dec 22 '23 06:12 aheze