Is there any way in Haskell to get the constant that is the largest and smallest possible positive rational number greater than zero that can be represented by doubles?
+2
A:
GHC.Float has the function [floatRange][2]
:
floatRange :: a -> (Int, Int) Source
a constant function, returning the lowest and highest values the exponent may assume
which should be what you want.
ire_and_curses
2009-11-23 00:39:16
Umm? `Prelude.floatRange` works on all instances of `class RealFloat`, including `Double`.
ephemient
2009-11-23 01:40:13
@ephemient: I understand. Thanks for the explanation.
ire_and_curses
2009-11-23 02:44:53
A:
The fraction part of a double is 52 bits, so it can represent integers up to 4503599627370495 without losing precision.
The smallest possible positive non-zero integer would of course be one.
Guffa
2009-11-23 00:41:58
+5
A:
maxNonInfiniteFloat :: RealFloat a => a -> a
maxNonInfiniteFloat a = encodeFloat m n where
b = floatRadix a
e = floatDigits a
(_, e') = floatRange a
m = b ^ e - 1
n = e' - e
minPositiveFloat :: RealFloat a => a -> a
minPositiveFloat a = encodeFloat 1 $ fst (floatRange a) - floatDigits a
ephemient
2009-11-23 01:39:19