Arithmetic-sign
Arithmetic Sign(≈+1|-1) to/from arithmetic types such as f64, i32 utility.
Feature
-
FromandTryFrom: An arithmetic type such asf64,i32=(from/try_from)>Sign-
From: For an integral such asi32.; It has no infinite pattern. -
TryFrom: For an float such asf64.; It has +inf, -inf and NaN infinite patterns.- A finite, +inf, -inf are valid =>
Ok, nan is invalid =>Err.
- A finite, +inf, -inf are valid =>
-
-
as_T:Sign=(as_T)> an arithmetic type such asf64,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"
-
-
MulandDiv:Sign * SignorSign / Sign->Sign; eg. neg * neg => pos, pos / neg => neg -
NegandNot:-Signor!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
- See also: tests/test.rs.
use *;
let _sign = from; // -> Sign::Positive
let _sign = from; // -> Sign::Positive
let _sign = from; // -> Sign::Positive
let _sign = from; // -> Sign::Negative
let _sign_maybe = try_from; // -> Ok( Sign::Positive )
let _sign_maybe = try_from; // -> Ok( Sign::Positive )
let _sign_maybe = try_from; // -> Ok( Sign::Positive )
let _sign_maybe = try_from; // -> Ok( Sign::Negative )
let _sign_maybe = try_from; // -> Ok( Sign::Positive )
let _sign = from * from; // -> Sign::Negative
let _sign = !from; // -> Sign::Positive
let _f64 = Positive.as_f64; // 1f64
let _i32 = Negative.as_i32; // -1i32
let _u8 = Positive.as_u8.unwrap; // 1u8
let _u8 = Negative.as_u8.is_err; // true
Motivation
- The std sign features such as
std::{f32|f64}::copysignorstd::*::signums are regard to negative from-0.0.- But, I author wanted to regard to "not a negative" as "positive" from
0.0and-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.
- But, I author wanted to regard to "not a negative" as "positive" from