Crates.io | arithmetic-sign |
lib.rs | arithmetic-sign |
version | 1.0.0 |
source | src |
created_at | 2020-09-14 13:37:49.029296 |
updated_at | 2020-09-14 13:37:49.029296 |
description | Arithmetic `Sign`(≈+1|-1) to/from arithmetic types such as `f64`, `i32` utility. |
homepage | |
repository | https://github.com/usagi/arithmetic-sign |
max_upload_size | |
id | 288605 |
size | 18,316 |
Arithmetic Sign
(≈+1|-1) to/from arithmetic types such as f64
, i32
utility.
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.
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 => ErrDisplay
: 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 => negNeg
and Not
: -Sign
or !Sign
-> Sign
; eg. -neg => pos, !pos => negEq
: Sign
==
|!=
Sign
; eg. neg == neg => true, pos != neg => trueOrd
: Sign
<
|<=
|>=
|>
Sign
; eg. neg < pos => true, pos >= pos => true, neg > pos => falseuse 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
std::{f32|f64}::copysign
or std::*::signum
s are regard to negative from -0.0
.
0.0
and -0.0
.