78 lines
2.6 KiB
Markdown
78 lines
2.6 KiB
Markdown
---
|
|
title: "DEC64 Numbers"
|
|
description: "Decimal floating point representation"
|
|
---
|
|
|
|
## Overview
|
|
|
|
ƿit uses DEC64 as its number format. DEC64 represents numbers as `coefficient * 10^exponent` in a 64-bit word. This eliminates the rounding errors that plague IEEE 754 binary floating point — `0.1 + 0.2` is exactly `0.3`.
|
|
|
|
DEC64 was designed by Douglas Crockford as a general-purpose number type suitable for both business and scientific computation.
|
|
|
|
## Format
|
|
|
|
A DEC64 number is a 64-bit value:
|
|
|
|
```
|
|
[coefficient: 56 bits][exponent: 8 bits]
|
|
```
|
|
|
|
- **Coefficient** — a 56-bit signed integer (two's complement)
|
|
- **Exponent** — an 8-bit signed integer (range: -127 to 127)
|
|
|
|
The value of a DEC64 number is: `coefficient * 10^exponent`
|
|
|
|
### Examples
|
|
|
|
| Value | Coefficient | Exponent | Hex |
|
|
|-------|------------|----------|-----|
|
|
| `0` | 0 | 0 | `0000000000000000` |
|
|
| `1` | 1 | 0 | `0000000000000100` |
|
|
| `3.14159` | 314159 | -5 | `000000004CB2FFFB` |
|
|
| `-1` | -1 | 0 | `FFFFFFFFFFFFFF00` |
|
|
| `1000000` | 1 | 6 | `0000000000000106` |
|
|
|
|
## Special Values
|
|
|
|
### Null
|
|
|
|
The exponent `0x80` (-128) indicates null. This is the only special value — there is no infinity, no NaN, no negative zero. Operations that would produce undefined results (such as division by zero) return null.
|
|
|
|
```
|
|
coefficient: any, exponent: 0x80 → null
|
|
```
|
|
|
|
## Arithmetic Properties
|
|
|
|
- **Exact decimals**: All decimal fractions with up to 17 significant digits are represented exactly
|
|
- **No rounding**: `0.1 + 0.2 == 0.3` is true
|
|
- **Integer range**: Exact integers up to 2^55 (about 3.6 * 10^16)
|
|
- **Normalized on demand**: The runtime normalizes coefficients to remove trailing zeros when needed for comparison
|
|
|
|
## Comparison with IEEE 754
|
|
|
|
| Property | DEC64 | IEEE 754 double |
|
|
|----------|-------|----------------|
|
|
| Decimal fractions | Exact | Approximate |
|
|
| Significant digits | ~17 | ~15-16 |
|
|
| Special values | null only | NaN, ±Infinity, -0 |
|
|
| Rounding errors | None (decimal) | Common |
|
|
| Financial arithmetic | Correct | Requires libraries |
|
|
| Scientific range | ±10^127 | ±10^308 |
|
|
|
|
DEC64 trades a smaller exponent range for exact decimal arithmetic. Most applications never need exponents beyond ±127.
|
|
|
|
## In ƿit
|
|
|
|
All numbers in ƿit are DEC64. There is no separate integer type at the language level — the distinction is internal. The `is_integer` function checks whether a number has no fractional part.
|
|
|
|
```javascript
|
|
var x = 42 // coefficient: 42, exponent: 0
|
|
var y = 3.14 // coefficient: 314, exponent: -2
|
|
var z = 1000000 // coefficient: 1, exponent: 6 (normalized)
|
|
|
|
is_integer(x) // true
|
|
is_integer(y) // false
|
|
1 / 0 // null
|
|
```
|