multitype 0.21.0

Arithmetic type traits.
Documentation
// Copyright 2025-2026 Gabriel Bjørnager Jensen.
//
// SPDX: MIT OR Apache-2.0

//! The [`StdFloatingPoint`] trait.

#![cfg(feature = "std")]

mod test;

use multitype::FloatingPoint;

/// Extends [`FloatingPoint`] with [`std`]-only
/// functionality.
///
/// Note that as `core_float_math` stabilises (see
/// [`#137578`]), users of this trait should prepare
/// for its deprecation.
///
/// [`#137578`]: <https://github.com/rust-lang/rust/issues/137578/>
///
/// # Safety
///
/// All items must behave to the same specifications
/// as the standard items.
pub unsafe trait StdFloatingPoint: FloatingPoint {
	/// See [`f64::acos`].
	#[must_use]
	fn acos(self) -> Self;

	/// See [`f64::acosh`].
	#[must_use]
	fn acosh(self) -> Self;

	/// See [`f64::asin`].
	#[must_use]
	fn asin(self) -> Self;

	/// See [`f64::asinh`].
	#[must_use]
	fn asinh(self) -> Self;

	/// See [`f64::atan`].
	#[must_use]
	fn atan(self) -> Self;

	/// See [`f64::atan2`].
	#[must_use]
	fn atan2(self, rhs: Self) -> Self;

	/// See [`f64::atanh`].
	#[must_use]
	fn atanh(self) -> Self;

	/// See [`f64::cbrt`].
	#[must_use]
	fn cbrt(self) -> Self;

	/// See [`f64::ceil`].
	#[must_use]
	fn ceil(self) -> Self;

	/// See [`f64::cos`].
	#[must_use]
	fn cos(self) -> Self;

	/// See [`f64::cosh`].
	#[must_use]
	fn cosh(self) -> Self;

	/// See [`f64::div_euclid`].
	#[must_use]
	fn div_euclid(self, rhs: Self) -> Self;

	/// See [`f64::exp_m1`].
	#[must_use]
	fn exp_m1(self) -> Self;

	/// See [`f64::exp`].
	#[must_use]
	fn exp(self) -> Self;

	/// See [`f64::exp2`].
	#[must_use]
	fn exp2(self) -> Self;

	/// See [`f64::floor`].
	#[must_use]
	fn floor(self) -> Self;

	/// See [`f64::fract`].
	#[must_use]
	fn fract(self) -> Self;

	/// See [`f64::hypot`].
	#[must_use]
	fn hypot(self, rhs: Self) -> Self;

	/// See [`f64::ln`].
	#[must_use]
	fn ln(self) -> Self;

	/// See [`f64::ln_1p`].
	#[must_use]
	fn ln_1p(self) -> Self;

	/// See [`f64::log`].
	#[must_use]
	fn log(self, rhs: Self) -> Self;

	/// See [`f64::log10`].
	#[must_use]
	fn log10(self) -> Self;

	/// See [`f64::log2`].
	#[must_use]
	fn log2(self) -> Self;

	/// See [`f64::mul_add`].
	#[must_use]
	fn mul_add(self, mul: Self, add: Self) -> Self;

	/// See [`f64::powf`].
	#[must_use]
	fn powf(self, rhs: Self) -> Self;

	/// See [`f64::powi`].
	#[must_use]
	fn powi(self, rhs: i32) -> Self;

	/// See [`f64::rem_euclid`].
	#[must_use]
	fn rem_euclid(self, rhs: Self) -> Self;

	/// See [`f64::round_ties_even`].
	#[must_use]
	fn round_ties_even(self) -> Self;

	/// See [`f64::round`].
	#[must_use]
	fn round(self) -> Self;

	/// See [`f64::sin`].
	#[must_use]
	fn sin(self) -> Self;

	/// See [`f64::sin_cos`].
	#[must_use]
	fn sin_cos(self) -> (Self, Self);

	/// See [`f64::sinh`].
	#[must_use]
	fn sinh(self) -> Self;

	/// See [`f64::sqrt`].
	#[must_use]
	fn sqrt(self) -> Self;

	/// See [`f64::tan`].
	#[must_use]
	fn tan(self) -> Self;

