| 1 |
/* -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*- */ |
| 2 |
/* vim: set filetype=c tabstop=2 shiftwidth=2 expandtab: */ |
| 3 |
|
| 4 |
/* |
| 5 |
* sqrt.d - square root |
| 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 |
|
| 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 |
#pragma STDC FENV_ACCESS ON |
| 22 |
|
| 23 |
cl_object |
| 24 |
cl_sqrt(cl_object x) |
| 25 |
{ |
| 26 |
@(return ecl_sqrt(x)); |
| 27 |
} |
| 28 |
|
| 29 |
static cl_object |
| 30 |
ecl_sqrt_rational(cl_object x) |
| 31 |
{ |
| 32 |
if (ecl_minusp(x)) { |
| 33 |
x = ecl_sqrt_rational(ecl_negate(x)); |
| 34 |
return ecl_make_complex(ecl_make_fixnum(0), x); |
| 35 |
} else { |
| 36 |
return ecl_make_single_float(sqrtf(ecl_to_float(x))); |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
static cl_object |
| 41 |
ecl_sqrt_single_float(cl_object x) |
| 42 |
{ |
| 43 |
float f = ecl_single_float(x); |
| 44 |
if (f < 0) { |
| 45 |
return ecl_make_complex(ecl_make_fixnum(0), |
| 46 |
ecl_make_single_float(sqrtf(-f))); |
| 47 |
} else { |
| 48 |
return ecl_make_single_float(sqrtf(f)); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
static cl_object |
| 53 |
ecl_sqrt_double_float(cl_object x) |
| 54 |
{ |
| 55 |
double f = ecl_double_float(x); |
| 56 |
if (f < 0) { |
| 57 |
return ecl_make_complex(ecl_make_fixnum(0), |
| 58 |
ecl_make_double_float(sqrt(-f))); |
| 59 |
} else { |
| 60 |
return ecl_make_double_float(sqrt(f)); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
static cl_object |
| 65 |
ecl_sqrt_long_float(cl_object x) |
| 66 |
{ |
| 67 |
long double f = ecl_long_float(x); |
| 68 |
if (f < 0) { |
| 69 |
return ecl_make_complex(ecl_make_fixnum(0), |
| 70 |
ecl_make_long_float(sqrtl(-f))); |
| 71 |
} else { |
| 72 |
return ecl_make_long_float(sqrtl(f)); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
static cl_object |
| 77 |
ecl_sqrt_complex(cl_object x) |
| 78 |
{ |
| 79 |
return ecl_expt(x, cl_core.plus_half); |
| 80 |
} |
| 81 |
|
| 82 |
#ifdef ECL_COMPLEX_FLOAT |
| 83 |
static cl_object |
| 84 |
ecl_sqrt_csfloat(cl_object x) |
| 85 |
{ |
| 86 |
cl_object result = ecl_alloc_object(t_csfloat); |
| 87 |
ecl_csfloat(result) = csqrtf(ecl_csfloat(x)); |
| 88 |
return result; |
| 89 |
} |
| 90 |
|
| 91 |
static cl_object |
| 92 |
ecl_sqrt_cdfloat(cl_object x) |
| 93 |
{ |
| 94 |
cl_object result = ecl_alloc_object(t_cdfloat); |
| 95 |
ecl_cdfloat(result) = csqrt(ecl_cdfloat(x)); |
| 96 |
return result; |
| 97 |
} |
| 98 |
|
| 99 |
static cl_object |
| 100 |
ecl_sqrt_clfloat(cl_object x) |
| 101 |
{ |
| 102 |
cl_object result = ecl_alloc_object(t_clfloat); |
| 103 |
ecl_clfloat(result) = csqrtl(ecl_clfloat(x)); |
| 104 |
return result; |
| 105 |
} |
| 106 |
#endif |
| 107 |
|
| 108 |
MATH_DEF_DISPATCH1(sqrt, @[sqrt], @[number], |
| 109 |
ecl_sqrt_rational, ecl_sqrt_rational, ecl_sqrt_rational, |
| 110 |
ecl_sqrt_single_float, ecl_sqrt_double_float, ecl_sqrt_long_float, |
| 111 |
ecl_sqrt_complex, |
| 112 |
ecl_sqrt_csfloat, ecl_sqrt_cdfloat, ecl_sqrt_clfloat); |