| 1 |
/* -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*- */ |
| 2 |
/* vim: set filetype=c tabstop=2 shiftwidth=2 expandtab: */ |
| 3 |
|
| 4 |
/* |
| 5 |
* abs.d - absolute value |
| 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 |
* |
| 11 |
* See file 'LICENSE' for the copyright details. |
| 12 |
* |
| 13 |
*/ |
| 14 |
|
| 15 |
#include <stdlib.h> |
| 16 |
#define ECL_INCLUDE_MATH_H |
| 17 |
#include <ecl/ecl.h> |
| 18 |
#include <ecl/internal.h> |
| 19 |
#include <ecl/impl/math_dispatch.h> |
| 20 |
|
| 21 |
cl_object |
| 22 |
cl_abs(cl_object x) |
| 23 |
{ |
| 24 |
@(return ecl_abs(x)); |
| 25 |
} |
| 26 |
|
| 27 |
static cl_object |
| 28 |
ecl_abs_fixnum(cl_object x) |
| 29 |
{ |
| 30 |
return ecl_fixnum_minusp(x)? ecl_make_integer(-ecl_fixnum(x)) : x; |
| 31 |
} |
| 32 |
|
| 33 |
static cl_object |
| 34 |
ecl_abs_bignum(cl_object x) |
| 35 |
{ |
| 36 |
return (_ecl_big_sign(x) < 0)? _ecl_big_negate(x) : x; |
| 37 |
} |
| 38 |
|
| 39 |
static cl_object |
| 40 |
ecl_abs_rational(cl_object x) |
| 41 |
{ |
| 42 |
return (ecl_minusp(x->ratio.num))? |
| 43 |
ecl_make_ratio(ecl_negate(x->ratio.num), x->ratio.den) : x; |
| 44 |
} |
| 45 |
|
| 46 |
/* Example in ABS spec is a bit misleading because it says that |
| 47 |
|
| 48 |
(abs -0.0) ; -> -0.0 |
| 49 |
|
| 50 |
but CLHS 1.4.3 states that the examples are not normative. */ |
| 51 |
|
| 52 |
static cl_object |
| 53 |
ecl_abs_single_float(cl_object x) |
| 54 |
{ |
| 55 |
float f = ecl_single_float(x); |
| 56 |
f = fabsf(f); |
| 57 |
return ecl_make_single_float(f); |
| 58 |
} |
| 59 |
|
| 60 |
static cl_object |
| 61 |
ecl_abs_double_float(cl_object x) |
| 62 |
{ |
| 63 |
double f = ecl_double_float(x); |
| 64 |
f = fabs(f); |
| 65 |
return ecl_make_double_float(f); |
| 66 |
} |
| 67 |
|
| 68 |
static cl_object |
| 69 |
ecl_abs_long_float(cl_object x) |
| 70 |
{ |
| 71 |
long double f = ecl_long_float(x); |
| 72 |
f = fabsl(f); |
| 73 |
return ecl_make_long_float(f); |
| 74 |
} |
| 75 |
|
| 76 |
static cl_object |
| 77 |
ecl_abs_complex(cl_object x) |
| 78 |
{ |
| 79 |
/* Compute sqrt(r*r + i*i) carefully to prevent overflow. |
| 80 |
* Assume |i| >= |r|. Then sqrt(i*i + r*r) = |i|*sqrt(1 +(r/i)^2). |
| 81 |
*/ |
| 82 |
cl_object r = ecl_abs(x->gencomplex.real); |
| 83 |
cl_object i = ecl_abs(x->gencomplex.imag); |
| 84 |
int comparison; |
| 85 |
comparison = ecl_number_compare(r, i); |
| 86 |
if (comparison == 0) { |
| 87 |
r = ecl_times(r, r); |
| 88 |
return ecl_sqrt(ecl_plus(r, r)); |
| 89 |
} else { |
| 90 |
if (comparison > 0) { |
| 91 |
cl_object aux = i; |
| 92 |
i = r; r = aux; |
| 93 |
} |
| 94 |
r = ecl_divide(r, i); |
| 95 |
r = ecl_plus(ecl_make_fixnum(1), ecl_times(r, r)); |
| 96 |
return ecl_times(cl_sqrt(r), i); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
#ifdef ECL_COMPLEX_FLOAT |
| 101 |
static cl_object |
| 102 |
ecl_abs_csfloat(cl_object x) |
| 103 |
{ |
| 104 |
float f = crealf(cabsf(ecl_csfloat(x))); |
| 105 |
x = ecl_make_single_float(f); |
| 106 |
return x; |
| 107 |
} |
| 108 |
|
| 109 |
static cl_object |
| 110 |
ecl_abs_cdfloat(cl_object x) |
| 111 |
{ |
| 112 |
double f = creal(cabs(ecl_cdfloat(x))); |
| 113 |
x = ecl_make_double_float(f); |
| 114 |
return x; |
| 115 |
} |
| 116 |
|
| 117 |
static cl_object |
| 118 |
ecl_abs_clfloat(cl_object x) |
| 119 |
{ |
| 120 |
long double f = creall(cabsl(ecl_clfloat(x))); |
| 121 |
x = ecl_make_long_float(f); |
| 122 |
return x; |
| 123 |
} |
| 124 |
#endif |
| 125 |
|
| 126 |
MATH_DEF_DISPATCH1_NE(abs, @[abs], @[number], |
| 127 |
ecl_abs_fixnum, ecl_abs_bignum, ecl_abs_rational, |
| 128 |
ecl_abs_single_float, ecl_abs_double_float, ecl_abs_long_float, |
| 129 |
ecl_abs_complex, |
| 130 |
ecl_abs_csfloat, ecl_abs_cdfloat, ecl_abs_clfloat); |