Skip to content

Commit

Permalink
Merge pull request #594 from kpoeck/feature-ansi-numbers
Browse files Browse the repository at this point in the history
resolve many issues in ansi numbers tests

E.g. ratio and complex normalization.
  • Loading branch information
Bike committed Sep 10, 2018
2 parents 95536d3 + be389a4 commit 0954c9c
Show file tree
Hide file tree
Showing 7 changed files with 387 additions and 59 deletions.
4 changes: 2 additions & 2 deletions include/clasp/core/num_co.h
Expand Up @@ -31,9 +31,9 @@ THE SOFTWARE.
#include <math.h>

namespace core {


Float_sp cl__float(Real_sp x, T_sp y);
Real_mv cl__integer_decode_float(Float_sp x);

int clasp_signbit(Number_sp x);

};
Expand Down
28 changes: 26 additions & 2 deletions include/clasp/core/numbers.h
Expand Up @@ -47,6 +47,21 @@
#include <clasp/core/bignum.fwd.h>
#include <clasp/core/numbers.fwd.h>

//Class Hierarchy
//Number_0
// Real_O
// Rational_O
// Integer_O
// Fixnum_dummy_O (immediate)
// Bignum_O
// Ratio_O
// Float_O
// ShortFloat_O
// SingleFloat_dummy_O (immediate)
// DoubleFloat_O
// LongFloat_O
// Complex_O

#define CLASP_PI_D 3.14159265358979323846264338327950288
#define CLASP_PI_L 3.14159265358979323846264338327950288l
#define CLASP_PI2_D 1.57079632679489661923132169163975144
Expand Down Expand Up @@ -141,6 +156,7 @@ namespace core
SingleFloat_sp clasp_make_single_float(float d);
DoubleFloat_sp clasp_make_double_float(double d);
Number_sp clasp_log1_complex_inner(Number_sp r, Number_sp i);
void clasp_report_divide_by_zero(Number_sp x);
};

