---
# System prepended metadata

title: A High-Precision Floating-Point Math Library for Solidity and Vyper
tags: [defi, vyper, evm, scientific computing, numerical methods, amm-design, solidity]

---

# A High-Precision Floating-Point Math Library for Solidity and Vyper

One persistent limitation of the EVM is the absence of a native floating-point numerical model. For many applications, fixed-point arithmetic is sufficient, but there are domains - particularly AMM design, nonlinear pricing curves, numerical solvers, and probabilistic modeling - where fixed-point approaches introduce structural distortions that are difficult or impossible to eliminate.

Out of necessity, I developed a floating-point library for Solidity (“FloatLib”) intended to address this gap. The implementation uses a packed `(mantissa, exponent)` representation inside an `int256`, with:

* a **72-bit mantissa** (**21 significant digits**)
* a signed exponent
* deterministic normalization
* stable rounding rules

This places the precision strictly above IEEE-754 double precision (≈15 digits) while remaining inexpensive and deterministic for on-chain use.

Crucially, the representation is **designed specifically for the EVM**. Packing and unpacking the `(mantissa, exponent)` pair is cheap in terms of gas: it is just bit-twiddling on a single `int256` word, not a full-blown IEEE-754 implementation with complex hardware semantics. All arithmetic and transcendental functions are implemented in Solidity with that constraint in mind.

The library currently supports:

* addition and subtraction
* multiplication and division
* `ln`, `exp`, `sqrt`
* a deterministic PRNG
* normal and log-normal sampling (Marsaglia polar method)

To evaluate the quality of the implementation, I generated large samples of normals and lognormals entirely inside Solidity, exported the values, and plotted histograms. Representative cases:

* **Normal(5, 5)**: smooth Gaussian shape, correct tails

![histogram_5_5_overlay](https://hackmd.io/_uploads/HyjLB0mWWx.png)

* **Lognormal(0.25, 0.5)**: clean unimodal distribution centered at 1

![lognormal__25__5_overlay](https://hackmd.io/_uploads/ByIdH07WZx.png)

The Solidity-generated samples line up closely with NumPy. Visually and statistically, the distributions coincide: no discretization artifacts, no binning discontinuities, and no rounding-induced anomalies. The tails behave as expected, even in pathological parameter regimes.

For AMM research, this matters for several reasons:

* The dynamic-weight Multiswap model depends on solving a family of cubic equations; the stability of Newton iterations is heavily influenced by numeric precision.
* Fuzz testing near critical boundaries requires a realistic sampling distribution.
* Large-magnitude values arise naturally in log-normal stress tests; avoiding overflow and underflow is essential for meaningful verification.

FloatLib provides a deterministic and high-precision numerical environment that preserves the functional forms of the underlying mathematics, while still being gas-conscious and EVM-native. Its behavior is substantially closer to a trimmed scientific-computing model than to traditional fixed-point Solidity patterns.

I’ll continue refining the library, but the initial results suggest that it is possible to conduct serious numerical work inside Solidity without compromising the mathematics or introducing avoidable instability.

If this kind of work is relevant to your research or engineering needs, I’m happy to compare notes. Both Solidity and Vyper implementations are available and kept in sync; the Vyper version is particularly useful for tests, numerical experiments, and environments where Python tooling is preferred.

**FloatLib Repository (Solidity \+ Vyper):**  
- [FloatLib.sol](https://github.com/CavalRe/cavalre-contracts/blob/main/libraries/FloatLib.sol)
- [FloatLib.vy](https://github.com/CavalRe/cavalre-contracts/blob/main/vyper/FloatLib.vy)

Contributions, issues, and benchmark comparisons are welcome.
