mirror of
https://github.com/janet-lang/janet
synced 2025-01-24 14:16:52 +00:00
Actually got the comparisons working for s64 (still need to fix u64)
This commit is contained in:
parent
01837f2bb6
commit
3e423722c6
@ -197,6 +197,10 @@ static Janet cfun_it_u64_new(int32_t argc, Janet *argv) {
|
|||||||
return janet_wrap_u64(janet_unwrap_u64(argv[0]));
|
return janet_wrap_u64(janet_unwrap_u64(argv[0]));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int64_t compare_double(double x, double y) {
|
||||||
|
return (x < y) ? -1 : ((x > y) ? 1 : 0);
|
||||||
|
}
|
||||||
|
|
||||||
static Janet cfun_it_s64_compare(int32_t argc, Janet *argv) {
|
static Janet cfun_it_s64_compare(int32_t argc, Janet *argv) {
|
||||||
janet_fixarity(argc, 2);
|
janet_fixarity(argc, 2);
|
||||||
if (janet_is_int(argv[0]) != JANET_INT_S64)
|
if (janet_is_int(argv[0]) != JANET_INT_S64)
|
||||||
@ -206,9 +210,20 @@ static Janet cfun_it_s64_compare(int32_t argc, Janet *argv) {
|
|||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
case JANET_NUMBER : {
|
case JANET_NUMBER : {
|
||||||
double y = round(janet_unwrap_number(argv[1]));
|
double y = janet_unwrap_number(argv[1]);
|
||||||
double dx = round((double) x); //double trouble?
|
if (isnan(y)) {
|
||||||
return janet_wrap_number(dx < y ? -1 : (dx > y ? 1 : 0));
|
return janet_wrap_number(0); // per python compare function
|
||||||
|
} else if ((y > ((double) -MAX_INT_IN_DBL)) && (y < ((double) MAX_INT_IN_DBL))) {
|
||||||
|
double dx = (double) x;
|
||||||
|
return janet_wrap_number(compare_double(dx, y));
|
||||||
|
} else if (y > ((double) INT64_MAX)) {
|
||||||
|
return janet_wrap_number(1);
|
||||||
|
} else if (y < ((double) INT64_MIN)) {
|
||||||
|
return janet_wrap_number(-1);
|
||||||
|
} else {
|
||||||
|
int64_t yi = (int64_t) y;
|
||||||
|
return janet_wrap_number((x < yi) ? -1 : ((x > yi) ? 1 : 0));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
case JANET_ABSTRACT: {
|
case JANET_ABSTRACT: {
|
||||||
void *abst = janet_unwrap_abstract(argv[1]);
|
void *abst = janet_unwrap_abstract(argv[1]);
|
||||||
|
@ -389,7 +389,7 @@
|
|||||||
# test polymorphic compare with int/u64 and int/s64
|
# test polymorphic compare with int/u64 and int/s64
|
||||||
(def MAX_INT_64_STRING "9223372036854775807")
|
(def MAX_INT_64_STRING "9223372036854775807")
|
||||||
(def MAX_UINT_64_STRING "18446744073709551615")
|
(def MAX_UINT_64_STRING "18446744073709551615")
|
||||||
(def MAX_INT_IN_DBL_STRING "9007199254740992")
|
(def MAX_INT_IN_DBL_STRING "9007199254740991")
|
||||||
(assert (= 0 (compare (int/u64 3) 3)) "compare number to int/u64")
|
(assert (= 0 (compare (int/u64 3) 3)) "compare number to int/u64")
|
||||||
(assert (= -1 (compare (int/u64 3) 4)) "compare number to int/u64 less")
|
(assert (= -1 (compare (int/u64 3) 4)) "compare number to int/u64 less")
|
||||||
(assert (= 0 (compare 3 (int/u64 3))) "compare number to int/u64")
|
(assert (= 0 (compare 3 (int/u64 3))) "compare number to int/u64")
|
||||||
@ -410,10 +410,7 @@
|
|||||||
(assert (= -1 (compare (int/s64 MAX_INT_64_STRING) (int/u64 MAX_UINT_64_STRING))) "compare big ints")
|
(assert (= -1 (compare (int/s64 MAX_INT_64_STRING) (int/u64 MAX_UINT_64_STRING))) "compare big ints")
|
||||||
(assert (= 0 (compare (int/s64 MAX_INT_IN_DBL_STRING) (scan-number MAX_INT_IN_DBL_STRING))) "compare max int in double (1)")
|
(assert (= 0 (compare (int/s64 MAX_INT_IN_DBL_STRING) (scan-number MAX_INT_IN_DBL_STRING))) "compare max int in double (1)")
|
||||||
(assert (= 0 (compare (int/u64 MAX_INT_IN_DBL_STRING) (scan-number MAX_INT_IN_DBL_STRING))) "compare max int in double (2)")
|
(assert (= 0 (compare (int/u64 MAX_INT_IN_DBL_STRING) (scan-number MAX_INT_IN_DBL_STRING))) "compare max int in double (2)")
|
||||||
(assert (= 1 (compare (+ 2 (int/u64 MAX_INT_IN_DBL_STRING)) (scan-number MAX_INT_IN_DBL_STRING))) "compare max int in double (3)")
|
(assert (= 1 (compare (+ 1 (int/u64 MAX_INT_IN_DBL_STRING)) (scan-number MAX_INT_IN_DBL_STRING))) "compare max int in double (3)")
|
||||||
# Beware: This is a horrible effect of comparing doubles to integers
|
|
||||||
# int/64(MAX+1) should compare greater than the double, (but doesn't due to precision)
|
|
||||||
(assert (= 0 (compare (+ 1 (int/u64 MAX_INT_IN_DBL_STRING)) (scan-number MAX_INT_IN_DBL_STRING))) "compare max int in double (3)")
|
|
||||||
|
|
||||||
(end-suite)
|
(end-suite)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user