namespace core {
Expand Down Expand Up @@ -800,6 +816,13 @@ namespace core {
return Ratio_O::create(num, denom);
}

inline Rational_sp clasp_make_rational(Integer_sp num, Integer_sp denom) {
// will check whether denom = 1
return Rational_O::create(num, denom);
}

Number_sp clasp_make_complex (Real_sp r, Real_sp i);

inline Fixnum_sp clasp_make_fixnum(gc::Fixnum i) {
return make_fixnum(i);
}
Expand Down Expand Up @@ -1079,8 +1102,9 @@ namespace core {

inline Number_sp clasp_reciprocal(Number_sp x) {
if (x.fixnump() ) {
if ( x.unsafe_fixnum() == 1 ) return x;
return Ratio_O::create(clasp_make_fixnum((Fixnum)1),x);
if (x.unsafe_fixnum() == 1) return x;
if (x.unsafe_fixnum() == 0) clasp_report_divide_by_zero(x);
return Rational_O::create(clasp_make_fixnum((Fixnum)1),x);
} else if (x.single_floatp()) {
float f = x.unsafe_single_float();
return clasp_make_single_float(1.0 / f);
Expand Down
13 changes: 7 additions & 6 deletions src/core/lispReader.cc
Expand Up @@ -833,12 +833,13 @@ T_sp interpret_token_or_throw_reader_error(T_sp sin, Token &token, bool only_dot
// interpret ratio
SimpleString_sp sRatioStr = tokenStr(sin,token, start - token.data());
std::string ratioStr = sRatioStr->get_std_string();
if (ratioStr[0] == '+') {
Ratio_sp rp = Ratio_O::create(ratioStr.substr(1, ratioStr.size() - 1).c_str());
return rp;
}
Ratio_sp r = Ratio_O::create(ratioStr.c_str());
return r;
if (ratioStr[0] == '+')
ratioStr = ratioStr.substr(1, ratioStr.size() - 1);
vector<string> parts = split(ratioStr.c_str(), "/");
ASSERT(parts.size() == 2);
Integer_sp num = Integer_O::create(parts[0]);
Integer_sp denom = Integer_O::create(parts[1]);
return Rational_O::create(num, denom);
break;
}
case tfloat0:
Expand Down
33 changes: 19 additions & 14 deletions src/core/num_co.cc
Expand Up @@ -257,7 +257,7 @@ Real_mv clasp_floor2(Real_sp x, Real_sp y) {
v0 = mv_v0;
Integer_sp t1 = gc::As<Integer_sp>(mv_v0.valueGet_(1));
// v1 = clasp_make_ratio(clasp_nth_value(the_env, 1), y->den());
v1 = clasp_make_ratio(t1, ry->den());
v1 = clasp_divide(t1, ry->den());
break;
}
case number_SingleFloat: /* FIX / SF */
Expand Down Expand Up @@ -314,7 +314,7 @@ Real_mv clasp_floor2(Real_sp x, Real_sp y) {
Real_mv mv_v0 = clasp_floor2(gc::As<Real_sp>(clasp_times(x, ry->den())), ry->num());
v0 = mv_v0;
Integer_sp tv1 = gc::As<Integer_sp>(mv_v0.valueGet_(1));
v1 = clasp_make_ratio(tv1, ry->den());
v1 = clasp_divide(tv1, ry->den());
break;
}
case number_SingleFloat: { /* BIG / SF */
Expand Down Expand Up @@ -353,23 +353,28 @@ Real_mv clasp_floor2(Real_sp x, Real_sp y) {
{
Ratio_sp rx = gc::As<Ratio_sp>(x);
Ratio_sp ry = gc::As<Ratio_sp>(y);
Real_mv mv_v0 = clasp_floor2(gc::As<Real_sp>(clasp_times(rx->num(), ry->den())),
// rx-num/rx-den / ry-num/ry-den = rx-num* ry-den / rx-den*ry-num
Real_mv temp = clasp_floor2(gc::As<Real_sp>(clasp_times(rx->num(), ry->den())),
gc::As<Real_sp>(clasp_times(rx->den(), ry->num())));
v0 = mv_v0;
Integer_sp tv1 = gc::As<Integer_sp>(mv_v0.valueGet_(1));
v1 = clasp_make_ratio(tv1, gc::As<Integer_sp>(clasp_times(rx->den(), ry->den())));
// do I need here to access the first value?
// where on earth is Real_mv defined
Integer_sp secondValue = gc::As<Integer_sp>(temp.valueGet_(1));
v0 = temp;
v1 = Rational_O::create(secondValue, clasp_times(rx->den(), ry->den()));
break;
}
default: /* RAT / ANY */
{
Ratio_sp rx = gc::As<Ratio_sp>(x);
Real_mv mv_v0 = clasp_floor2(rx->num(), gc::As<Real_sp>(clasp_times(rx->den(), y)));
v0 = mv_v0;
Number_sp tv1 = gc::As<Number_sp>(mv_v0.valueGet_(1));
v1 = gc::As<Real_sp>(clasp_divide(tv1, rx->den()));
// rx-num/rx-den / y == (floor rx-num (* rx-den y)
Real_mv temp = clasp_floor2(rx->num(), gc::As<Real_sp>(clasp_times(rx->den(), y)));
Real_sp secondValue = gc::As<Real_sp>(temp.valueGet_(1));
v0 = temp;
v1 = clasp_divide(secondValue, rx->den());
break;
}
}
break;
case number_SingleFloat: { /* SF / ANY */
float n = clasp_to_double(y);
float p = clasp_single_float(x) / n;
Expand Down Expand Up @@ -503,7 +508,7 @@ Real_mv clasp_ceiling2(Real_sp x, Real_sp y) {
Real_mv mv_v = clasp_ceiling2(gc::As<Real_sp>(clasp_times(x, ry->den())), ry->num());
v0 = mv_v;
Integer_sp tv1 = gc::As<Integer_sp>(mv_v.valueGet_(1));
v1 = clasp_make_ratio(tv1, ry->den());
v1 = Rational_O::create(tv1, ry->den());
break;
}
case number_SingleFloat: { /* FIX / SF */
Expand Down Expand Up @@ -556,7 +561,7 @@ Real_mv clasp_ceiling2(Real_sp x, Real_sp y) {
Real_mv mv_v = clasp_ceiling2(gc::As<Real_sp>(clasp_times(x, ry->den())), ry->num());
v0 = mv_v;
Integer_sp tv1 = gc::As<Integer_sp>(mv_v.valueGet_(1));
v1 = clasp_make_ratio(tv1, ry->den());
v1 = Rational_O::create(tv1, ry->den());
break;
}
case number_SingleFloat: { /* BIG / SF */
Expand Down Expand Up @@ -600,7 +605,7 @@ Real_mv clasp_ceiling2(Real_sp x, Real_sp y) {
gc::As<Real_sp>(clasp_times(rx->den(), ry->num())));
v0 = mv_v;
Integer_sp tv1 = gc::As<Integer_sp>(mv_v.valueGet_(1));
v1 = clasp_make_ratio(tv1, gc::As<Integer_sp>(clasp_times(rx->den(), ry->den())));
v1 = Rational_O::create(tv1, gc::As<Integer_sp>(clasp_times(rx->den(), ry->den())));
break;
}
default: /* RAT / ANY */
Expand Down Expand Up @@ -1145,7 +1150,7 @@ CL_LAMBDA(r &optional (i 0));
CL_DECLARE();
CL_DOCSTRING("complex");
CL_DEFUN Complex_sp cl__complex(Real_sp r, Real_sp i) {
return Complex_O::create(r, i);
return clasp_make_complex(r, i);
}

CL_LAMBDA(x);
Expand Down

0 comments on commit 0954c9c

Please sign in to comment.