factor/native/float.c

211 lines
3.7 KiB
C
Raw Normal View History

2004-08-05 16:49:55 -04:00
#include "factor.h"
2004-09-19 00:33:40 -04:00
double to_float(CELL tagged)
2004-08-05 16:49:55 -04:00
{
F_RATIO* r;
2004-09-19 00:33:40 -04:00
double x;
double y;
2004-08-27 02:09:24 -04:00
2004-08-05 16:49:55 -04:00
switch(type_of(tagged))
{
case FIXNUM_TYPE:
2004-09-19 00:33:40 -04:00
return (double)untag_fixnum_fast(tagged);
2004-08-05 16:49:55 -04:00
case BIGNUM_TYPE:
return s48_bignum_to_double((F_ARRAY*)UNTAG(tagged));
2004-08-05 16:49:55 -04:00
case RATIO_TYPE:
r = (F_RATIO*)UNTAG(tagged);
2004-09-19 00:33:40 -04:00
x = to_float(r->numerator);
y = to_float(r->denominator);
return x / y;
2004-08-05 16:49:55 -04:00
case FLOAT_TYPE:
return ((F_FLOAT*)UNTAG(tagged))->n;
2004-08-05 16:49:55 -04:00
default:
type_error(FLOAT_TYPE,tagged);
2004-09-19 00:33:40 -04:00
return 0.0; /* can't happen */
2004-08-05 16:49:55 -04:00
}
}
void primitive_to_float(void)
{
maybe_garbage_collection();
2004-09-19 00:33:40 -04:00
drepl(tag_object(make_float(to_float(dpeek()))));
2004-08-05 16:49:55 -04:00
}
2004-08-05 17:33:02 -04:00
void primitive_str_to_float(void)
{
F_STRING* str;
char *c_str, *end;
double f;
maybe_garbage_collection();
str = untag_string(dpeek());
c_str = to_c_string(str);
end = c_str;
f = strtod(c_str,&end);
if(end != c_str + str->capacity)
general_error(ERROR_FLOAT_FORMAT,tag_object(str));
drepl(tag_object(make_float(f)));
2004-08-05 17:33:02 -04:00
}
void primitive_float_to_str(void)
{
char tmp[33];
maybe_garbage_collection();
2004-09-19 17:39:28 -04:00
snprintf(tmp,32,"%.16g",to_float(dpop()));
2004-08-05 17:33:02 -04:00
tmp[32] = '\0';
2004-09-19 17:39:28 -04:00
box_c_string(tmp);
2004-08-05 17:33:02 -04:00
}
2004-08-06 02:51:32 -04:00
void primitive_float_to_bits(void)
{
double f;
2004-12-10 21:39:45 -05:00
int64_t f_raw;
maybe_garbage_collection();
f = untag_float(dpeek());
2004-12-10 21:39:45 -05:00
f_raw = *(int64_t*)&f;
2004-08-26 19:37:22 -04:00
drepl(tag_object(s48_long_long_to_bignum(f_raw)));
2004-08-06 02:51:32 -04:00
}
#define GC_AND_POP_FLOATS(x,y) \
double x, y; \
maybe_garbage_collection(); \
2004-12-18 20:24:46 -05:00
y = untag_float_fast(dpop()); \
x = untag_float_fast(dpop());
2004-09-19 00:33:40 -04:00
void primitive_float_eq(void)
2004-08-05 16:49:55 -04:00
{
GC_AND_POP_FLOATS(x,y);
2004-11-08 22:36:51 -05:00
box_boolean(x == y);
2004-08-05 16:49:55 -04:00
}
2004-09-19 00:33:40 -04:00
void primitive_float_add(void)
2004-08-05 16:49:55 -04:00
{
GC_AND_POP_FLOATS(x,y);
2004-09-19 00:33:40 -04:00
dpush(tag_object(make_float(x + y)));
2004-08-05 16:49:55 -04:00
}
2004-09-19 00:33:40 -04:00
void primitive_float_subtract(void)
2004-08-05 16:49:55 -04:00
{
GC_AND_POP_FLOATS(x,y);
2004-09-19 00:33:40 -04:00
dpush(tag_object(make_float(x - y)));
2004-08-05 16:49:55 -04:00
}
2004-09-19 00:33:40 -04:00
void primitive_float_multiply(void)
2004-08-05 16:49:55 -04:00
{
GC_AND_POP_FLOATS(x,y);
2004-09-19 00:33:40 -04:00
dpush(tag_object(make_float(x * y)));
2004-08-05 16:49:55 -04:00
}
2004-09-19 00:33:40 -04:00
void primitive_float_divfloat(void)
2004-08-05 16:49:55 -04:00
{
GC_AND_POP_FLOATS(x,y);
2004-09-19 00:33:40 -04:00
dpush(tag_object(make_float(x / y)));
2004-08-05 16:49:55 -04:00
}
2004-09-19 00:33:40 -04:00
void primitive_float_less(void)
2004-08-05 16:49:55 -04:00
{
2004-11-08 22:36:51 -05:00
GC_AND_POP_FLOATS(x,y);
box_boolean(x < y);
2004-08-05 16:49:55 -04:00
}
2004-09-19 00:33:40 -04:00
void primitive_float_lesseq(void)
2004-08-05 16:49:55 -04:00
{
2004-11-08 22:36:51 -05:00
GC_AND_POP_FLOATS(x,y);
box_boolean(x <= y);
2004-08-05 16:49:55 -04:00
}
2004-09-19 00:33:40 -04:00
void primitive_float_greater(void)
2004-08-05 16:49:55 -04:00
{
2004-11-08 22:36:51 -05:00
GC_AND_POP_FLOATS(x,y);
box_boolean(x > y);
2004-08-05 16:49:55 -04:00
}
2004-09-19 00:33:40 -04:00
void primitive_float_greatereq(void)
2004-08-05 16:49:55 -04:00
{
2004-11-08 22:36:51 -05:00
GC_AND_POP_FLOATS(x,y);
box_boolean(x >= y);
2004-08-05 16:49:55 -04:00
}
void primitive_facos(void)
{
maybe_garbage_collection();
2004-09-19 00:33:40 -04:00
drepl(tag_object(make_float(acos(to_float(dpeek())))));
}
void primitive_fasin(void)
{
maybe_garbage_collection();
2004-09-19 00:33:40 -04:00
drepl(tag_object(make_float(asin(to_float(dpeek())))));
}
void primitive_fatan(void)
{
maybe_garbage_collection();
2004-09-19 00:33:40 -04:00
drepl(tag_object(make_float(atan(to_float(dpeek())))));
}
void primitive_fatan2(void)
{
2004-12-18 20:24:46 -05:00
double x, y;
maybe_garbage_collection();
y = to_float(dpop());
x = to_float(dpop());
dpush(tag_object(make_float(atan2(x,y))));
}
void primitive_fcos(void)
{
maybe_garbage_collection();
2004-09-19 00:33:40 -04:00
drepl(tag_object(make_float(cos(to_float(dpeek())))));
}
void primitive_fexp(void)
{
maybe_garbage_collection();
2004-09-19 00:33:40 -04:00
drepl(tag_object(make_float(exp(to_float(dpeek())))));
}
void primitive_fcosh(void)
{
maybe_garbage_collection();
2004-09-19 00:33:40 -04:00
drepl(tag_object(make_float(cosh(to_float(dpeek())))));
}
void primitive_flog(void)
{
maybe_garbage_collection();
2004-09-19 00:33:40 -04:00
drepl(tag_object(make_float(log(to_float(dpeek())))));
}
void primitive_fpow(void)
{
2004-12-18 20:24:46 -05:00
double x, y;
maybe_garbage_collection();
y = to_float(dpop());
x = to_float(dpop());
dpush(tag_object(make_float(pow(x,y))));
}
void primitive_fsin(void)
{
maybe_garbage_collection();
2004-09-19 00:33:40 -04:00
drepl(tag_object(make_float(sin(to_float(dpeek())))));
}
void primitive_fsinh(void)
{
maybe_garbage_collection();
2004-09-19 00:33:40 -04:00
drepl(tag_object(make_float(sinh(to_float(dpeek())))));
}
void primitive_fsqrt(void)
{
maybe_garbage_collection();
2004-09-19 00:33:40 -04:00
drepl(tag_object(make_float(sqrt(to_float(dpeek())))));
}