# /// script
# requires-python = ">=3.10"
# dependencies = ["numpy"]
# ///
# Copyright (c) 2026 Aayush Sharma · SPDX-License-Identifier: MIT
# https://thedataengineering.com/lab-code/LICENSE.txt
"""Are your floats decimals in disguise?

The idea behind ALP: a column of doubles that are really six-decimal numbers
compresses badly as doubles, and well as integers after multiplying by 10^6,
when that round-trips exactly.
"""
import argparse, json, zlib
import numpy as np

p = argparse.ArgumentParser(description=__doc__.splitlines()[0])
p.add_argument("--values", type=int, default=1_000_000, help="Values.")
p.add_argument("--decimals", type=int, default=6, help="Decimal places in the data (and the exponent tried).")
p.add_argument("--json", help="Also write the results to this JSON file.")
args = p.parse_args()
rng = np.random.default_rng(0)
x = np.round(rng.normal(52.37, 0.5, args.values), args.decimals)
raw = x.tobytes()
zr = len(raw) / len(zlib.compress(raw, 6))
scaled = np.round(x * 10**args.decimals).astype(np.int64)
exact = bool(np.array_equal(scaled / 10**args.decimals, x))
w = int(scaled.max() - scaled.min()).bit_length()
print(f"raw doubles + zlib       : {zr:.2f}x")
print(f"as integers + FOR        : {64 / w:.2f}x  (width {w} bits; round-trips exactly: {exact})")
packed = (scaled - scaled.min()).astype(np.uint32).tobytes() if w <= 32 else b""
zi = len(raw) / len(zlib.compress(packed, 6)) if packed else 0
print(f"as integers + FOR + zlib : {zi:.2f}x")

if args.json:
    with open(args.json, "w") as f:
        json.dump({"tables": {"summary": [{"zlib_double": zr, "for_ratio": 64 / w, "width": w, "exact": "yes" if exact else "no", "for_zlib": zi}]}}, f)