	/// See [`f64::tanh`].
	#[must_use]
	fn tanh(self) -> Self;

	/// See [`f64::trunc`].
	#[must_use]
	fn trunc(self) -> Self;
}

/// Implements [`StdFloatingPoint`] for the given
/// types.
macro_rules! impl_std_floating_point {
	{
		$(
			$(#[$attr:meta])?
			$Ty:ty
		),*$(,)?
	} => {$(
		$(#[$attr])?
		unsafe impl ::multitype::StdFloatingPoint for $Ty {
			#[inline(always)]
			fn acos(self) -> Self {
				self.acos()
			}

			#[inline(always)]
			fn acosh(self) -> Self {
				self.acosh()
			}

			#[inline(always)]
			fn asin(self) -> Self {
				self.asin()
			}

			#[inline(always)]
			fn asinh(self) -> Self {
				self.asinh()
			}

			#[inline(always)]
			fn atan(self) -> Self {
				self.atan()
			}

			#[inline(always)]
			fn atan2(self, rhs: Self) -> Self {
				self.atan2(rhs)
			}

			#[inline(always)]
			fn atanh(self) -> Self {
				self.atanh()
			}

			#[inline(always)]
			fn cbrt(self) -> Self {
				self.cbrt()
			}

			#[inline(always)]
			fn ceil(self) -> Self {
				self.ceil()
			}
			#[inline(always)]
			fn cos(self) -> Self {
				self.cos()
			}

			#[inline(always)]
			fn cosh(self) -> Self {
				self.cosh()
			}

			#[inline(always)]
			fn div_euclid(self, rhs: Self) -> Self {
				self.div_euclid(rhs)
			}

			#[inline(always)]
			fn exp_m1(self) -> Self {
				self.exp_m1()
			}

			#[inline(always)]
			fn exp(self) -> Self {
				self.exp()
			}

			#[inline(always)]
			fn exp2(self) -> Self {
				self.exp2()
			}

			#[inline(always)]
			fn floor(self) -> Self {
				self.floor()
			}

			#[inline(always)]
			fn fract(self) -> Self {
				self.fract()
			}

			#[inline(always)]
			fn hypot(self, rhs: Self) -> Self {
				self.hypot(rhs)
			}

			#[inline(always)]
			fn ln(self) -> Self {
				self.ln()
			}

			#[inline(always)]
			fn ln_1p(self) -> Self {
				self.ln_1p()
			}

			#[inline(always)]
			fn log(self, rhs: Self) -> Self {
				self.log(rhs)
			}

			#[inline(always)]
			fn log10(self) -> Self {
				self.log10()
			}

			#[inline(always)]
			fn log2(self) -> Self {
				self.log2()
			}

			#[inline(always)]
			fn mul_add(self, mul: Self, add: Self) -> Self {
				self.mul_add(mul, add)
			}

			#[inline(always)]
			fn powf(self, rhs: Self) -> Self {
				self.powf(rhs)
			}

			#[inline(always)]
			fn powi(self, rhs: i32) -> Self {
				self.powi(rhs)
			}

			#[inline(always)]
			fn rem_euclid(self, rhs: Self) -> Self {
				self.rem_euclid(rhs)
			}

			#[inline(always)]
			fn round_ties_even(self) -> Self {
				self.round_ties_even()
			}

			#[inline(always)]
			fn round(self) -> Self {
				self.round()
			}

			#[inline(always)]
			fn sin(self) -> Self {
				self.sin()
			}

			#[inline(always)]
			fn sin_cos(self) -> (Self, Self) {
				self.sin_cos()
			}

			#[inline(always)]
			fn sinh(self) -> Self {
				self.sinh()
			}

			#[inline(always)]
			fn sqrt(self) -> Self {
				self.sqrt()
			}

			#[inline(always)]
			fn tan(self) -> Self {
				self.tan()
			}

			#[inline(always)]
			fn tanh(self) -> Self {
				self.tanh()
			}

			#[inline(always)]
			fn trunc(self) -> Self {
				self.trunc()
			}
		}
	)*}
}

impl_std_floating_point! {
	f32,
	f64,

	#[cfg(feature = "f128")]
	f128,

	#[cfg(feature = "f16")]
	f16,
}