# MPFR-based correctness testing for Mojo 🔥

**URL:** <https://forum.modular.com/t/mpfr-based-correctness-testing-for-mojo/457>\
**Category:** Community Showcase\
**Created:** [January 19, 2025, 9:25pm UTC](https://forum.modular.com/t/mpfr-based-correctness-testing-for-mojo/457 "2025-01-19T21:25:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![leandrolcampos](https://avatars.discourse-cdn.com/v4/letter/l/ec9cab/32.png) [@leandrolcampos](https://forum.modular.com/u/leandrolcampos)\
**Post date:** [January 19, 2025, 9:25pm UTC](https://forum.modular.com/t/mpfr-based-correctness-testing-for-mojo/457/1 "2025-01-19T21:25:09Z")

</div>

This project is a proof of concept (PoC) that demonstrates how we can use the [MPFR](https://www.mpfr.org/) library as a “gold standard” to test the correctness of mathematical functions implemented in [Mojo](https://www.modular.com/mojo).

MPFR is an efficient C library for multiple-precision floating-point computations with _correct rounding_. It is used to test numerical routines in projects such as [CORE-MATH](https://core-math.gitlabpages.inria.fr/), [LLVM-libc](https://libc.llvm.org/index.html), and [RLIBM](https://people.cs.rutgers.edu/~sn349/rlibm/).

By comparing the outputs of our custom Mojo functions with MPFR, we can ensure our implementations are correctly rounded or, at least, as accurate as possible within the memory or latency requirements.

Main Features:

- Provides a routine to convert MPFR values into lower-precision floating-point types (like `float16` and `bfloat16` ), avoiding double rounding errors.
- Supports correctness testing under multiple IEEE 754 rounding modes.
- Includes utilities to change and check rounding mode at runtime.

[Project Repository](https://github.com/leandrolcampos/mpfr-for-mojo)

---

<div class="post-metadata">

**Author:** ![leandrolcampos](https://avatars.discourse-cdn.com/v4/letter/l/ec9cab/32.png) [@leandrolcampos](https://forum.modular.com/u/leandrolcampos)\
**Post date:** [February 26, 2025, 9:25pm UTC](https://forum.modular.com/t/mpfr-based-correctness-testing-for-mojo/457/2 "2025-02-26T21:25:29Z")

</div>

With just a few lines of code, we can now test the correctness of a numerical routine under all rounding modes required by the IEEE 754 standard:

```mojo
fn test_sqrt_bf16() raises:
    alias ROUNDING_MODES = available_rounding_modes()

    @parameter
    for i in range(len(ROUNDING_MODES)):
        alias ROUNDING_MODE = ROUNDING_MODES[i]

        var sqrt_checker = UnaryOperatorChecker[
            DType.bfloat16,
            mpfr.sqrt,
            math.sqrt,
            rounding_mode=ROUNDING_MODE,
            default_ulp_tolerance=0.5,
        ]()

        sqrt_checker.assert_special_values()
        sqrt_checker.assert_negative_normals[count=101]()
        sqrt_checker.assert_negative_subnormals[count=11]()
        sqrt_checker.assert_positive_subnormals[count=101]()
        sqrt_checker.assert_positive_normals[count=1_001]()

```

In the above example, we test the implementation of the `sqrt` function available in the Mojo standard library. We expect the specialization of this function for `bfloat16` is correctly rounded or, if not, that the approximation error is at most 0.5 ULP.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex001/uploads/modular/original/1X/2751e0fbdc595a99718b216730957e9db4448cfd.jpeg) [@system](https://forum.modular.com/u/system)\
**Post date:** [August 25, 2025, 9:26pm UTC](https://forum.modular.com/t/mpfr-based-correctness-testing-for-mojo/457/3 "2025-08-25T21:26:30Z")

</div>

Closed on August 25, 2025, 9:26pm UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
