| 1 |
/* -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*- */ |
| 2 |
/* vim: set filetype=c tabstop=2 shiftwidth=2 expandtab: */ |
| 3 |
|
| 4 |
/* |
| 5 |
* truncate.d - implementation of CL:TRUNCATE |
| 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 <float.h> |
| 19 |
#ifndef HAVE_ISOC99 |
| 20 |
# define floorf floor |
| 21 |
# define ceilf ceil |
| 22 |
#endif |
| 23 |
#include <ecl/internal.h> |
| 24 |
|
| 25 |
#pragma STDC FENV_ACCESS ON |
| 26 |
|
| 27 |
cl_object |
| 28 |
ecl_truncate1(cl_object x) |
| 29 |
{ |
| 30 |
cl_object v0, v1; |
| 31 |
ECL_MATHERR_CLEAR; |
| 32 |
|
| 33 |
switch (ecl_t_of(x)) { |
| 34 |
case t_fixnum: |
| 35 |
case t_bignum: |
| 36 |
v0 = x; |
| 37 |
v1 = ecl_make_fixnum(0); |
| 38 |
break; |
| 39 |
case t_ratio: |
| 40 |
if (ecl_plusp(x->ratio.num)) |
| 41 |
return ecl_floor1(x); |
| 42 |
else |
| 43 |
return ecl_ceiling1(x); |
| 44 |
case t_singlefloat: { |
| 45 |
float d = ecl_single_float(x); |
| 46 |
float y = d > 0? floorf(d) : ceilf(d); |
| 47 |
v0 = _ecl_float_to_integer(y); |
| 48 |
v1 = ecl_make_single_float(d - y); |
| 49 |
break; |
| 50 |
} |
| 51 |
case t_doublefloat: { |
| 52 |
double d = ecl_double_float(x); |
| 53 |
double y = d > 0? floor(d) : ceil(d); |
| 54 |
v0 = _ecl_double_to_integer(y); |
| 55 |
v1 = ecl_make_double_float(d - y); |
| 56 |
break; |
| 57 |
} |
| 58 |
case t_longfloat: { |
| 59 |
long double d = ecl_long_float(x); |
| 60 |
long double y = d > 0? floorl(d) : ceill(d); |
| 61 |
v0 = _ecl_long_double_to_integer(y); |
| 62 |
v1 = ecl_make_long_float(d - y); |
| 63 |
break; |
| 64 |
} |
| 65 |
default: |
| 66 |
FEwrong_type_nth_arg(@[truncate],1,x,@[real]); |
| 67 |
} |
| 68 |
ECL_MATHERR_TEST; |
| 69 |
{ |
| 70 |
const cl_env_ptr the_env = ecl_process_env(); |
| 71 |
ecl_return2(the_env, v0, v1); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
cl_object |
| 76 |
ecl_truncate2(cl_object x, cl_object y) |
| 77 |
{ |
| 78 |
if (ecl_plusp(x) != ecl_plusp(y)) |
| 79 |
return ecl_ceiling2(x, y); |
| 80 |
else |
| 81 |
return ecl_floor2(x, y); |
| 82 |
} |
| 83 |
|
| 84 |
@(defun truncate (x &optional (y OBJNULL)) |
| 85 |
@ |
| 86 |
if (narg == 1) |
| 87 |
return ecl_truncate1(x); |
| 88 |
else |
| 89 |
return ecl_truncate2(x, y); |
| 90 |
@) |