Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Split Linear.Epsilon into own package. #161

Closed
phadej opened this issue Jun 12, 2021 · 5 comments
Closed

Split Linear.Epsilon into own package. #161

phadej opened this issue Jun 12, 2021 · 5 comments

Comments

@phadej
Copy link
Contributor

phadej commented Jun 12, 2021

I find often needing it when doing something numeric (e.g. ad), but depending on linear feels wrong. A package (epsilon) would be very nice.

I'll probably will make it anyway, but it would be nice to dedup functionality on Hackage.

Another example is ApproxEq in aeson which does slightly different thing, but could reuse Epsilon class for implementations.

EDIT: also half could depend on it.

@RyanGlScott
Copy link
Collaborator

RyanGlScott commented Oct 5, 2021

I have also wondered about making Epsilon a standalone piece of functionality. For what it's worth, the somewhat commonly used (and lightweight) ieee754 library also has an epsilon method in the IEEE class. That being said, ieee754 and linear disagree on what particular values to use—for instance, ieee754 picks FLT_EPSILON has the value for Float, whereas linear picks 1e-6. Moreover, linear's Epsilon class can work for more complicated data types, such as Complex and length-indexed vectors, whereas it's unclear how these would be made instances of IEEE.

@phadej
Copy link
Contributor Author

phadej commented Oct 5, 2021

Yes, so ieee754 probably uses https://en.wikipedia.org/wiki/Machine_epsilon, but linear's nearZero is picking larger one (probably) to be more resilient about accumulated errors etc. The ApproxEq usage is common, eg.

tests/Plucker.hs:             , "quadrance" ~: nearZero (quadranceToOrigin ln2 - 10) ~?= True

testing that two numbers are equal. (And I think that we should check for relative error, above it's not visible as 10 is not extremely large nor extremely small number)

I fail to find any reliable source saying "Numerically stable computations using floats (respectively doubles) can be generally trusted to have N digit accuracy", I would use that for precision of ApproxEq. The machine epsilon is too precise for that.

@AliceRixte
Copy link

I also support the idea of splitting Linear.Epsilon in its own package. As soon as one wants to test functions on floats we need this.

I also think it would be great to be able to adjust the epsilon value depending on the need, for instance in QuickCheck it would be great to be able to specify some epsilon to explicitely say what error is fine.

I just made a PR #184 that adds a class MkEpsilon that provides a particular value for epsilon. It works nicely in conjunction with nearZero, IMHO.

@phadej I couldn't find the class ApproxEq from aeson. Was it removed ?

@phadej
Copy link
Contributor Author

phadej commented Jan 31, 2025

@AliceRixte ApproxEq is defined and used in aeson's tests only.

@phadej
Copy link
Contributor Author

phadej commented Jan 31, 2025

FWIW, I don't think that Epsilon is a right thing to do. When comparing floating point values, we either have some concrete precision requirements (say 0.0001%) or something like https://en.wikipedia.org/wiki/Unit_in_the_last_place, FLT_EPSILON is ups(1) so makes sense only for values near 1.

Therefore I'm closing this, I have no good idea of what I want exactly, but I know that's not Epsilon as in linear.

@phadej phadej closed this as completed Jan 31, 2025
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants