[TIP] doctest and floats

Tim Head betatim at gmail.com
Mon Mar 10 15:45:37 PDT 2008


Hi

On 10/03/2008, Ben Finney <bignose+hates-spam at benfinney.id.au> wrote:
> "Tim Head" <betatim at gmail.com> writes:
>
<snip>
>  Instead, do what people *should* be doing to compare floats anyway:
>  never compare floats for equality, instead compare the difference
>  against an epsilon value.
>
>     >>> epsilon = 1e-12
>     >>> want = 0.1
>     >>> got = foo(0.1)
>     >>> abs(want - got) < epsilon
>     True
>

Yes, obviously that is the right way to do it, the problem with this
it requires four lines to test the return value of a function. Which
considering we are in doctest world means someone reading the docs
needs to wade through four lines (and comprehend that all they do is
check (near) equality) which IMHO reduces the benefit of doctests to
near zero ;]]

To make matters even worse, what I really was after was a nifty way to
compare an array of floats ...

I was hoping there would be a way to (ab)use the ellipsis flag to say
"I dont care about the digits after this one", obviously not a smart
thing to do for real tests, but humans would understand it.

cheers,
tim

>  --
>   \              "Ignorance more frequently begets confidence than does |
>   `\           knowledge." —Charles Darwin, _The Descent of Man_, 1871 |
>  _o__)                                                                  |
>  Ben Finney
>
>
>  _______________________________________________
>  testing-in-python mailing list
>  testing-in-python at lists.idyll.org
>  http://lists.idyll.org/listinfo/testing-in-python
>


-- 
http://tim.jottit.com/



More information about the testing-in-python mailing list