From 240506436359f5e5f2a9f9ce6311e8482e4c0b20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johanna=20S=C3=B6rng=C3=A5rd?= Date: Fri, 6 Sep 2024 09:47:24 +0200 Subject: [PATCH] Add 32 bit example --- README.md | 18 +++++++++++++++--- src/lib.rs | 23 +++++++++++++++++++++-- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index a0fe114..ca09c59 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ # lambert_w -[![Static Badge](https://img.shields.io/badge/github-lambert__w-8da0cb?logo=github)](https://github.com/JSorngard/lambert_w) [![Crates.io Version](https://img.shields.io/crates/v/lambert_w?logo=rust)](https://crates.io/crates/lambert_w) [![docs.rs](https://img.shields.io/docsrs/lambert_w?logo=docs.rs)](https://docs.rs/lambert_w/latest/lambert_w/) +[![Static Badge](https://img.shields.io/badge/github-lambert__w-8da0cb?logo=github)](https://github.com/JSorngard/lambert_w) [![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/JSorngard/lambert_w/rust.yml?logo=github&label=CI)](https://github.com/JSorngard/lambert_w/actions/workflows/rust.yml) [![codecov](https://codecov.io/gh/JSorngard/lambert_w/graph/badge.svg?token=F61FO63ZKW)](https://codecov.io/gh/JSorngard/lambert_w) @@ -22,8 +22,8 @@ accuracy and one with 24 bits. The one with 50 bits of accuracy uses higher degree polynomials in the rational functions compared to the one with only 24 bits, and thus more of the multiplications and additions by constants. -This crate can also evaluate the approximation with 24 bits of accuracy on 32 -bit floats, even though it is defined on 64 bit floats in the paper. +This crate can also evaluate the approximation with 24 bits of accuracy on +32-bit floats, even though it is defined on 64-bit floats in the paper. This may result in a reduction in the accuracy to less than 24 bits, but this reduction has not been quantified by the author of this crate. @@ -54,6 +54,18 @@ let mln4 = lambert_wm1(-f64::ln(2.0) / 2.0); assert_abs_diff_eq!(mln4, -f64::ln(4.0)); ``` +Do it on 32-bit floats: + +```rust +use lambert_w::{lambert_w0f, lambert_wm1f}; + +let Ω = lambert_w0f(1.0); +let mln4 = lambert_wm1(-f32::ln(2.0) / 2.0); + +assert_abs_diff_eq!(Ω, 0.56714329); +assert_abs_diff_eq!(mln4, -f32::ln(4.0)); +``` + ## License Licensed under either of diff --git a/src/lib.rs b/src/lib.rs index 49d7269..825c3cb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,8 +10,8 @@ //! The one with 50 bits of accuracy uses higher degree polynomials in the rational functions compared to the one with only 24 bits, //! and thus more of the multiplications and additions by constants. //! -//! This crate can also evaluate the approximation with 24 bits of accuracy on 32 -//! bit floats, even though it is defined on 64 bit floats in the paper. +//! This crate can also evaluate the approximation with 24 bits of accuracy on +//! 32-bit floats, even though it is defined on 64-bit floats in the paper. //! This may result in a reduction in the accuracy to less than 24 bits, //! but this reduction has not been quantified by the author of this crate. //! @@ -47,6 +47,25 @@ assert_abs_diff_eq!(mln4, -f64::ln(4.0)); ``` "## )] +//! +//! Do it on 32-bit floats: +//! +#![cfg_attr( + feature = "24bits", + doc = r##" +``` +# use approx::assert_abs_diff_eq; +use lambert_w::{lambert_w0f, lambert_wm1f}; + +let Ω = lambert_w0f(1.0); +let mln4 = lambert_wm1f(-f32::ln(2.0) / 2.0); + +assert_abs_diff_eq!(Ω, 0.56714329); +assert_abs_diff_eq!(mln4, -f32::ln(4.0)); +``` +"## +)] +//! //! The macro is from the [`approx`](https://docs.rs/approx/latest/approx/) crate, and is used in the documentation examples of this crate. //! The assertion passes if the two supplied values are the same to within floating point error, or within an optional epsilon. //!