;; -*-theme-d-*-

;; Copyright (C) 2018  Tommi Höynälänmaa
;; Distributed under GNU General Public License version 3,
;; see file doc/GPL-3.

;; Expected results: translation and running OK

(define-proper-program (tests test686)

  (import (standard-library core)
	  (standard-library math)
	  (standard-library extra-math)
	  (standard-library console-io)
	  (tests numerical-test-env))

  (define-simple-proc main (() <none> nonpure)

    (report-test (exp2 3) 8)
    (report-test (exp2 -4) (rational 1 16))
    (report-test (exp2 (rational 1 3)) 1.2599210498948732)
    (report-test (exp2 (rational -2 5)) 0.757858283255199)
    (report-test (exp2 (rational 3 1)) 8)
    (report-test (exp2 (rational -4 1)) (rational 1 16))
    (report-test (exp2 1.5) 2.8284271247461903)
    (report-test (exp2 (complex 1.5 -2.0))
		 (complex 0.5188946835878313 -2.780422325357118))
    (console-newline)

    (report-test (expm1 0) 0)
    (report-test (expm1 1) 1.718281828459045)
    (report-test (expm1 (rational 0 1)) 0)
    (report-test (expm1 (rational -4 5)) -0.5506710358827784)
    (report-test (expm1 1.1) 2.0041660239464334)
    (report-test (expm1 (complex -1.0 1.0))
		 (complex -0.801233889653587 0.3095598756531122))
    (console-newline)

    (console-display "(log2 0): ")
    (guard-without-result (exc (else (console-display "exception ")
				     (console-display-line exc)))
			  (console-display-line (log2 0)))
    (report-test (log2 1) 0)
    (report-test (log2 6) 2.58496250072116)
    (report-test (log2 -5)
		 (complex 2.321928094887362 4.532360141827194))
    (console-display "(log2 0/1): ")
    (guard-without-result (exc (else (console-display "exception ")
				     (console-display-line exc)))
			  (console-display-line (log2 (rational 0 1))))
    (report-test (log2 (rational 1 1)) 0)
    (report-test (log2 (rational 5 6)) -0.26303440583379384)
    (report-test (log2 (rational -9 4))
		 (complex 1.1699250014423124 4.532360141827194))
    (report-test (log2 2.5) 1.3219280948873624)
    (report-test (log2 -1.3)
		 (complex 0.37851162325372983 4.532360141827194))
    (report-test (log2 (complex -1.5 1.6))
		 (complex 1.1330184469976587 3.352747688996925))
    (report-test (log2 8) 3)
    (report-test (log2 (rational 1 16)) -4)
    (report-test (log2 (make <rational> 3 12)) -2)
    (console-newline)

    (console-display "(log1p -1): ")
    (guard-without-result (exc (else (console-display "exception ")
				     (console-display-line exc)))
			  (console-display-line (log1p -1)))
    (report-test (log1p 2) 1.0986122886681098)
    (report-test (log1p -2)
		 (complex 0.0 3.141592653589793))
    (report-test (log1p -3)
		 (complex 0.6931471805599453 3.141592653589793))

    (report-test (log1p 2) 1.0986122886681098)
    (console-display "(log1p -1/1): ")
    (guard-without-result (exc (else (console-display "exception ")
				     (console-display-line exc)))
			  (console-display-line (log1p (rational -1 1))))
    (report-test (log1p (rational -1 2)) -0.6931471805599453)
    (report-test (log1p (rational -6 5))
		 (complex -1.6094379124341003 3.141592653589793))
    (report-test (log1p 1.5) 0.9162907318741551)
    (report-test (log1p -1.0) (neg-inf))
    (report-test (log1p -2.2)
		 (complex 0.1823215567939548 3.141592653589793))
    (report-test (log1p (complex 5.0 -4.5))
		 (complex 2.0149030205422647 -0.6435011087932844))
    (console-newline)

    (report-test (cbrt 8) 2)
    (report-test (cbrt -8) -2)
    ;; It seems that r-cbrt can't compute the cubic root of 27/64 exactly.
    ;; This may be system dependent.
    ;; (report-test (cbrt (rational 27 64)) (rational 3 4))
    ;; (report-test (cbrt (rational -27 64)) (rational -3 4))
    (report-test (cbrt (rational 27 64)) 0.75)
    (report-test (cbrt (rational -27 64)) -0.75)
    (report-test (cbrt 15.1) 2.4716804116889617)
    (report-test (cbrt -15.1) -2.4716804116889617)
    (report-test (cbrt (complex -0.5 0.4))
		 (complex 0.5865752513534657 0.6315262137427671))
    (console-newline)

    (report-test (hypot 1.5 -3.1) 3.443835071544513)
    (report-test (hypot 1.5 (rational 4 5)) 1.7)
    (report-test (hypot 2.1 1) 2.3259406699226015)
    (report-test (hypot (rational -3 4) -3.1) 3.1894356867634124)
    (report-test (hypot (rational 3 5) (rational 2 9)) 0.6398302243950208)
    (report-test (hypot (rational 7 5) 5) 5.192301994298868)
    (report-test (hypot -1 2.5) 2.692582403567252)
    (report-test (hypot -2 (rational 13 9)) 2.4670670345749466)
    (report-test (hypot 3 7) 7.615773105863909)
    (console-newline)

    (report-test (erf 1.5) 0.966105146475311)
    (report-test (erf (rational 3 2)) 0.966105146475311)
    (report-test (erf 2) 0.995322265018953)
    (report-test (erf 0) 0)
    (console-newline)

    (report-test (erfc 1.5) 0.033894853524689)
    (report-test (erfc (rational 3 2)) 0.033894853524689)
    (report-test (erfc 2) 0.004677734981047)
    (report-test (erfc 0) 1)
    (console-newline)

    (report-test (lgamma 2.5) 0.284682870472919)
    (report-test (lgamma (rational 5 2)) 0.284682870472919)
    (report-test (lgamma 1) 0)
    (report-test (lgamma 2) 0)
    (report-test (lgamma 3) 0.693147180559945)
    (report-test (lgamma 10) 12.8018274800815) 
    (console-newline)

    (report-test (tgamma 2.5) 1.329340388179137)
    (report-test (tgamma (rational 5 2)) 1.329340388179137)
    (report-test (tgamma 1) 1)
    (report-test (tgamma 2) 1)
    (report-test (tgamma 3) 2)
    (report-test (tgamma 10) 362880)
    (console-newline)))
