rounding of floating-point numbers
Closed this issue · 12 comments
Is there a way to specify a rounding for floating point numbers? It is a bit inconvenient to specify all the numbers, i.e. 4243.827160493827 as result, when 4243.82 would suffice.
This would be the job of an expectation library, which is not in the initial scope of etd
.
The current expectation libraries ert-expectations
and the expect macro in buttercup
don't handle this, as far as I know.
When an approximate float is acceptable, we can use a function like this...
(defvar precision-factor 1.0e-3)
(defun approx-equal (x y)
(or (= x y)
(< (/ (abs (- x y))
(max (abs x) (abs y)))
precision-factor)))
(approx-equal 1.2321 1.2322) => t
Because defexamples
only match on =>
which is effectively equal
it's designed for 1:1 examples.
However, if I add a ~>
matcher meaning, output is approximately the right side that would be ok.
What do you think?
There would need to be an approximation var, like precision-factor
in the code above.
That would be cool! I like the ~> idea.
It would be even better if the precision factor could be a parameter to this matcher somehow, but it's also fine if we would set it in some variable beforehand.
TBH I don't want to dirty the example syntax very much. A few symbolic matchers is ok, but I'll start insisting on APL symbols ... (joking? possibly)
A let which encloses a group of tests should work for (working name) etd-float-approximation
I need to test it of course.
(better names are welcome!)
(let ((operator "~>"))
(pcase operator
("~>" (message "Erm, that's approximately what I thought"))
("=>" (message "Yeah, that's what I thought!"))))
pcase
does it better than cond
Single scalar float approximate comparison is working.
- Add support for flat lists.
- Add support for nested lists.
Oh wait, no it's not...
I don't know why, pcase
cases get nil'ed, cond
may be bulky, but it works.
Ok, looking good on https://github.com/emacsfodder/kurecolor/runs/8267010300?check_suite_focus=true
Approximation expects correct rounding, not simple truncation.
https://github.com/emacsfodder/kurecolor/actions/runs/3023245026
2 lists of floats can be approximately equal. (For now they can only be flat lists, not trees.)
I'll merge these changes into etd tomorrow.
(already merged the changes.)