Debian Codegraph @

src/c/numbers/atan.d

raw
1 /* -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /* vim: set filetype=c tabstop=2 shiftwidth=2 expandtab: */
3
4 /*
5 * atan.d - Trascendental functions: arc tangent
6 *
7 * Copyright (c) 1984 Taiichi Yuasa and Masami Hagiya
8 * Copyright (c) 1990 Giuseppe Attardi
9 * Copyright (c) 2001 Juan Jose Garcia Ripoll
10 * Copyright (c) 2016 Daniel Kochmański
11 *
12 * See file 'LICENSE' for the copyright details.
13 *
14 */
15
16 #define ECL_INCLUDE_MATH_H
17 #include <ecl/ecl.h>
18 #include <ecl/internal.h>
19 #include <ecl/impl/math_fenv.h>
20
21 #pragma STDC FENV_ACCESS ON
22
23 cl_object
24 ecl_atan2(cl_object y, cl_object x)
25 {
26 cl_object output;
27 ECL_MATHERR_CLEAR;
28 {
29 int tx = ecl_t_of(x);
30 int ty = ecl_t_of(y);
31 if (tx < ty)
32 tx = ty;
33 if (tx == t_longfloat) {
34 long double d = atan2l(ecl_to_long_double(y), ecl_to_long_double(x));
35 output = ecl_make_long_float(d);
36 } else {
37 double dx = ecl_to_double(x);
38 double dy = ecl_to_double(y);
39 double dz = atan2(dy, dx);
40 if (tx == t_doublefloat) {
41 output = ecl_make_double_float(dz);
42 } else {
43 output = ecl_make_single_float(dz);
44 }
45 }
46 }
47 ECL_MATHERR_TEST;
48 return output;
49 }
50
51 cl_object
52 ecl_atan1(cl_object y)
53 {
54 if (ECL_COMPLEXP(y)) {
55 #if 0 /* ANSI states it should be this first part */
56 cl_object z = ecl_times(cl_core.imag_unit, y);
57 z = ecl_plus(ecl_log1(ecl_one_plus(z)),
58 ecl_log1(ecl_minus(ecl_make_fixnum(1), z)));
59 z = ecl_divide(z, ecl_times(ecl_make_fixnum(2),
60 cl_core.imag_unit));
61 #else
62 cl_object z1, z = ecl_times(cl_core.imag_unit, y);
63 z = ecl_one_plus(z);
64 z1 = ecl_times(y, y);
65 z1 = ecl_one_plus(z1);
66 z1 = ecl_sqrt(z1);
67 z = ecl_divide(z, z1);
68 z = ecl_log1(z);
69 z = ecl_times(cl_core.minus_imag_unit, z);
70 #endif /* ANSI */
71 return z;
72 } else {
73 return ecl_atan2(y, ecl_make_fixnum(1));
74 }
75 }
76
77 @(defun atan (x &optional (y OBJNULL))
78 @
79 /* INV: type check in ecl_atan1() & ecl_atan2() */
80 /* FIXME ecl_atan1() and ecl_atan2() produce generic errors
81 without recovery and function information. */
82 if (y == OBJNULL) {
83 @(return ecl_atan1(x));
84 }
85 @(return ecl_atan2(x, y));
86 @)