Skip to content
A rustc plugin to check for numerical instability
Rust Shell
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.
misc Add the Dockerfile Jan 26, 2016
.gitignore Initial commit Dec 24, 2015
LICENSE Fixed grammar mistake Aug 28, 2016

Herbie lint for Rust Build Status License


This plugin can add warnings or errors to your crate when using a numerically unstable floating point expression.

Quick example of what you can get when compiling tests/compile-fail/general/ 40:18 warning: Numerically unstable expression, #[warn(herbie)] on by default     (a/b + c) * b;
               ^~~~~~~~~~~~~ 40:18 help: Try this       (c * b) + a; 67:23 warning: Numerically unstable expression, #[warn(herbie)] on by default     (a*a + b*b).sqrt();
               ^~~~~~~~~~~~~~~~~~ 67:23 help: Try this       a.hypot(b); 155:30 warning: Numerically unstable expression, #[warn(herbie)] on by default     (a+b).sin() - (a+b).cos();
                ^~~~~~~~~~~~~~~~~~~~~~~~~ 155:30 help: Try this        (b.sin() * (a.sin() + a.cos())) - ((a.cos() - a.sin()) * b.cos());

As you can see, it will report numerically unstable expressions, and suggest a (sometimes over-parenthesized) more stable correction.



This is a rustc plugin, to use it, you need a nightly Rust.

You need a database of possible corrections for this plugin to work. The database format is the same as Herbie GHC Plugin (for Haskell) from which this plugin is inspired so this file should work. Just put it in the same directory you call cargo or rustc from.

Add in your Cargo.toml:

herbie-lint = "{{VERSION}}"

and in your crate:


See clippy's Usage section if you want to know more and if you want more Rust lints.


If you don't want the plugin to lint a particular function or method, you can mark it with the #[herbie_ignore] attribute:

fn foo(a: f64, b: f64, c: f64) -> f64 {
    (a/b + c) * b
    // This will suggest to use “(c * b) + a” instead.

fn bar(a: f64, b: f64, c: f64) -> f64 {
    (a/b + c) * b
    // This won't.

You can also put a Herbie.toml file next to your Cargo.toml with the following fields:

# Path to the database.
db_path = "Herbie.db"

# The seed used by Herbie. If not provided, a fixed seed will be used. Fixing
# the seed ensures deterministic builds.
herbie_seed = "#(1461197085 2376054483 1553562171 1611329376 2497620867 2308122621)"

# Allow the plugin to call Herbie on unknown expressions. Positive results from
# Herbie will be cached in the database. WARNING: Herbie is slow.
# If ‘true’, the plugin will fail if it cannot find the executable.
# If ‘false’, the plugin will not try to run Herbie.
# By default, the plugin will call the executable only if it's found, but won't
# complain otherwise.
use_herbie = false

# Maximum time in seconds that Herbie is allowed to play with an expression. If
# null, allow Herbie to run indefinitely. Default is two minutes.
timeout = 120

More information about calling Herbie can be found in the wiki.


Thanks to @llogiq for the idea.

You can’t perform that action at this time.