GeeCON Prague 2014*
The moose likes Testing and the fly likes jUnit assert - what is delta? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Engineering » Testing
Bookmark "jUnit assert - what is delta?" Watch "jUnit assert - what is delta?" New topic
Author

jUnit assert - what is delta?

louise rochford
Ranch Hand

Joined: Apr 04, 2002
Posts: 119
I'm struggling to understand the API for junit.framework.Assert - probably because my understanding of Maths terminology is weak.
The javadocs state:
assertEquals(double expected, double actual, double delta)
Asserts that two doubles are equal concerning a delta.
What is delta? I think I'm supposed to pass in a double representing the limit that the actual & expected can vary by and still be considered equal.
I can't remember where I read this or if I made it up.
Is it true? Is it usual to make the acuracy of the delta value more precise than the others, e.g. expected = 4.5, actual = 5.30, delta = 0.009?
Thansk in advance,
Louise
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

The delta value is indeed the "error" or "uncertainty" allowed in the comparison. Comparing floating point numbers is tricky -- exact equality is hard to come by in many cases.
It's quite common for the delta to be much smaller than the actual values -- for no particular reason, I generally use 1e-8, and this works well.


[Jess in Action][AskingGoodQuestions]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
I'm struggling to understand the API for junit.framework.Assert - probably because my understanding of Maths terminology is weak.
Also because the JUnit javadoc is rather poor, for such a widely used system. Fortunately the source code is available.
In this case, delta is the maximum allowed absolute difference between actual and expected value. So the test is basically

It's quite common for the delta to be much smaller than the actual values -- for no particular reason, I generally use 1e-8, and this works well.
Unless the values you're comparing are large enough that 1e-8 is below the roundoff error for the datatype. There's usually a wide range of values that can work acceptably well for delta, but it does depend on what datatype we're using and what magnitude the expected value has; there are plenty of case where 1e-8 would be completely ineffectual.
Personally I find it much easier to compare floats and doubles using a relative error. Here's a method I'd use:

Here the test is essentially

I've found that 1e-15 seems reasonable for a double; 1e-7 for a float. But higher values may legitmately be necessary in many situations, depending on what types of calculations were involved in generating a number. Error propagation in numeric calculations is probably too complex for this post, so perhaps it's best to just realize that in many cases you can just set the relative higher if you keep getting errors with a given value. The question you really need to ask is, how much accuracy do you really need? 1e-15 is an extremely small relative error for most applications; often you'd be OK with it as high as 1e-3 or even 1e-2. Depends on the situation; hard to say more.


"I'm not back." - Bill Harding, Twister
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30586
    
154

Sometimes I cast to int to get around this. (When expecting doubles with no decimal parts)

Does anyone see any problems with this approach? besides the fact that it can't be used everywhere...


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
I think you're making your tests brittle for no real reason. It would be just as easy to provide a delta...


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Jeanne Boyarsky:
Sometimes I cast to int to get around this. (When expecting doubles with no decimal parts)

Does anyone see any problems with this approach?

Wouldn't the following pass? Would it be bad?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30586
    
154

Doh! Thanks guys.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
You're welcome!
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
My favorite language ever - REXX - has a SET NUMERIC FUZZ option that sets the compare precision for numbers. I just love the name!


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
louise rochford
Ranch Hand

Joined: Apr 04, 2002
Posts: 119
Thanks for all your help guys
Eilidh Troup
Greenhorn

Joined: May 02, 2012
Posts: 1

The assertEqualsRelative didn't work when a negative value is expected. Here it is with Math.abs to fix it.

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: jUnit assert - what is delta?