diff --git a/cmd/tools/vtest-fixed.v b/cmd/tools/vtest-fixed.v index 1fab2b5714..4a84049928 100644 --- a/cmd/tools/vtest-fixed.v +++ b/cmd/tools/vtest-fixed.v @@ -7,8 +7,6 @@ import v.pref const ( skip_test_files = [ 'vlib/arrays/arrays_test.v', - 'vlib/builtin/float_test.v', - 'vlib/builtin/int_test.v', 'vlib/builtin/map_test.v', 'vlib/cli/command_test.v', 'vlib/cli/flag_test.v', diff --git a/vlib/builtin/int_test.v b/vlib/builtin/int_test.v index f77bcc93b2..b604e0a9ee 100644 --- a/vlib/builtin/int_test.v +++ b/vlib/builtin/int_test.v @@ -12,16 +12,17 @@ fn test_const() { } fn test_float_equal_operator() { - mut a := f32(1) + b := f32(1.0) + mut a := f32(1.0) a += 0.000001 a -= 0.000001 - assert a == 1 - assert !a.eqbit(1) - assert !(a != 1) - assert a.nebit(1) + assert a == b + assert !a.eqbit(1.0) + assert !(a != f32(1.0)) + assert a.nebit(f32(1.0)) a += 0.000001 - assert !(a < 1) - assert !a.ltbit(1) + assert !(a < 1.0) + assert !a.ltbit(1.0) assert !(a <= 1) assert !a.lebit(1) assert a > 1 @@ -32,16 +33,16 @@ fn test_float_equal_operator() { a = f64(1) a += 0.000001 a -= 0.000001 - assert a == 1 - assert !a.eqbit(1) - assert !(a != 1) + assert a == f32(1.0) + assert !a.eqbit(f32(1.0)) + assert !(a != f32(1.0)) a += 0.000001 - assert !(a < 1) - assert !a.ltbit(1) - assert !(a <= 1) - assert !a.lebit(1) - assert a > 1 - assert a.gtbit(1) + assert !(a < f32(1)) + assert !a.ltbit(f32(1)) + assert !(a <= f32(1)) + assert !a.lebit(f32(1)) + assert a > f32(1) + assert a.gtbit(f32(1)) assert a >= 1 assert a.gebit(1) diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index 731a223e1b..d9d12947d6 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -1391,6 +1391,25 @@ fn (g mut Gen) infix_expr(node ast.InfixExpr) { g.expr(node.right) g.write('), $tmp, $elem_type_str)') } + } else if (node.left_type == node.right_type ) && node.left_type in [ table.f32_type_idx, table.f64_type_idx ] && node.op in [ .eq, .ne ] { + // floats should be compared with epsilon + if node.left_type == table.f64_type_idx { + if node.op == .eq { + g.write('f64_eq(') + } else { + g.write('f64_ne(') + } + }else{ + if node.op == .eq { + g.write('f32_eq(') + } else { + g.write('f32_ne(') + } + } + g.expr(node.left) + g.write(',') + g.expr(node.right) + g.write(')') } else { need_par := node.op in [.amp, .pipe, .xor] // `x & y == 0` => `(x & y) == 0` in C if need_par {