goldenfloat
v1.1.0
Published
φ-optimized 16-bit floating point format for ML (GF16)
Downloads
113
Maintainers
Readme
GoldenFloat — φ-optimized 16-bit floating point format
GF16: [sign:1][exp:6][mant:9] — φ-distance 0.049 (best among 16-bit formats)
Installation
npm install goldenfloatNote: You need the libgoldenfloat shared library. Build from source:
git clone https://github.com/gHashTag/zig-golden-float
cd zig-golden-float
zig build shared
export GOLDENFLOAT_LIB_PATH=$(pwd)/zig-out/libQuick Start
import { GF16 } from 'goldenfloat';
// Basic arithmetic
const a = new GF16(3.14);
const b = new GF16(2.71);
const c = a.add(b);
console.log(`3.14 + 2.71 = ${c.toFloat()}`); // 5.85
// φ-optimized quantization
const weight = 2.71828;
const quantized = GF16.phiQuantize(weight);
console.log(`Original: ${weight}`);
console.log(`Quantized: ${quantized.toFloat()}`);
// Check properties
console.log(`Is zero? ${quantized.isZero()}`);
console.log(`Is negative? ${quantized.isNegative()}`);API
Class: GF16
| Method | Description |
|--------|-------------|
| new GF16(value: number) | Create from float |
| GF16.fromBits(bits: number) | Create from raw bits |
| GF16.phiQuantize(value: number) | φ-optimized quantization |
| .toFloat() | Convert to number |
| .add(other: GF16) | Addition |
| .sub(other: GF16) | Subtraction |
| .mul(other: GF16) | Multiplication |
| .div(other: GF16) | Division |
| .neg() | Negation |
| .abs() | Absolute value |
| .isNaN() | Check if NaN |
| .isInf() | Check if infinite |
| .isZero() | Check if zero |
| .isNegative() | Check if negative |
Low-level Functions
import {
gf16_from_f32,
gf16_to_f32,
gf16_add,
gf16_phi_quantize,
GF16_ZERO,
GF16_ONE,
PHI,
TRINITY,
} from 'goldenfloat';
// Direct bit manipulation
const bits = gf16_from_f32(3.14);
const value = gf16_to_f32(bits);Constants
import {
GF16_ZERO, // 0x0000 (zero)
GF16_ONE, // 0x3C00 (one)
GF16_PINF, // 0x7E00 (+infinity)
PHI, // 1.6180339887498949 (golden ratio)
TRINITY, // 3.0 (φ² + 1/φ²)
} from 'goldenfloat';Express.js Integration
import express from 'express';
import { GF16, gf16_from_f32, gf16_to_f32 } from 'goldenfloat';
const app = express();
app.post('/compute', (req, res) => {
const { a, b } = req.body;
const gfA = gf16_from_f32(a);
const gfB = gf16_from_f32(b);
const sum = gf16_add(gfA, gfB);
res.json({ sum: gf16_to_f32(sum) });
});
app.listen(3000);License
MIT — See LICENSE
