2024-04-15 14:11:54 +02:00
|
|
|
import { test, expect } from "vitest"
|
|
|
|
import { encodeFloat, decodeFloat } from "./encode"
|
|
|
|
|
|
|
|
test("encode_float", () => {
|
|
|
|
const input = 1.23;
|
|
|
|
const encoded = encodeFloat(input)
|
2024-04-16 15:32:23 +02:00
|
|
|
const output = decodeFloat(encoded)
|
2024-04-15 14:11:54 +02:00
|
|
|
console.log(input, output)
|
|
|
|
expect(output).toBeCloseTo(input);
|
|
|
|
});
|
|
|
|
|
|
|
|
test("encode 2.0", () => {
|
|
|
|
const input = 2.0;
|
|
|
|
const encoded = encodeFloat(input)
|
2024-04-16 15:32:23 +02:00
|
|
|
expect(encoded).toEqual(1073741824)
|
2024-04-15 14:11:54 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
test("floating point imprecision", () => {
|
|
|
|
let maxError = 0;
|
|
|
|
new Array(10_000).fill(null).forEach((_, i) => {
|
|
|
|
const input = i < 5_000 ? i : Math.random() * 100;
|
|
|
|
const encoded = encodeFloat(input);
|
2024-04-16 15:32:23 +02:00
|
|
|
const output = decodeFloat(encoded);
|
2024-04-15 14:11:54 +02:00
|
|
|
|
|
|
|
const error = Math.abs(input - output);
|
|
|
|
if (error > maxError) {
|
|
|
|
maxError = error;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(maxError).toBeLessThan(0.00001);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Test with negative numbers
|
|
|
|
test("negative numbers", () => {
|
|
|
|
const inputs = [-1, -0.5, -123.456, -0.0001];
|
|
|
|
inputs.forEach(input => {
|
|
|
|
const encoded = encodeFloat(input);
|
2024-04-16 15:32:23 +02:00
|
|
|
const output = decodeFloat(encoded);
|
2024-04-15 14:11:54 +02:00
|
|
|
expect(output).toBeCloseTo(input);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// Test with very small numbers
|
|
|
|
test("very small numbers", () => {
|
|
|
|
const input = 1.2345e-38;
|
|
|
|
const encoded = encodeFloat(input)
|
2024-04-16 15:32:23 +02:00
|
|
|
const output = decodeFloat(encoded)
|
2024-04-15 14:11:54 +02:00
|
|
|
expect(output).toBeCloseTo(input);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Test with zero
|
|
|
|
test("zero", () => {
|
|
|
|
const input = 0;
|
|
|
|
const encoded = encodeFloat(input)
|
2024-04-16 15:32:23 +02:00
|
|
|
const output = decodeFloat(encoded)
|
2024-04-15 14:11:54 +02:00
|
|
|
expect(output).toBe(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Test with infinity
|
|
|
|
test("infinity", () => {
|
|
|
|
const input = Infinity;
|
|
|
|
const encoded = encodeFloat(input)
|
2024-04-16 15:32:23 +02:00
|
|
|
const output = decodeFloat(encoded)
|
2024-04-15 14:11:54 +02:00
|
|
|
expect(output).toBe(Infinity);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Test with large numbers
|
|
|
|
test("large numbers", () => {
|
|
|
|
const inputs = [1e+5, 1e+10];
|
|
|
|
inputs.forEach(input => {
|
|
|
|
const encoded = encodeFloat(input);
|
2024-04-16 15:32:23 +02:00
|
|
|
const output = decodeFloat(encoded);
|
2024-04-15 14:11:54 +02:00
|
|
|
// Note: Large numbers may lose precision, hence using toBeCloseTo with a tolerance
|
|
|
|
expect(output).toBeCloseTo(input, 0);
|
|
|
|
});
|
|
|
|
});
|