Plan 9 from Bell Labs’s /usr/web/sources/contrib/fernan/nhc98/tests/nofib/real/hidden/Numbers.hs

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


module Numbers(Number) where
data Number = Tolerant Float
-- if you are not using the Chalmers Haskell B compiler then remove the @
{- Number is the same as Float except that Number uses a comparison
   tolerance eps.
-}
instance Eq Number where
	Tolerant a == Tolerant b	= abs(a-b) < eps
	Tolerant a /= Tolerant b	= abs(a-b) > eps
instance Ord Number where
	Tolerant a <= Tolerant b	= a-eps < b
	Tolerant a < Tolerant b		= a < b-eps
instance Num Number where
	Tolerant a + Tolerant b		= Tolerant (a+b)
	Tolerant a - Tolerant b		= Tolerant (a-b)
	Tolerant a * Tolerant b		= Tolerant (a*b)
	negate (Tolerant a)		= Tolerant (-a)
	fromInteger n			= Tolerant (fromInteger n)
instance Fractional Number where
	Tolerant a / Tolerant b		= Tolerant (a/b)
instance Floating Number where
	sqrt (Tolerant a)		= Tolerant (sqrt a)
{- Allow both integral and floating denotations for numbers -}
instance Read Number where
	readsPrec p s = [(Tolerant n,t) | (n,t) <- readsPrec p s] -- ++
			--[(Tolerant (fromInteger n),t) | (n,t) <- readsPrec p s]
instance Show Number where
	showsPrec p (Tolerant x) = showsPrec p x
eps	= 0.0001 :: Float

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.