algebra icon indicating copy to clipboard operation
algebra copied to clipboard

Add ApproxEq trait with asserts

Open brendanzab opened this issue 9 years ago • 8 comments

See sebcrozet/nalgebra#40

brendanzab avatar Nov 23 '14 06:11 brendanzab

This effectively adds some topology to this library. In particular, the trait effectively is a metric space M combined with some (small) constant ε∈M. Do floating point numbers form a metric space?

milibopp avatar Nov 23 '14 12:11 milibopp

NaN could be considered infinitely far from everything, in particular, from itself, so with that assumption, no.

huonw avatar Nov 23 '14 12:11 huonw

I really think we should just ignore special values and the various irregularities of floating point numbers. I we don't we could not use them for anything in this library because it would not even implement basic property-traits (e.g. no multiplicative associativity), nor structural-traits because of the lack of unique additive identity element (among thousands of other problems).

sebcrozet avatar Nov 23 '14 12:11 sebcrozet

Ok, I was not entirely sure about that, since one might want floating point numbers to fulfill weaker variants of these properties, e.g. multiplication and addition should be approximately associative. But looking at the literature it appears that the theory there is not entirely fleshed out. However we should at the very least document these inaccuracies when implementing traits for floating point types that do not actually fulfill these properties though.

Ignoring it has the downside that quickcheck-style theorem testing will not work on floating point types. I wonder if we could weaken the abstract mathematical properties, s.t. the weaker variants hold for floating point arithmetic and also are implied by the more strict ones. The question then is, of course, whether the stricter variants would see much use, when they are not implemented for floating-point types.

milibopp avatar Nov 23 '14 13:11 milibopp

@huonw that's not directly a problem, when you use another form of identity where NaN != NaN. But this may lead to other problems down the road, I guess.

milibopp avatar Nov 23 '14 13:11 milibopp

Should the macros be packaged in separate crate btw? Quickcheck does it that way and I think this approach makes more sense, if you look at the extern crate declarations, as you need to declare the library and its exported macros separately anyway.

milibopp avatar Dec 16 '14 10:12 milibopp

Yeah, I think an algebra_macros crate would be the right way to do it.

brendanzab avatar Dec 16 '14 11:12 brendanzab

Mind sebcrozet/nalgebra#60 for the implementation. The essence is to just ignore the least significant bits of the mantissa instead of specifying some arbitrary epsilon. This might also yield more consistent algebraic properties.

milibopp avatar Jan 03 '15 14:01 milibopp