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

Make tan function match sbcl #1046

Open
Izaakwltn opened this issue Jan 15, 2024 · 0 comments
Open

Make tan function match sbcl #1046

Izaakwltn opened this issue Jan 15, 2024 · 0 comments

Comments

@Izaakwltn
Copy link
Collaborator

Trigonometric:tan doesn't match SBCL:

coalton-user> (coalton (tan (Complex 1.0d0 -1.0d0)))
#C(0.2717525853195118 -1.0839233273386946)
NEW-TRIG> (cl:tan #C (1.0d0 -1.0d0))
#C(0.27175258531951174 -1.0839233273386943)

I made an attempt as seen below, though it still doesn't match exactly, and causes NaN errors during identity tests.

;; unused first attempt

(defpackage #:new-trig
  (:use #:coalton
        #:coalton-prelude)
  (:local-nicknames (#:math #:coalton-library/math)))

(in-package #:new-trig)

(coalton-toplevel

  (define (old-tan z)
    (let x = (real-part z))
    (let y = (imag-part z))
    (let c = (exp (complex y (negate x))))
    (let recip-c = (/ 1 c))
    ;; (i (e^(y - i x) - e^(-y + i x)))
    ;; / (e^(-y + i x) + e^(y - i x))
    (/ (* math:ii (- c recip-c)) (+ c recip-c))))

  (define (new-tan z)
    (let x = (real-part z))
    (let y = (imag-part z))
    (let e+y = (exp y))
    (let e-y = (exp (negate y)))
    (let a = (/ (* (+ e+y e-y) (math:sin x)) 2))
    (let b = (/ (* (- e+y e-y) (math:cos x)) 2))
    (let c = (/ (* (+ e+y e-y) (cos x)) 2))
    (let d = (/ (* (- e+y e-y) (sin x)) -2))
    (let denom = (+ (^ c 2) (^ d 2)))
    (Complex (/ (+ (* c a) (* b d))
                denom)
             (/ (- (* b c) (* a d))
                denom)))

;; NEW-TRIG> (coalton (old-tan (Complex 1.0d0 -1.0d0)))
;; #C(0.2717525853195118 -1.0839233273386946)
;; NEW-TRIG> (coalton (new-tan (Complex 1.0d0 -1.0d0)))
;; #C(0.2717525853195117 -1.0839233273386946)
;; NEW-TRIG> (cl:tan #C (1.0d0 -1.0d0))
;; #C(0.27175258531951174 -1.0839233273386943)
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

1 participant