fullung2
2007-07-29 09:25:52 UTC
Hello all
I was recently using JUnit 4.3.1 (included with Eclipse 3.3) to test
some numerical code. During this process I noticed that the following
test would pass:
assertEquals(1.0f, 1.999f)
but that this test would fail:
assertEquals(1.0f, 2.0f)
The assertEquals being called here is assertEquals(Object, Object).
Autoboxing kicks in and constructs two Floats. The comparison is then
done in isEquals(Object expected, Object actual) where the check is
((Number) expected).longValue() == ((Number) actual).longValue()
I realise that when comparing floating point values, one should
generally use a delta, i.e., you want to call assertEquals(double,
double, double), and in the case where you want the numbers to be
exactly equal, you could use a zero delta.
However, it seems rather counter-intuitive that assertEquals(Object,
Object) allows these completely different objects to compare equal.
Any thoughts?
Regards,
Albert Strasheim
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/junit/
<*> Your email settings:
Individual Email | Traditional
<*> To change settings online go to:
http://groups.yahoo.com/group/junit/join
(Yahoo! ID required)
<*> To change settings via email:
mailto:junit-***@yahoogroups.com
mailto:junit-***@yahoogroups.com
<*> To unsubscribe from this group, send an email to:
junit-***@yahoogroups.com
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/
I was recently using JUnit 4.3.1 (included with Eclipse 3.3) to test
some numerical code. During this process I noticed that the following
test would pass:
assertEquals(1.0f, 1.999f)
but that this test would fail:
assertEquals(1.0f, 2.0f)
The assertEquals being called here is assertEquals(Object, Object).
Autoboxing kicks in and constructs two Floats. The comparison is then
done in isEquals(Object expected, Object actual) where the check is
((Number) expected).longValue() == ((Number) actual).longValue()
I realise that when comparing floating point values, one should
generally use a delta, i.e., you want to call assertEquals(double,
double, double), and in the case where you want the numbers to be
exactly equal, you could use a zero delta.
However, it seems rather counter-intuitive that assertEquals(Object,
Object) allows these completely different objects to compare equal.
Any thoughts?
Regards,
Albert Strasheim
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/junit/
<*> Your email settings:
Individual Email | Traditional
<*> To change settings online go to:
http://groups.yahoo.com/group/junit/join
(Yahoo! ID required)
<*> To change settings via email:
mailto:junit-***@yahoogroups.com
mailto:junit-***@yahoogroups.com
<*> To unsubscribe from this group, send an email to:
junit-***@yahoogroups.com
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/