| 1 |
/* -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*- */ |
| 2 |
/* vim: set filetype=c tabstop=2 shiftwidth=2 expandtab: */ |
| 3 |
|
| 4 |
/* |
| 5 |
* round.d - implementation of CL:ROUND |
| 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 |
#include <ecl/ecl.h> |
| 20 |
#include <ecl/impl/math_dispatch2.h> |
| 21 |
#ifndef HAVE_ISOC99 |
| 22 |
# define floorf floor |
| 23 |
#endif |
| 24 |
#include <ecl/internal.h> |
| 25 |
|
| 26 |
#pragma STDC FENV_ACCESS ON |
| 27 |
|
| 28 |
@(defun round (x &optional (y OBJNULL)) |
| 29 |
@ |
| 30 |
if (narg == 1) |
| 31 |
return ecl_round1(x); |
| 32 |
else |
| 33 |
return ecl_round2(x, y); |
| 34 |
@) |
| 35 |
|
| 36 |
static cl_object |
| 37 |
number_remainder(cl_object x, cl_object y, cl_object q) |
| 38 |
{ |
| 39 |
cl_object z; |
| 40 |
|
| 41 |
z = ecl_times(q, y); |
| 42 |
z = ecl_minus(x, z); |
| 43 |
return(z); |
| 44 |
} |
| 45 |
|
| 46 |
static double |
| 47 |
round_double(double d) |
| 48 |
{ |
| 49 |
if (d >= 0) { |
| 50 |
double q = floor(d += 0.5); |
| 51 |
if (q == d) { |
| 52 |
int i = (int)fmod(q, 10); |
| 53 |
if (i & 1) { |
| 54 |
return q-1; |
| 55 |
} |
| 56 |
} |
| 57 |
return q; |
| 58 |
} else if (isnan(d)) { |
| 59 |
return d; |
| 60 |
} else { |
| 61 |
return -round_double(-d); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
static long double |
| 66 |
round_long_double(long double d) |
| 67 |
{ |
| 68 |
if (d >= 0) { |
| 69 |
long double q = floorl(d += 0.5); |
| 70 |
if (q == d) { |
| 71 |
int i = (int)fmodl(q, 10); |
| 72 |
if (i & 1) { |
| 73 |
return q-1; |
| 74 |
} |
| 75 |
} |
| 76 |
return q; |
| 77 |
} else if (isnan(d)) { |
| 78 |
return d; |
| 79 |
} else { |
| 80 |
return -round_long_double(-d); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
static cl_object |
| 85 |
ecl_round2_integer(const cl_env_ptr the_env, cl_object x, cl_object y, cl_object q) |
| 86 |
{ |
| 87 |
cl_object q1 = ecl_integer_divide(q->ratio.num, q->ratio.den); |
| 88 |
cl_object r = ecl_minus(q, q1); |
| 89 |
if (ecl_minusp(r)) { |
| 90 |
int c = ecl_number_compare(cl_core.minus_half, r); |
| 91 |
if (c > 0 || (c == 0 && ecl_oddp(q1))) { |
| 92 |
q1 = ecl_one_minus(q1); |
| 93 |
} |
| 94 |
} else { |
| 95 |
int c = ecl_number_compare(r, cl_core.plus_half); |
| 96 |
if (c > 0 || (c == 0 && ecl_oddp(q1))) { |
| 97 |
q1 = ecl_one_plus(q1); |
| 98 |
} |
| 99 |
} |
| 100 |
r = number_remainder(x, y, q1); |
| 101 |
ecl_return2(the_env, q1, r); |
| 102 |
} |
| 103 |
|
| 104 |
cl_object |
| 105 |
ecl_round1(cl_object x) |
| 106 |
{ |
| 107 |
const cl_env_ptr the_env = ecl_process_env(); |
| 108 |
cl_object v0, v1; |
| 109 |
ECL_MATHERR_CLEAR; |
| 110 |
|
| 111 |
switch (ecl_t_of(x)) { |
| 112 |
case t_fixnum: |
| 113 |
case t_bignum: |
| 114 |
v0 = x; |
| 115 |
v1 = ecl_make_fixnum(0); |
| 116 |
break; |
| 117 |
case t_ratio: |
| 118 |
v0 = ecl_round2_integer(the_env, x->ratio.num, x->ratio.den, x); |
| 119 |
v1 = ecl_make_ratio(ecl_nth_value(the_env, 1), x->ratio.den); |
| 120 |
break; |
| 121 |
case t_singlefloat: { |
| 122 |
float d = ecl_single_float(x); |
| 123 |
float q = round_double(d); |
| 124 |
v0 = _ecl_float_to_integer(q); |
| 125 |
v1 = ecl_make_single_float(d - q); |
| 126 |
break; |
| 127 |
} |
| 128 |
case t_doublefloat: { |
| 129 |
double d = ecl_double_float(x); |
| 130 |
double q = round_double(d); |
| 131 |
v0 = _ecl_double_to_integer(q); |
| 132 |
v1 = ecl_make_double_float(d - q); |
| 133 |
break; |
| 134 |
} |
| 135 |
case t_longfloat: { |
| 136 |
long double d = ecl_long_float(x); |
| 137 |
long double q = round_long_double(d); |
| 138 |
v0 = _ecl_long_double_to_integer(q); |
| 139 |
v1 = ecl_make_long_float(d - q); |
| 140 |
break; |
| 141 |
} |
| 142 |
default: |
| 143 |
FEwrong_type_nth_arg(@[round],1,x,@[real]); |
| 144 |
} |
| 145 |
|
| 146 |
ECL_MATHERR_TEST; |
| 147 |
ecl_return2(the_env, v0, v1); |
| 148 |
} |
| 149 |
|
| 150 |
cl_object |
| 151 |
ecl_round2(cl_object x, cl_object y) |
| 152 |
{ |
| 153 |
const cl_env_ptr the_env = ecl_process_env(); |
| 154 |
cl_object v0, v1; |
| 155 |
cl_object q; |
| 156 |
|
| 157 |
q = ecl_divide(x, y); |
| 158 |
switch (ecl_t_of(q)) { |
| 159 |
case t_fixnum: |
| 160 |
case t_bignum: |
| 161 |
v0 = q; |
| 162 |
v1 = ecl_make_fixnum(0); |
| 163 |
break; |
| 164 |
case t_ratio: |
| 165 |
return ecl_round2_integer(the_env, x, y, q); |
| 166 |
default: |
| 167 |
v0 = q = ecl_round1(q); |
| 168 |
v1 = number_remainder(x, y, q); |
| 169 |
} |
| 170 |
ecl_return2(the_env, v0, v1); |
| 171 |
} |