Giter Club home page Giter Club logo

close-to-rs's Introduction

close-to-rs

Provides a function, Tolerate, to verify the equivalence of floating-point numbers at arbitrary precision.

任意の精度で浮動小数点数の等価性を検証するための関数、トレイトを提供します。

An alias also exists for compatibility with assert-be-close, so changing the crate does not require changing the code.

assert-be-closeとの互換性を保つためのエイリアスも存在するため、クレートを変更してもコードを変更する必要がありません。

使い方(Usage)

Compare values with arbitrary precision.

任意の精度で数値を比較する。

use close_to::{close_to, far_from};

fn example() {
    let (is_close, expected_diff, received_diff) = close_to(1.0, 1.0001, 3);
    assert!(is_close);

    let (is_close, expected_diff, received_diff) = close_to(1.0, 1.0001, 4);
    assert!(is_close); // panic!


    let (is_close, expected_diff, received_diff) = far_from(1.0, 1.0001, 4);
    assert!(is_close);

    let (is_close, expected_diff, received_diff) = far_from(1.0, 1.0001, 3);
    assert!(is_close); // panic!
}

Different types can be compared if the first argument T is a floating-point number and the second argument U implements Into<T>.

異なる型同士でも、第1引数Tが浮動小数点数かつ、第2引数UInto<T>を実装していれば比較できます。

use close_to::{close_to, far_from};

fn example() {
    close_to(1.0_f64, 1.0001_f32, 3);
    close_to(1.0001_f64, 1_u8, 3);

    close_to(1.0_f32, 1.0001_f64, 3); // the trait bound `f32: std::convert::From<f64>` is not satisfied [E0277]
}

There is also a function that panics if the result of the comparison is false, along with a debug expression.

また、比較した結果がfalseである場合、デバック表現とともにpanicする関数もあります。

use close_to::{assert_close_to, assert_far_from};

fn example() {
    assert_close_to(1.0, 1.0001, 4); // panic with the following message

    // assertion 'left ≈ right` failed
    // left:  1
    // right: 1.0001
    // received_diff: 0.00009999999999998899
    // expected_diff: 0.00005

    assert_far_from(1.0, 1.0001, 3); // panic with the following message

    // assertion 'left != right` failed
    // left:  1
    // right: 1.0001
    // received_diff: 0.00009999999999998899
    // expected_diff: 0.0005
}

Traits are also provided to implement these functions. By default, they are implemented for f32 and f64. T.close_to(U, precision) and close_to(T, U, precision) have the same behavior.

これらの機能を実装するためのトレイトも提供しています。 デフォルトでは、f32f64に対して実装されています。 T.close_to(U, precision)close_to(T, U, precision)は同じ挙動をします。

use close_to::{AssertCloseTo, CloseTo};

fn example() {
    let is_close = 1_f64.close_to(1.0001, 3).0;
    assert!(is_close);

    let is_close = 1_f64.close_to(1.0001, 4).0;
    assert!(is_close); // panic!

    1_f64.assert_close_to(1.0001, 4); // panic with the same message as `assert_close_to`
    1_f64.assert_far_from(1.0001, 3); // panic with the same message as `assert_far_from`
}

ライセンス(License)

Licensed under either of

at your option.

close-to-rs's People

Contributors

azishio avatar

Watchers

 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.