Skip to content

Commit 4131424

Browse files
committed
Add constants ZERO, ONE, TWO and NEG_ZERO
1 parent d759b00 commit 4131424

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

src/num.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,19 +131,27 @@ pub trait Number:
131131
+ Sum
132132
+ Product
133133
{
134+
const ZERO: Self;
135+
const ONE: Self;
136+
const TWO: Self;
137+
134138
fn from_bytes(bytes: Self::ByteArray) -> Self;
135139
fn as_mut_bytes(&mut self) -> &mut Self::ByteArray;
136140
}
137141

138142
macro_rules! impl_number {
139-
($ty:ty, min: $min:expr, max: $max:expr) => {
143+
($ty:ty, zero: $zero:expr, one: $one:expr, min: $min:expr, max: $max:expr) => {
140144
impl_number_like!($ty,
141145
underlying: Self,
142146
min: $min,
143147
max: $max,
144148
try_from_underlying: |v| Some(v)
145149
);
146150
impl Number for $ty {
151+
const ZERO: Self = $zero;
152+
const ONE: Self = $one;
153+
const TWO: Self = $one + $one;
154+
147155
fn from_bytes(bytes: Self::ByteArray) -> Self {
148156
unsafe { transmute::<Self::ByteArray, Self>(bytes) }
149157
}
@@ -177,6 +185,8 @@ pub trait Float: Number + From<f32> + From<bool> + Into<f64> {
177185
const INFINITY: Self;
178186
const NEG_INFINITY: Self;
179187

188+
const NEG_ZERO: Self;
189+
180190
type Unsigned: Unsigned;
181191

182192
/// `from_bits`
@@ -189,7 +199,7 @@ pub trait Float: Number + From<f32> + From<bool> + Into<f64> {
189199

190200
macro_rules! impl_float {
191201
($ty:ty, $unsigned:ty, $min_positive_subnormal:expr) => {
192-
impl_number!($ty, min: Self::NEG_INFINITY, max: Self::INFINITY);
202+
impl_number!($ty, zero: 0.0, one: 1.0, min: Self::NEG_INFINITY, max: Self::INFINITY);
193203
impl Float for $ty {
194204
const RADIX: u32 = Self::RADIX;
195205
const MANTISSA_DIGITS: u32 = Self::MANTISSA_DIGITS;
@@ -212,6 +222,8 @@ macro_rules! impl_float {
212222
const INFINITY: Self = Self::INFINITY;
213223
const NEG_INFINITY: Self = Self::NEG_INFINITY;
214224

225+
const NEG_ZERO: Self = -0.0;
226+
215227
type Unsigned = $unsigned;
216228

217229
fn from_unsigned(unsigned: Self::Unsigned) -> Self {
@@ -292,7 +304,7 @@ pub trait Integer:
292304

293305
macro_rules! impl_integer {
294306
($ty:ty, $unsigned:ty, $signed:ty) => {
295-
impl_number!($ty, min: Self::MIN, max: Self::MAX);
307+
impl_number!($ty, zero: 0, one: 1, min: Self::MIN, max: Self::MAX);
296308
impl Integer for $ty {
297309
type Unsigned = $unsigned;
298310
type Signed = $signed;

0 commit comments

Comments
 (0)