From 2c158f949192e632c9e70d268f2a3ee9a95486dd Mon Sep 17 00:00:00 2001 From: "K. Lange" Date: Wed, 15 Dec 2021 17:31:15 +0900 Subject: [PATCH] Fixup bad cast when comparing int to float --- src/vm.c | 4 ++-- test/testIntFloatCompare.krk | 14 ++++++++++++++ test/testIntFloatCompare.krk.expect | 13 +++++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 test/testIntFloatCompare.krk create mode 100644 test/testIntFloatCompare.krk.expect diff --git a/src/vm.c b/src/vm.c index 6c8561d..e5d79a9 100644 --- a/src/vm.c +++ b/src/vm.c @@ -1508,10 +1508,10 @@ MAKE_BIT_OP(mod,%,rmod) /* not a bit op, but doesn't work on floating point */ KrkValue krk_operator_ ## name (KrkValue a, KrkValue b) { \ if (IS_INTEGER(a) && IS_INTEGER(b)) return BOOLEAN_VAL(AS_INTEGER(a) operator AS_INTEGER(b)); \ if (IS_FLOATING(a)) { \ - if (IS_INTEGER(b)) return BOOLEAN_VAL(AS_FLOATING(a) operator AS_INTEGER(b)); \ + if (IS_INTEGER(b)) return BOOLEAN_VAL(AS_FLOATING(a) operator (double)AS_INTEGER(b)); \ else if (IS_FLOATING(b)) return BOOLEAN_VAL(AS_FLOATING(a) operator AS_FLOATING(b)); \ } else if (IS_FLOATING(b)) { \ - if (IS_INTEGER(a)) return BOOLEAN_VAL(AS_INTEGER(a) operator AS_INTEGER(b)); \ + if (IS_INTEGER(a)) return BOOLEAN_VAL((double)AS_INTEGER(a) operator AS_FLOATING(b)); \ } \ return tryBind("__" #name "__", a, b, #operator, "__" #inv "__"); \ } diff --git a/test/testIntFloatCompare.krk b/test/testIntFloatCompare.krk new file mode 100644 index 0000000..6f7c520 --- /dev/null +++ b/test/testIntFloatCompare.krk @@ -0,0 +1,14 @@ +import math +print(1 < 1.2) +print(1 > 1.2) +print(1 < math.inf) +print(1 > math.inf) +print(1 < math.nan) +print(1 > math.nan) +print('--') +print(1.2 > 1) +print(1.2 < 1) +print(math.inf > 1) +print(math.inf < 1) +print(math.nan > 1) +print(math.nan < 1) diff --git a/test/testIntFloatCompare.krk.expect b/test/testIntFloatCompare.krk.expect new file mode 100644 index 0000000..a1ec45a --- /dev/null +++ b/test/testIntFloatCompare.krk.expect @@ -0,0 +1,13 @@ +True +False +True +False +False +False +-- +True +False +True +False +False +False