GithubHelp home page GithubHelp logo

microtransactionsmattertoo / noisy_float-rs Goto Github PK

View Code? Open in Web Editor NEW

This project forked from sergiusiw/noisy_float-rs

0.0 2.0 0.0 31 KB

A rust library for checked floating point operations

License: Apache License 2.0

Rust 100.00%

noisy_float-rs's Introduction

noisy_float-rs

This is a Rust library containing floating point types that panic if they are set to an illegal value, such as NaN.

Crate

The Rust crate for this library can be found here.

Documentation

The documentation for this library can be found here.

Description

The name "Noisy Float" comes from the terms "quiet NaN" and "signaling NaN"; "signaling" was too long to put in a struct/crate name, so "noisy" is used instead, being the opposite of "quiet."

The standard types defined in noisy_float::types follow the principle demonstrated by Rust's handling of integer overflow: a bad arithmetic operation is considered an error, but it is too costly to check everywhere in optimized builds. For each floating point number that is created, a debug_assert! invocation is used to check if it is valid or not. This way, there are guarantees when developing code that floating point numbers have valid values, but during a release run there is no overhead for using these floating point types compared to using f32 or f64 directly.

This crate makes use of the floating point and number traits in the popular num_traits crate.

Examples

An example using the R64 type, which corresponds to finite f64 values.

use noisy_float::prelude::*;

fn geometric_mean(a: R64, b: R64) -> R64 {
    (a * b).sqrt() //used just like regular floating-point numbers
}

println!("geometric_mean(10.0, 20.0) = {}", geometric_mean(r64(10.0), r64(20.0)));
//prints 14.142...

An example using the N32 type, which corresponds to non-NaN f32 values. The float types in this crate are able to implement Eq and Ord properly, since NaN is not allowed.

use noisy_float::prelude::*;

let values = vec![n32(3.0), n32(-1.5), n32(71.3), N32::infinity()];
assert!(values.iter().cloned().min() == Some(n32(-1.5)));
assert!(values.iter().cloned().max() == Some(N32::infinity()));

License

Noisy_float is licensed under the Apache 2.0 License.

noisy_float-rs's People

Contributors

sergiusiw avatar michael-f-bryan avatar coriolinus avatar wadelma avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.