simplify ecl_to_float() and use it everywhere instead of number_to_float()

This commit is contained in:
Juan Jose Garcia Ripoll 2012-06-10 00:46:18 +02:00
parent 7b79e8d2d7
commit 2bf083856b
13 changed files with 44 additions and 40 deletions

View file

@ -112,30 +112,6 @@ ecl_to_unsigned_integer(cl_object x)
}
}
float
ecl_to_float(cl_object x)
{
if (ECL_FIXNUMP(x)) return(ecl_fixnum(x)); /* Immediate fixnum */
switch (type_of(x)) {
/* case t_fixnum: return ecl_fixnum(x); */
/* case t_character: return ECL_CHAR_CODE(x); */
case t_bignum:
case t_ratio:
return ecl_to_double(x);
case t_singlefloat:
return ecl_single_float(x);
case t_doublefloat:
return ecl_double_float(x);
#ifdef ECL_LONG_FLOAT
case t_longfloat:
return ecl_long_float(x);
#endif
default:
FEwrong_type_nth_arg(@[coerce], 1, x, @[real]);
}
}
int
ecl_aref_bv(cl_object x, cl_index index)
{

View file

@ -743,6 +743,31 @@ ratio_to_long_double(cl_object num, cl_object den)
}
#endif /* ECL_LONG_FLOAT */
float
ecl_to_float(cl_object x)
{
if (ECL_FIXNUMP(x)) return(ecl_fixnum(x)); /* Immediate fixnum */
switch (type_of(x)) {
case t_fixnum:
return (float)ecl_fixnum(x);
case t_bignum:
return (float)ratio_to_double(x, ecl_make_fixnum(1));
case t_ratio:
return (float)ratio_to_double(x->ratio.num, x->ratio.den);
case t_singlefloat:
return ecl_single_float(x);
case t_doublefloat:
return (float)ecl_double_float(x);
#ifdef ECL_LONG_FLOAT
case t_longfloat:
return (float)ecl_long_float(x);
#endif
default:
FEwrong_type_nth_arg(@[coerce], 1, x, @[real]);
}
}
double
ecl_to_double(cl_object x)
{

View file

@ -31,7 +31,7 @@ cl_cos(cl_object x)
static cl_object
ecl_cos_rational(cl_object x)
{
return ecl_make_single_float(cosf(number_to_float(x)));
return ecl_make_single_float(cosf(ecl_to_float(x)));
}
static cl_object

View file

@ -31,7 +31,7 @@ cl_cosh(cl_object x)
static cl_object
ecl_cosh_rational(cl_object x)
{
return ecl_make_single_float(coshf(number_to_float(x)));
return ecl_make_single_float(coshf(ecl_to_float(x)));
}
static cl_object

View file

@ -31,7 +31,7 @@ cl_exp(cl_object x)
static cl_object
ecl_exp_rational(cl_object x)
{
return ecl_make_single_float(expf(number_to_float(x)));
return ecl_make_single_float(expf(ecl_to_float(x)));
}
static cl_object

View file

@ -58,7 +58,7 @@ ecl_log1_bignum(cl_object x)
} else {
cl_fixnum l = ecl_integer_length(x) - 1;
cl_object r = ecl_make_ratio(x, ecl_ash(ecl_make_fixnum(1), l));
float d = logf(number_to_float(r)) + l * logf(2.0);
float d = logf(ecl_to_float(r)) + l * logf(2.0);
return ecl_make_single_float(d);
}
}
@ -66,9 +66,9 @@ ecl_log1_bignum(cl_object x)
static cl_object
ecl_log1_rational(cl_object x)
{
float f = number_to_float(x);
float f = ecl_to_float(x);
if (f < 0) return ecl_log1_complex_inner(x, ecl_make_fixnum(0));
return ecl_make_single_float(logf(number_to_float(x)));
return ecl_make_single_float(logf(ecl_to_float(x)));
}
static cl_object
@ -179,9 +179,9 @@ ecl_log1p_simple(cl_object x)
static cl_object
ecl_log1p_rational(cl_object x)
{
float f = number_to_float(x);
float f = ecl_to_float(x);
if (f < -1) return ecl_log1p_simple(x);
return ecl_make_single_float(log1pf(number_to_float(x)));
return ecl_make_single_float(log1pf(ecl_to_float(x)));
}
static cl_object

