msakai / data-interval

Interval datatype, interval arithmetic, and interval-based containers for Haskell
Other
21 stars 9 forks source link

Amend recip so that recip (recip xs) == xs // {0} #21

Closed Bodigrim closed 4 years ago

Bodigrim commented 4 years ago

I was surprised to learn the behaviour of recip on intervals, containing 0:

recip [0,2]  = whole 
recip [-2,0] = whole 
recip [-2,2] = whole 

I'd expect that

recip [0,2]  = [1/2,+Inf)
recip [-2,0] = (-Inf,-1/2]
recip [-2,2] = (-Inf,-1/2] + [1/2, +Inf)

This expectation can be expressed as a property

recip (recip xs) == xs // {0}

The PR amends instances of Fractional for Interval and IntervalSet to comply with the property above. The instance for IntervalSet remains total. The instance for Interval throws DivideByZero, when 0 is an interior point (because in this case xs // {0} is not an interval but two adjacent intervals).

coveralls commented 4 years ago

Coverage Status

Coverage decreased (-0.2%) to 86.154% when pulling 1829f9e9429cd39393af3a0487e7b743b7fcfdaf on Bodigrim:recip-zero into 71917515ac187b6d319a6cc65bcfeb5719ecae4c on msakai:master.