views:

341

answers:

3

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
Umm? `Prelude.floatRange` works on all instances of `class RealFloat`, including `Double`.
ephemient
@ephemient: I understand. Thanks for the explanation.
ire_and_curses
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
i mis-spoke: i meant the smallest positive non-zero rational #.
Claudiu
+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