View file

@ -31,7 +31,7 @@ cl_sin(cl_object x)
static cl_object
ecl_sin_rational(cl_object x)
{
return ecl_make_single_float(sinf(number_to_float(x)));
return ecl_make_single_float(sinf(ecl_to_float(x)));
}
static cl_object

View file

@ -31,7 +31,7 @@ cl_sinh(cl_object x)
static cl_object
ecl_sinh_rational(cl_object x)
{
return ecl_make_single_float(sinhf(number_to_float(x)));
return ecl_make_single_float(sinhf(ecl_to_float(x)));
}
static cl_object

View file

@ -35,7 +35,7 @@ ecl_sqrt_rational(cl_object x)
x = ecl_sqrt_rational(ecl_negate(x));
return ecl_make_complex(ecl_make_fixnum(0), x);
} else {
return ecl_make_single_float(sqrtf(number_to_float(x)));
return ecl_make_single_float(sqrtf(ecl_to_float(x)));
}
}

View file

@ -43,7 +43,7 @@ cl_tan(cl_object x)
static cl_object
ecl_tan_rational(cl_object x)
{
return ecl_make_single_float(safe_tanf(number_to_float(x)));
return ecl_make_single_float(safe_tanf(ecl_to_float(x)));
}
static cl_object

View file

@ -31,7 +31,7 @@ cl_tanh(cl_object x)
static cl_object
ecl_tanh_rational(cl_object x)
{
return ecl_make_single_float(tanhf(number_to_float(x)));
return ecl_make_single_float(tanhf(ecl_to_float(x)));
}
static cl_object

View file

@ -500,8 +500,6 @@ extern ECL_API cl_fixnum ecl_imod(cl_fixnum x, cl_fixnum y);
extern ECL_API char ecl_to_char(cl_object x);
extern ECL_API cl_fixnum ecl_to_fixnum(cl_object x);
extern ECL_API cl_index ecl_to_unsigned_integer(cl_object x);
extern ECL_API float ecl_to_float(cl_object x);
extern ECL_API double ecl_to_double(cl_object x);
extern ECL_API int ecl_aref_bv(cl_object x, cl_index index);
extern ECL_API int ecl_aset_bv(cl_object x, cl_index index, int value);
extern ECL_API void cl_throw(cl_object tag) /*ecl_attr_noreturn*/;
@ -1110,8 +1108,8 @@ extern ECL_API cl_object ecl_make_double_float(double f);
extern ECL_API cl_object ecl_make_complex(cl_object r, cl_object i);
extern ECL_API cl_object cl_rational(cl_object x);
#define cl_rationalize cl_rational
extern ECL_API float ecl_to_float(cl_object x);
extern ECL_API double ecl_to_double(cl_object x);
#define number_to_float(x) ((float)ecl_to_double(x))
#ifdef ECL_LONG_FLOAT
extern ECL_API long double ecl_to_long_double(cl_object x);
extern ECL_API cl_object ecl_make_long_float(long double f);

View file

@ -117,3 +117,8 @@
#define ecl_make_singlefloat ecl_make_single_float
#define ecl_make_doublefloat ecl_make_double_float
#define ecl_make_longfloat ecl_make_long_float
#define number_to_float(x) ((float)ecl_to_double(x))
#define ecl_make_unsigned_long_Long(o) ecl_make_ulong_long(o)
#define ecl_to_unsigned_long_long(o) ecl_to_ulong_long(o)