parser: float1 == float2 uses machine epsilon by default

pull/2166/head
hazohelet 2019-09-30 00:27:53 +09:00 committed by Alexander Medvednikov
parent 21f9dc6b7c
commit 6d483c0a56
4 changed files with 35 additions and 5 deletions

View File

@ -1597,11 +1597,14 @@ fn (p mut Parser) bterm() string {
p.expected_type = typ
is_str := typ=='string' && !p.is_sql
is_ustr := typ=='ustring'
is_float := typ=='f64' || typ=='f32'
expr_type := typ
tok := p.tok
// if tok in [ .eq, .gt, .lt, .le, .ge, .ne] {
if tok == .eq || tok == .gt || tok == .lt || tok == .le || tok == .ge || tok == .ne {
p.fgen(' ${p.tok.str()} ')
if (is_str || is_ustr) && !p.is_js {
if ((is_float && tok == .eq) || (is_str || is_ustr)) && !p.is_js {
p.gen(',')
}
else if p.is_sql && tok == .eq {
@ -1655,6 +1658,10 @@ fn (p mut Parser) bterm() string {
case Token.lt: p.cgen.set_placeholder(ph, 'ustring_lt(')
}
}
if is_float && tok == .eq {
p.gen(')')
p.cgen.set_placeholder(ph, '${expr_type}_eq(')
}
}
return typ
}

View File

@ -29,6 +29,15 @@ pub fn ptr_str(ptr voidptr) string {
pub fn (a f64) eq(b f64) bool {
return C.fabs(a - b) <= C.DBL_EPSILON
}
pub fn (a f32) eq(b f32) bool {
return C.fabsf(a - b) <= C.FLT_EPSILON
}
pub fn (a f64) eqbit(b f64) bool {
return C.DEFAULT_EQUAL(a, b)
}
pub fn (a f32) eqbit(b f32) bool {
return C.DEFAULT_EQUAL(a, b)
}
// fn (nn i32) str() string {
// return i

View File

@ -11,6 +11,20 @@ fn test_const() {
assert u == 1 // make sure this works without the cast
}
fn test_float_equal_operator() {
mut a := f32(1)
a += 0.000001
a -= 0.000001
assert a == 1
assert !a.eqbit(1)
a = f64(1)
a += 0.000001
a -= 0.000001
assert a == 1
assert !a.eqbit(1)
}
fn test_str_methods() {
assert i8(1).str() == '1'
assert i8(-1).str() == '-1'

View File

@ -37,14 +37,14 @@ fn test_factorial() {
fn test_erf() {
assert math.erf(0) == 0
assert (math.erf(1.5) + math.erf(-1.5)).eq(0)
assert math.erf(1.5) + math.erf(-1.5) == 0
assert math.erfc(0) == 1
assert (math.erf(2.5) + math.erfc(2.5)).eq(1)
assert (math.erfc(3.6) + math.erfc(-3.6)).eq(2)
assert math.erf(2.5) + math.erfc(2.5) == 1
assert math.erfc(3.6) + math.erfc(-3.6) == 2
}
fn test_gamma() {
assert math.gamma(1) == 1
assert math.gamma(5) == 24
assert math.log_gamma(4.5).eq(math.log(math.gamma(4.5)))
assert math.log_gamma(4.5) == math.log(math.gamma(4.5))
}