Git Product home page Git Product logo

arithmetic-sign's Introduction

Arithmetic-sign

Arithmetic Sign(≈+1|-1) to/from arithmetic types such as f64, i32 utility.

Feature

  • From and TryFrom: An arithmetic type such as f64, i32 =(from/try_from)> Sign
    • From: For an integral such as i32.; It has no infinite pattern.
    • TryFrom: For an float such as f64.; It has +inf, -inf and NaN infinite patterns.
      • A finite, +inf, -inf are valid => Ok, nan is invalid => Err.
  • as_T: Sign =(as_T)> an arithmetic type such as f64, i32
    • as_uT: Safety unsigned casting. eg. pos => Ok, neg => Err
  • Display: Sign =(to_string)> "-" or "+"
    • Sign =(to_string_specified("S", "N"))> "S" or "N"
  • Mul and Div: Sign * Sign or Sign / Sign -> Sign; eg. neg * neg => pos, pos / neg => neg
  • Neg and Not: -Sign or !Sign -> Sign; eg. -neg => pos, !pos => neg
  • Eq: Sign ==|!= Sign; eg. neg == neg => true, pos != neg => true
  • Ord: Sign <|<=|>=|> Sign; eg. neg < pos => true, pos >= pos => true, neg > pos => false

Usage

use arithmetic_sign::*;
let _sign = Sign::from( 123 ); // -> Sign::Positive
let _sign = Sign::from( 0 ); // -> Sign::Positive
let _sign = Sign::from( -0 ); // -> Sign::Positive
let _sign = Sign::from( -123 ); // -> Sign::Negative
let _sign_maybe = Sign::try_from( 1.23 ); // -> Ok( Sign::Positive )
let _sign_maybe = Sign::try_from( 0.0 ); // -> Ok( Sign::Positive )
let _sign_maybe = Sign::try_from( -0.0 ); // -> Ok( Sign::Positive )
let _sign_maybe = Sign::try_from( 1.23 ); // -> Ok( Sign::Negative )
let _sign_maybe = Sign::try_from( std::f64::inf() ); // -> Ok( Sign::Positive )
let _sign = Sign::from( -123 ) * Sign::from( 123 ); // -> Sign::Negative
let _sign = !Sign::from( -123 ); // -> Sign::Positive
let _f64 = Sign::Positive.as_f64(); // 1f64
let _i32 = Sign::Negative.as_i32(); // -1i32
let _u8 = Sign::Positive.as_u8().unwrap(); // 1u8
let _u8 = Sign::Negative.as_u8().is_err(); // true

Motivation

  • The std sign features such as std::{f32|f64}::copysign or std::*::signums are regard to negative from -0.0.
    • But, I author wanted to regard to "not a negative" as "positive" from 0.0 and -0.0.
    • And, I author wanted to boolean-like type such as "positive" and "negative" that has only 2-variant.
    • And, Infinity is a valid signed value, NaN is not a valid value.

License

Author

arithmetic-sign's People

Contributors

usagi avatar

Stargazers

 avatar  avatar

Watchers

 avatar  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.