Module Prelude.PreludeDecimal

Decimal numbers, after Douglas Crockfords idea See http://dec64.com/

Imports

Table of Content

Definitions

>> ∷ Long → Int → Long
pure native >>
<< ∷ Long → Int → Long
pure native <<
& ∷ Long → Long → Long
pure native &
¦ ∷ Long → Long → Long
pure native |
data Decimal = pure native long

Member Functions

coefficient ∷ Decimal → Long

extract the coefficient from a Decimal

exponent ∷ Decimal → Int

extract the exponent, sign extended in an Int

fromBits ∷ Long → Decimal
pure native (long)

Interpret the bits of the Long value as a Decimal This is a no-operation, as Decimals are Longs and every Long value is a valid Decimal (though it might not be a number, see also Decimal.nan).

isNaN ∷ Decimal → Bool

tell if a Decimal is not a number. This works for all 2^56 NaN values.

isZero ∷ Decimal → Bool

tell if a Decimal is 0. This works for all 255 0 values.

maxCoefficient ∷ Long
pure native 0x007FFFFFFFFFFFFFL

the largest possible coefficient is 36028797018963967

maxExponent ∷ Int
pure native 127

the largest exponent is 127

minCoefficient ∷ Long
pure native 0xFF80000000000000L

the smalles possible coefficient is -36028797018963968

minExponent ∷ Int
pure native 0xffffff81

the smallest exponent is -127

nan ∷ Decimal
pure native 128L

The canonical not a number value.

Note that there are 2^56 NaN values, all comparing equal.

one ∷ Decimal
pure native 0x100L

The canonical Decimal 1

pack ∷ Long → Int → Decimal

construct a Decimal from a coefficient and an exponent

This will be Decimal.nan if the coefficient is not in the range minCoefficient .. maxCoefficient or the exponent is not in the range minExponent .. maxExponent, even if the value could be represented. Thus:

 Decimal.pack 36028797018963967 0     == 36028797018963967z
 Decimal.pack 360287970189639670 (-1) == nan
sign ∷ Decimal → Int

the sign of a Decimal (either -1, 0 or 1)

toBits ∷ Decimal → Long
pure native (long)

Interpret the bits of a Decimal as Long value. The result will look like this

 mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm eeee eeee
         54        48        40        32        24        16        8 7        0
  • The 8 e bits 0..7 are the exponent in 2's complement encoding
  • The 56 m bits 8..63 are the coefficient in 2's complement encodeing
  • an exponent of -128 (0x80) signals not-a-number (NaN)
  • the value of a number that is not NaN is coefficient*(exp^10)

Note that there are usually several encodings for one and the same value. 1, for example, could be 0x100 or 0xAFF (10E-1) Likewise 5 could be represented as 5e0, 50e-1, 0.5e1 and so forth.

This is a no-operation, as Decimals are Longs. However, the Frege compiler sees them as totally unrelated types (and rightly so).

zero ∷ Decimal
pure native 0L

The canonical Decimal 0

Functions and Values by Type

Long → Int → Long

<<, >>

Long → Int → Decimal

Decimal.pack

Long → Long → Long

&, ¦

Long → Decimal

Decimal.fromBits

Decimal → Bool

Decimal.isNaN, Decimal.isZero

Decimal → Int

Decimal.sign, Decimal.exponent

Decimal → Long

Decimal.toBits, Decimal.coefficient

Int

Decimal.minExponent, Decimal.maxExponent

Long

Decimal.minCoefficient, Decimal.maxCoefficient

Decimal

Decimal.zero, Decimal.one, Decimal.nan

Valid HTML 4.01 Strict