Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/integer/z/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ mod exp;
mod logarithm;
mod modulo;
mod mul;
mod neg;
mod pow;
mod root;
mod sub;
80 changes: 80 additions & 0 deletions src/integer/z/arithmetic/neg.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright 2026 Joshua Limbrey
//
// This file is part of qFALL-math
//
// qFALL-math is free software: you can redistribute it and/or modify it under
// the terms of the Mozilla Public License Version 2.0 as published by the
// Mozilla Foundation. See <https://mozilla.org/en-US/MPL/2.0/>.

//! Implementation of the [`Neg`] trait for [`Z`] values.

use super::super::Z;
use flint3_sys::fmpz_neg;
use std::ops::Neg;

impl Neg for Z {
type Output = Z;

/// Implements the [`Neg`] trait for [`Z`] values.
/// [`Neg`] is implemented for both borrowed and owned [`Z`].
///
/// When called on owned [`Z`] it will reuse the memory of `self`.
///
/// Returns the additive inverse of `self` as a [`Z`].
///
/// # Examples
/// ```
/// use qfall_math::integer::Z;
///
/// let a: Z = Z::from(42);
///
/// let b: Z = -&a;
/// let c: Z = -a;
/// ```
fn neg(mut self) -> Self::Output {
unsafe { fmpz_neg(&mut self.value, &self.value) };
self
}
}

impl Neg for &Z {
type Output = Z;

/// Documentation at [`Z::neg`].
fn neg(self) -> Self::Output {
let mut out = Z::default();
unsafe { fmpz_neg(&mut out.value, &self.value) };
out
}
}

#[cfg(test)]
mod test_neg {
use super::Z;

/// Ensure that `neg` works for small numbers.
#[test]
fn correct_small() {
let a: Z = Z::ONE;
let b: Z = Z::MINUS_ONE;
let c: Z = Z::ZERO;

assert_eq!(a, -(-&a));
assert_eq!(a, -&b);
assert_eq!(b, -&a);
assert_eq!(a, -b.clone());
assert_eq!(b, -a);
assert_eq!(c, -&c);
assert_eq!(c.clone(), -c);
}

/// Ensure that `neg` works for large numbers.
#[test]
fn correct_large() {
let a: Z = Z::from(u64::MAX);
let b: Z = Z::from(i64::MAX);

assert_eq!(a, -(-&a));
assert_eq!(b, -(-&b));
}
}
1 change: 1 addition & 0 deletions src/rational/q/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ mod div;
mod exp;
mod logarithm;
mod mul;
mod neg;
mod pow;
mod root;
mod sub;
84 changes: 84 additions & 0 deletions src/rational/q/arithmetic/neg.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Copyright 2026 Joshua Limbrey
//
// This file is part of qFALL-math
//
// qFALL-math is free software: you can redistribute it and/or modify it under
// the terms of the Mozilla Public License Version 2.0 as published by the
// Mozilla Foundation. See <https://mozilla.org/en-US/MPL/2.0/>.

//! Implementation of the [`Neg`] trait for [`Q`] values.

use super::super::Q;
use flint3_sys::fmpq_neg;
use std::ops::Neg;

impl Neg for Q {
type Output = Q;

/// Implements the [`Neg`] trait for [`Q`] values.
/// [`Neg`] is implements for both borrowed and owned [`Q`].
///
/// When called on owned [`Q`] it will reuse the memory of `self`.
///
/// Returns the additive inverse of `self` as a [`Q`].
///
/// # Examples
/// ```
/// use qfall_math::rational::Q;
///
/// let a: Q = Q::from((1, 42));
///
/// let b: Q = -&a;
/// let c: Q = -a;
/// ```
fn neg(mut self) -> Self::Output {
unsafe { fmpq_neg(&mut self.value, &self.value) };
self
}
}

impl Neg for &Q {
type Output = Q;

/// Documentation at [`Q::neg`].
fn neg(self) -> Self::Output {
let mut out = Q::default();
unsafe { fmpq_neg(&mut out.value, &self.value) };
out
}
}

#[cfg(test)]
mod test_neg {
use super::Q;

/// Ensure that `neg` works for small numbers.
#[test]
fn correct_small() {
let a: Q = Q::ONE;
let b: Q = Q::MINUS_ONE;
let c: Q = Q::ZERO;

let d: Q = Q::from((1, 42));

assert_eq!(a, -(-&a));
assert_eq!(a, -&b);
assert_eq!(b, -&a);
assert_eq!(a, -b.clone());
assert_eq!(b, -a);
assert_eq!(c, -&c);
assert_eq!(c.clone(), -c);
assert_eq!(d, -(-&d));
assert_eq!(d.clone(), -(-d));
}

/// Ensure that `neg` works for large numbers.
#[test]
fn correct_large() {
let a: Q = Q::from(u64::MAX);
let b: Q = Q::from(i64::MAX);

assert_eq!(a, -(-&a));
assert_eq!(b, -(-&b));
}
}
Loading