Enum on Float/Double

Glynn Clements glynn.clements at virgin.net
Thu Oct 23 00:54:37 EDT 2003


Hal Daume III wrote:

> This works great for when x/=0...is there a good (Haskell) solution for 
> the smallest positive float?

I think that the following are correct for the smallest normalised
Float and Double values:

	Prelude> encodeFloat 1 (fst (floatRange (0 :: Float)) - 1) :: Float
	1.1754944e-38
	
	Prelude> encodeFloat 1 (fst (floatRange (0 :: Double)) - 1) :: Double
	2.2250738585072014e-308

They (roughly) agree with FLT_MIN and DBL_MIN from <float.h>:

	#define FLT_MIN 1.17549435e-38F
	...
	#define DBL_MIN 2.2250738585072014e-308

Furthermore, these values aren't denormalised, but reducing the
exponent by one gives a denormalised value:

	Prelude> isDenormalized (encodeFloat 1 (fst (floatRange (0 :: Float)) - 1) :: Float)
	False
	Prelude> isDenormalized (encodeFloat 1 (fst (floatRange (0 :: Float)) - 2) :: Float)
	True
	Prelude> isDenormalized (encodeFloat 1 (fst (floatRange (0 :: Double)) - 1) :: Double)
	False
	Prelude> isDenormalized (encodeFloat 1 (fst (floatRange (0 :: Double)) - 2) :: Double)
	True

These appear to give the smallest possible Float/Double values:

	Prelude> encodeFloat 1 (fst (floatRange (0 :: Double)) - floatDigits (0 :: Double)) :: Double
	5.0e-324
	Prelude> encodeFloat 1 (fst (floatRange (0 :: Float)) - floatDigits (0 :: Float)) :: Float
	1.0e-45

Reducing the exponent by 1 gives 0.0:

	Prelude> encodeFloat 1 (fst (floatRange (0 :: Float)) - floatDigits (0 :: Float) - 1) :: Float
	0.0
	Prelude> encodeFloat 1 (fst (floatRange (0 :: Double)) - floatDigits (0 :: Double) - 1) :: Double
	0.0

-- 
Glynn Clements <glynn.clements at virgin.net>


More information about the Haskell mailing list