Compare commits

...

5 Commits

Author SHA1 Message Date
59b1e2eea1 [#1] Fixed up unused imports, docs, and tests. 2025-03-10 13:04:37 -04:00
273a7c1699 [#1] Removing unneeded imports and adjusting code.
Bounded was brought over from the weave library since weave is being
eliminated. Also, at some point along the way, rust 1.43.0, f32 and
f64 started defining NaN, so now we are using the compiler definitions.
2025-03-10 12:17:37 -04:00
f670beacef [#1] Removed target_pointer_width for 8bit.
It seems that the only valid values are 16, 32, and 64. So that
is all that are handled now.
2025-03-10 11:58:40 -04:00
f38f21fe84 [#1] weave::attempt was removed in favor of ?.
The old try macro style fell out of favour a long time ago. As such
it is time to bring this code forward to the new style. It was just
doing the same thing in the macro call anyways.
2025-03-10 11:48:53 -04:00
c3f2bb7a23 [#1] Adjusted due to git repository move.
This was adjusted to drop weave usage and point to the new git
server. The Rust edition was also incremented.
2025-03-10 11:45:38 -04:00
11 changed files with 126 additions and 57 deletions

View File

@ -1,20 +1,18 @@
[package]
name = "sigils"
version = "0.1.0"
authors = ["Jason Travis Smith <Myrddin@CyberMagesLLC.com>"]
authors = ["Jason Travis Smith <Myrddin@CyberMages.tech>"]
description = "A mathematics library."
license = ""
repository = "https://gitlab.com/CyberMages/Core/sigils.git"
repository = "https://workshop.cybermages.tech/CyberMages/sigils.git"
documentation = ""
keywords = ["sigils"]
edition = "2018"
keywords = ["sigils", "math"]
edition = "2021"
[dependencies.weave]
git = "ssh://git@gitlab.com/CyberMages/Core/weave"
[dependencies.binding]
git = "ssh://git@gitlab.com/CyberMages/Core/binding"
git = "ssh://gitea@workshop.cybermages.tech:3022/CyberMages/binding.git"
[dependencies.pact]
git = "ssh://git@gitlab.com/CyberMages/Core/pact"
git = "ssh://gitea@workshop.cybermages.tech:3022/CyberMages/pact.git"

96
src/bounded.rs Normal file
View File

@ -0,0 +1,96 @@
/// Primitive types that have upper and lower bounds.
pub trait Bounded
{
/// The minimum value for this type.
const MIN: Self;
/// The maximum value for this type.
const MAX: Self;
}
/// A macro for making implementation of
/// the Bounded trait easier.
macro_rules! bounded_trait_impl
{
($T: ty, $minVal: expr, $maxVal: expr) =>
{
impl Bounded for $T
{
const MIN: $T = $minVal;
const MAX: $T = $maxVal;
}
}
}
// Implement the Bounded for all the primitive types.
bounded_trait_impl!(u8, 0u8, !0u8);
bounded_trait_impl!(u16, 0u16, !0u16);
bounded_trait_impl!(u32, 0u32, !0u32);
bounded_trait_impl!(u64, 0u64, !0u64);
bounded_trait_impl!(usize, 0usize, !0usize);
bounded_trait_impl!(i8, (!0i8 ^ (!0u8 >> 1u8) as i8),
!(!0i8 ^ (!0u8 >> 1u8) as i8));
bounded_trait_impl!(i16, (!0i16 ^ (!0u16 >> 1u16) as i16),
!(!0i16 ^ (!0u16 >> 1u16) as i16));
bounded_trait_impl!(i32, (!0i32 ^ (!0u32 >> 1u32) as i32),
!(!0i32 ^ (!0u32 >> 1u32) as i32));
bounded_trait_impl!(i64, (!0i64 ^ (!0u64 >> 1u64) as i64),
!(!0i64 ^ (!0u64 >> 1u64) as i64));
bounded_trait_impl!(isize, (!0isize ^ (!0usize >> 1usize) as isize),
!(!0isize ^ (!0usize >> 1usize) as isize));
bounded_trait_impl!(f32, -3.40282347e+38f32, 3.40282347e+38f32);
bounded_trait_impl!(f64, -1.7976931348623157e+308f64,
1.7976931348623157e+308f64);
#[cfg(test)]
mod tests
{
macro_rules! bounds_test
{
($T: ident, $func_name: ident, $minVal: expr, $maxVal: expr) =>
{
#[test]
fn $func_name()
{
assert_eq!($T::MIN, $minVal);
assert_eq!($T::MAX, $maxVal);
}
}
}
bounds_test!(u8, min_max_u8, 0u8, !0u8);
bounds_test!(u16, min_max_u16, 0u16, !0u16);
bounds_test!(u32, min_max_u32, 0u32, !0u32);
bounds_test!(u64, min_max_u64, 0u64, !0u64);
bounds_test!(usize, min_max_usize, 0usize, !0usize);
bounds_test!(i8, min_max_i8,
-1i8 << (((::std::mem::size_of::<i8>() as i8)*8i8)-1i8),
!(-1i8 << (((::std::mem::size_of::<i8>() as i8)*8i8)-1i8)));
bounds_test!(i16, min_max_i16,
-1i16 << (((::std::mem::size_of::<i16>() as i16)*8i16)-1i16),
!(-1i16 << (((::std::mem::size_of::<i16>() as i16)*8i16)-1i16)));
bounds_test!(i32, min_max_i32,
-1i32 << (((::std::mem::size_of::<i32>() as i32)*8i32)-1i32),
!(-1i32 << (((::std::mem::size_of::<i32>() as i32)*8i32)-1i32)));
bounds_test!(i64, min_max_i64,
-1i64 << (((::std::mem::size_of::<i64>() as i64)*8i64)-1i64),
!(-1i64 << (((::std::mem::size_of::<i64>() as i64)*8i64)-1i64)));
bounds_test!(isize, min_max_isize,
-1isize << (((::std::mem::size_of::<isize>() as isize)*8isize)-1isize),
!(-1isize << (((::std::mem::size_of::<isize>() as isize)*8isize)-1isize)));
bounds_test!(f32, min_max_f32,
-3.40282347e+38f32, 3.40282347e+38f32);
bounds_test!(f64, min_max_f64,
-1.7976931348623157e+308f64,
1.7976931348623157e+308f64);
}

View File

@ -57,6 +57,7 @@ pub trait Constants
const INVERSE_SQRT_3: Self;
/// The mathematical constant [E][1]. Also known as [Euler's number][1].
///
/// [1]: https://en.wikipedia.org/wiki/E_(mathematical_constant)
///
///```
@ -130,6 +131,7 @@ pub trait Constants
const TWO_PI: Self;
/// [PI][1]. The ratio of a circles circumference to its diameter.
///
/// [1]: https://en.wikipedia.org/wiki/Pi
///
///```

View File

@ -10,6 +10,8 @@ extern crate core as std;
#[macro_use]
mod macros;
mod bounded;
mod zero;
mod one;
mod number;
@ -26,9 +28,10 @@ pub mod quaternion;
pub use self::bounded::Bounded;
pub use self::zero::Zero;
pub use self::one::One;
pub use self::number::Number;
pub use self::number::{Number, FromNumber};
pub use self::whole::Whole;
pub use self::integer::Integer;
pub use self::real::Real;

View File

@ -1,8 +1,6 @@
//! This module defines the 2x2, 3x3, and 4x4 Matrix structures.
use std::ops::{Add, Sub, Mul, Div, Rem, Neg};
//use std::ops::{Add, Sub, Mul, Div, Rem, Neg};
use crate::zero::Zero;
use crate::one::One;
use crate::number::Number;
use crate::vector::{Vector, Vector2, Vector3, Vector4};

View File

@ -5,8 +5,7 @@ use std::ops::{Add, Sub, Mul, Div, Rem};
use std::ops::{AddAssign, SubAssign, MulAssign, DivAssign, RemAssign};
use std::str::FromStr;
use weave::Bounded;
use crate::bounded::Bounded;
use crate::zero::Zero;
use crate::one::One;
@ -900,8 +899,6 @@ float_number_trait_impl!(Number for f64, -1.7976931348623157e+308f64,
1.7976931348623157e+308f64,
fmin, fmax);
#[cfg(target_pointer_width = "8")]
int_number_trait_impl!(Number for usize, 0usize, 255usize);
#[cfg(target_pointer_width = "16")]
int_number_trait_impl!(Number for usize, 0usize, 65535usize);
#[cfg(target_pointer_width = "32")]
@ -909,8 +906,6 @@ int_number_trait_impl!(Number for usize, 0usize, 4294967295usize);
#[cfg(target_pointer_width = "64")]
int_number_trait_impl!(Number for usize, 0usize, 18446744073709551615usize);
#[cfg(target_pointer_width = "8")]
int_number_trait_impl!(Number for isize, -128isize, 127isize);
#[cfg(target_pointer_width = "16")]
int_number_trait_impl!(Number for isize, -32768isize, 32767isize);
#[cfg(target_pointer_width = "32")]

View File

@ -7,7 +7,7 @@ pub trait One: Sized + Mul<Self, Output=Self>
///
/// # Laws
///
/// ```{.text}
/// ```text
/// a * 1 = a ∀ a ∈ Self
/// 1 * a = a ∀ a ∈ Self
/// ```

View File

@ -8,10 +8,8 @@ use std::ops::{Add, Sub, Mul, Div, Neg};
use crate::zero::Zero;
use crate::one::One;
use crate::real::Real;
use crate::trig::Radian;
use crate::trig::Trig;
use crate::vector::{Vector, EuclideanVector, Vector3};
use crate::vector::{EuclideanVector, Vector3};

View File

@ -159,22 +159,18 @@ pub trait Real : Number + Constants + Neg<Output=Self>
/// use std::{f32, f64};
/// use sigils::Real;
///
/// let nan32: f32 = f32::NAN;
/// let f_val32 = 7.0f32;
/// let g_val32 = -7.0f32;
///
/// let nan64: f64 = f64::NAN;
/// let f_val64 = 7.0f64;
/// let g_val64 = -7.0f64;
///
/// // Requires both tests to determine if is `NaN`
/// assert!(!f_val32.is_sign_negative());
/// assert!(g_val32.is_sign_negative());
/// assert!(!nan32.is_sign_positive() && !nan32.is_sign_negative());
///
/// assert!(!f_val64.is_sign_negative());
/// assert!(g_val64.is_sign_negative());
/// assert!(!nan64.is_sign_positive() && !nan64.is_sign_negative());
/// ```
fn is_sign_negative(self) -> bool;
@ -185,22 +181,18 @@ pub trait Real : Number + Constants + Neg<Output=Self>
/// use std::{f32, f64};
/// use sigils::Real;
///
/// let nan32: f32 = f32::NAN;
/// let f_val32 = 7.0f32;
/// let g_val32 = -7.0f32;
///
/// let nan64: f64 = f64::NAN;
/// let f_val64 = 7.0f64;
/// let g_val64 = -7.0f64;
///
/// // Requires both tests to determine if is `NaN`
/// assert!(f_val32.is_sign_positive());
/// assert!(!g_val32.is_sign_positive());
/// assert!(!nan32.is_sign_positive() && !nan32.is_sign_negative());
///
/// assert!(f_val64.is_sign_positive());
/// assert!(!g_val64.is_sign_positive());
/// assert!(!nan64.is_sign_positive() && !nan64.is_sign_negative());
/// ```
fn is_sign_positive(self) -> bool;
@ -228,8 +220,6 @@ pub trait Real : Number + Constants + Neg<Output=Self>
/// ```
fn get_signum(self) -> Self;
/* TODO: Reimplement this when the error for 'use core::num::Float;'
goes away.
/// Returns the floating point category of the number.
/// If only one property is going to be tested, it is
/// generally faster to use the specific predicate instead.
@ -252,9 +242,7 @@ pub trait Real : Number + Constants + Neg<Output=Self>
/// assert_eq!(inf64.get_category(), FpCategory::Infinite);
/// ```
fn get_category(self) -> FpCategory;
*/
// TODO: Fix/check this example.
/// Returns the mantissa, base 2 exponent, and sign as
/// integers, respectively. The original number can be
/// recovered by `sign * mantissa * 2 ^ exponent`.
@ -265,7 +253,7 @@ pub trait Real : Number + Constants + Neg<Output=Self>
/// let num = 2.0f32;
///
/// // (8388608, -22, 1)
/// let (mantissa, exponent, sign) = Real::integer_decode(num);
/// let (mantissa, exponent, sign) = Real::get_integer_decode(num);
/// let sign_f = sign as f32;
/// let mantissa_f = mantissa as f32;
/// let exponent_f = num.powf(exponent as f32);
@ -646,18 +634,18 @@ impl Real for f32
const NEG_INFINITY: Self = -1.0f32 / 0.0f32;
///
const NAN: Self = 0.0f32 / 0.0f32;
const NAN: Self = f32::NAN;
fn is_nan(self) -> bool
{
self == Self::NAN
self.is_nan()
}
fn is_finite(self) -> bool
{
!self.is_infinite() && self != Self::NAN
!self.is_infinite() && !self.is_nan()
}
fn is_infinite(self) -> bool
@ -701,13 +689,10 @@ impl Real for f32
}
}
/* TODO: Reimplement this when the error for 'use core::num::Float;'
goes away.
fn get_category(self) -> FpCategory
{
self.classify()
}
*/
fn get_integer_decode(self) -> (u64, i16, i8)
{
@ -908,18 +893,18 @@ impl Real for f64
const NEG_INFINITY: Self = -1.0f64 / 0.0f64;
///
const NAN: Self = 0.0f64 / 0.0f64;
const NAN: Self = f64::NAN;
fn is_nan(self) -> bool
{
self == Self::NAN
self.is_nan()
}
fn is_finite(self) -> bool
{
!self.is_infinite() && self != Self::NAN
!self.is_infinite() && !self.is_nan()
}
fn is_infinite(self) -> bool
@ -963,13 +948,10 @@ impl Real for f64
}
}
/* TODO: Reimplement this when the error for 'use core::num::Float;'
goes away.
fn get_category(self) -> FpCategory
{
self.classify()
}
*/
fn get_integer_decode(self) -> (u64, i16, i8)
{

View File

@ -5,12 +5,9 @@ use std::fmt::{Error, Formatter, Debug, Display};
use std::ops::{Add, Sub, Mul, Div, Rem, Neg};
use std::ops::{AddAssign, SubAssign, MulAssign, DivAssign, RemAssign};
use weave::attempt;
use crate::zero::Zero;
use crate::one::One;
use crate::number::Number;
use crate::real::Real;
use crate::trig::Radian;
use crate::trig::Trig;
@ -550,12 +547,12 @@ macro_rules! define_vector
// pretty up the printing.
count = self.get_size();
attempt!(write!(formatter, "<"));
write!(formatter, "<")?;
$(
attempt!(write!(formatter, "{:?}", self.$field));
write!(formatter, "{:?}", self.$field)?;
if count > 0
{
attempt!(write!(formatter, ", "));
write!(formatter, ", ")?;
}
count -= 1;
)*
@ -575,12 +572,12 @@ macro_rules! define_vector
// pretty up the printing.
count = self.get_size();
attempt!(write!(formatter, "<"));
write!(formatter, "<")?;
$(
attempt!(write!(formatter, "{}", self.$field));
write!(formatter, "{}", self.$field)?;
if count > 1
{
attempt!(write!(formatter, ", "));
write!(formatter, ", ")?;
}
count -= 1;
)*

View File

@ -7,7 +7,7 @@ pub trait Zero: Sized + Add<Self, Output=Self>
///
/// # Laws
///
/// ```{.text}
/// ```text
/// a + 0 = a ∀ a ∈ Self
/// 0 + a = a ∀ a ∈ Self
/// ```