Module Prelude.PreludeDecimal

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

Imports

Table of Content

Definitions

>>LongIntLong
pure native >>
<<LongIntLong
pure native <<
&LongLongLong
pure native &
¦LongLongLong
pure native |
data Decimal = pure native long

Member Functions

coefficientDecimalLong

extract the coefficient from a Decimal

exponentDecimalInt

extract the exponent, sign extended in an Int

fromBitsLongDecimal
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).

isNaNDecimalBool

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

isZeroDecimalBool

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

maxCoefficientLong
pure native 0x007FFFFFFFFFFFFFL

the largest possible coefficient is 36028797018963967

maxExponentInt
pure native 127

the largest exponent is 127

minCoefficientLong
pure native 0xFF80000000000000L

the smalles possible coefficient is -36028797018963968

minExponentInt
pure native 0xffffff81

the smallest exponent is -127

nanDecimal
pure native 128L

The canonical not a number value.

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

oneDecimal
pure native 0x100L

The canonical Decimal 1

packLongIntDecimal

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
signDecimalInt

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

toBitsDecimalLong
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).

zeroDecimal
pure native 0L

The canonical Decimal 0

Functions and Values by Type

LongIntLong

<<, >>

LongIntDecimal

Decimal.pack

LongLongLong

&, ¦

LongDecimal

Decimal.fromBits

DecimalBool

Decimal.isNaN, Decimal.isZero

DecimalInt

Decimal.sign, Decimal.exponent

DecimalLong

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