mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
f64.eq()
fixes float comparison tests
This commit is contained in:
parent
e0b45e33ea
commit
375bc1352c
@ -4,6 +4,12 @@
|
||||
|
||||
module builtin
|
||||
|
||||
#include <float.h>
|
||||
|
||||
import const (
|
||||
DBL_EPSILON
|
||||
)
|
||||
|
||||
pub fn (d double) str() string {
|
||||
buf := malloc(sizeof(double) * 5 + 1)// TODO
|
||||
C.sprintf(buf, '%f', d)
|
||||
@ -28,6 +34,11 @@ pub fn ptr_str(ptr voidptr) string {
|
||||
return tos(buf, strlen(buf))
|
||||
}
|
||||
|
||||
// compare floats using C epsilon
|
||||
pub fn (a f64) eq(b f64) bool {
|
||||
return C.fabs(a - b) <= DBL_EPSILON
|
||||
}
|
||||
|
||||
// fn (nn i32) str() string {
|
||||
// return i
|
||||
// }
|
||||
|
@ -115,8 +115,8 @@ fn test_complex_abs() {
|
||||
mut c1 := cmplx.complex(3,4)
|
||||
assert c1.abs() == 5
|
||||
c1 = cmplx.complex(1,2)
|
||||
assert c1.abs() == math.sqrt(5)
|
||||
assert c1.abs() == c1.conjugate().abs()
|
||||
assert c1.abs().eq(math.sqrt(5))
|
||||
assert c1.abs().eq(c1.conjugate().abs())
|
||||
c1 = cmplx.complex(7,0)
|
||||
assert c1.abs() == 7
|
||||
}
|
||||
@ -125,17 +125,17 @@ fn test_complex_angle(){
|
||||
// Test is based on and verified from practice examples of Khan Academy
|
||||
// https://www.khanacademy.org/math/precalculus/imaginary-and-complex-numbers
|
||||
mut c := cmplx.complex(1, 0)
|
||||
assert c.angle() * 180 / math.Pi == 0
|
||||
assert (c.angle() * 180 / math.Pi).eq(0)
|
||||
c = cmplx.complex(1, 1)
|
||||
assert c.angle() * 180 / math.Pi == 45
|
||||
assert (c.angle() * 180 / math.Pi).eq(45)
|
||||
c = cmplx.complex(0, 1)
|
||||
assert c.angle() * 180 / math.Pi == 90
|
||||
assert (c.angle() * 180 / math.Pi).eq(90)
|
||||
c = cmplx.complex(-1, 1)
|
||||
assert c.angle() * 180 / math.Pi == 135
|
||||
assert (c.angle() * 180 / math.Pi).eq(135)
|
||||
c = cmplx.complex(-1, -1)
|
||||
assert c.angle() * 180 / math.Pi == -135
|
||||
assert (c.angle() * 180 / math.Pi).eq(-135)
|
||||
mut cc := c.conjugate()
|
||||
assert cc.angle() + c.angle() == 0
|
||||
assert (cc.angle() + c.angle()).eq(0)
|
||||
}
|
||||
|
||||
|
||||
|
@ -35,10 +35,10 @@ fn test_factorial() {
|
||||
|
||||
fn test_erf() {
|
||||
assert math.erf(0) == 0
|
||||
assert math.erf(1.5) + math.erf(-1.5) == 0
|
||||
assert (math.erf(1.5) + math.erf(-1.5)).eq(0)
|
||||
assert math.erfc(0) == 1
|
||||
assert math.erf(2.5) + math.erfc(2.5) == 1
|
||||
assert math.erfc(3.6) + math.erfc(-3.6) == 2
|
||||
assert (math.erf(2.5) + math.erfc(2.5)).eq(1)
|
||||
assert (math.erfc(3.6) + math.erfc(-3.6)).eq(2)
|
||||
}
|
||||
|
||||
fn test_gamma() {
|
||||
|
Loading…
Reference in New Issue
Block a user