1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: do comparison of f32/f64 values for == and != by calling f64_eq(a,b) and f64_ne(a,b)

This commit is contained in:
Delyan Angelov 2020-04-08 03:49:20 +03:00
parent f93445a022
commit 3bad02abdd
3 changed files with 36 additions and 18 deletions

View File

@ -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',

View File

@ -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)

View File

@ -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 {