Skip to content

Add Fractional, Real, and RealExt traits and update Round trait. Rename Natural trait to Integer. - #6048

Merged
bors merged 8 commits into
rust-lang:incomingfrom
brendanzab:numeric-traits
Apr 25, 2013
Merged

Add Fractional, Real, and RealExt traits and update Round trait. Rename Natural trait to Integer.#6048
bors merged 8 commits into
rust-lang:incomingfrom
brendanzab:numeric-traits

Conversation

@brendanzab

Copy link
Copy Markdown
Contributor

As part of the numeric trait reform (see issue #4819), I have added the following traits to core::num and implemented them for floating point types:

pubtraitRound{fnfloor(&self) -> Self;fnceil(&self) -> Self;fnround(&self) -> Self;fntrunc(&self) -> Self;fnfract(&self) -> Self;}pubtraitFractional:Num
+ Ord
+ Round
+ Quot<Self,Self>{fnrecip(&self) -> Self;}pubtraitReal:Signed
+ Fractional{// Common Constantsfnpi() -> Self;fntwo_pi() -> Self;fnfrac_pi_2() -> Self;fnfrac_pi_3() -> Self;fnfrac_pi_4() -> Self;fnfrac_pi_6() -> Self;fnfrac_pi_8() -> Self;fnfrac_1_pi() -> Self;fnfrac_2_pi() -> Self;fnfrac_2_sqrtpi() -> Self;fnsqrt2() -> Self;fnfrac_1_sqrt2() -> Self;fne() -> Self;fnlog2_e() -> Self;fnlog10_e() -> Self;fnlog_2() -> Self;fnlog_10() -> Self;// Exponential functionsfnpow(&self,n:Self) -> Self;fnexp(&self) -> Self;fnexp2(&self) -> Self;fnexpm1(&self) -> Self;fnldexp(&self,n:int) -> Self;fnlog(&self) -> Self;fnlog2(&self) -> Self;fnlog10(&self) -> Self;fnlog_radix(&self) -> Self;fnilog_radix(&self) -> int;fnsqrt(&self) -> Self;fnrsqrt(&self) -> Self;fncbrt(&self) -> Self;// Angular conversionsfnto_degrees(&self) -> Self;fnto_radians(&self) -> Self;// Triganomic functionsfnhypot(&self,other:Self) -> Self;fnsin(&self) -> Self;fncos(&self) -> Self;fntan(&self) -> Self;// Inverse triganomic functionsfnasin(&self) -> Self;fnacos(&self) -> Self;fnatan(&self) -> Self;fnatan2(&self,other:Self) -> Self;// Hyperbolic triganomic functionsfnsinh(&self) -> Self;fncosh(&self) -> Self;fntanh(&self) -> Self;}/// Methods that are harder to implement and not commonly used.pubtraitRealExt:Real{// Gamma functionsfnlgamma(&self) -> (int,Self);fntgamma(&self) -> Self;// Bessel functionsfnj0(&self) -> Self;fnj1(&self) -> Self;fnjn(&self,n:int) -> Self;fny0(&self) -> Self;fny1(&self) -> Self;fnyn(&self,n:int) -> Self;}

The constants in Real could be associated items in the future (see issue #5527). At the moment I have left the constants in {float|f32|f64}::consts in case folks need to access these at compile time. There are also instances of int in Real and RealExt. In the future these could be replaced with an associated INTEGER type on Real.

Natural has also been renamed to Integer. This is because Natural normally means 'positive integer' in mathematics. It is therefore strange to implement it on signed integer types. Integer is probably a better choice.

I have also switched some of the Integer methods to take borrowed pointers as arguments. This brings them in line with the Quot and Rem traits, and is be better for large Integer types like BigInt and BigUint because they don't need to be copied unnecessarily.

There has also been considerable discussion on the mailing list and IRC about the renaming of the Div and Modulo traits to Quot and Rem. Depending on the outcome of these discussions they might be renamed again.

'Natural' normally means 'positive integer' in mathematics. It is therefore strange to implement it on signed integer types. 'Integer' is probably a better choice.
This brings them in line with the quot and rem traits, and is be better for large Integer types like BigInt and BigUint because they don't need to be copied unnecessarily.
@brendanzab

Copy link
Copy Markdown
ContributorAuthor

Updated to fix a build error with std::rational::Ratio.

@brson

Copy link
Copy Markdown
Contributor

👍 Nice work.

@brendanzab

Copy link
Copy Markdown
ContributorAuthor

@brson Cheers! Is it just me or is @bors taking a while on this one?

bors added a commit that referenced this pull request Apr 25, 2013
As part of the numeric trait reform (see issue #4819), I have added the following traits to `core::num` and implemented them for floating point types:
~~~rust
pub trait Round {
fn floor(&self) -> Self;
fn ceil(&self) -> Self;
fn round(&self) -> Self;
fn trunc(&self) -> Self;
fn fract(&self) -> Self;
}
pub trait Fractional: Num
+ Ord
+ Round
+ Quot<Self,Self> {
fn recip(&self) -> Self;
}
pub trait Real: Signed
+ Fractional {
// Common Constants
fn pi() -> Self;
fn two_pi() -> Self;
fn frac_pi_2() -> Self;
fn frac_pi_3() -> Self;
fn frac_pi_4() -> Self;
fn frac_pi_6() -> Self;
fn frac_pi_8() -> Self;
fn frac_1_pi() -> Self;
fn frac_2_pi() -> Self;
fn frac_2_sqrtpi() -> Self;
fn sqrt2() -> Self;
fn frac_1_sqrt2() -> Self;
fn e() -> Self;
fn log2_e() -> Self;
fn log10_e() -> Self;
fn log_2() -> Self;
fn log_10() -> Self;
// Exponential functions
fn pow(&self, n: Self) -> Self;
fn exp(&self) -> Self;
fn exp2(&self) -> Self;
fn expm1(&self) -> Self;
fn ldexp(&self, n: int) -> Self;
fn log(&self) -> Self;
fn log2(&self) -> Self;
fn log10(&self) -> Self;
fn log_radix(&self) -> Self;
fn ilog_radix(&self) -> int;
fn sqrt(&self) -> Self;
fn rsqrt(&self) -> Self;
fn cbrt(&self) -> Self;
// Angular conversions
fn to_degrees(&self) -> Self;
fn to_radians(&self) -> Self;
// Triganomic functions
fn hypot(&self, other: Self) -> Self;
fn sin(&self) -> Self;
fn cos(&self) -> Self;
fn tan(&self) -> Self;
// Inverse triganomic functions
fn asin(&self) -> Self;
fn acos(&self) -> Self;
fn atan(&self) -> Self;
fn atan2(&self, other: Self) -> Self;
// Hyperbolic triganomic functions
fn sinh(&self) -> Self;
fn cosh(&self) -> Self;
fn tanh(&self) -> Self;
}
/// Methods that are harder to implement and not commonly used.
pub trait RealExt: Real {
// Gamma functions
fn lgamma(&self) -> (int, Self);
fn tgamma(&self) -> Self;
// Bessel functions
fn j0(&self) -> Self;
fn j1(&self) -> Self;
fn jn(&self, n: int) -> Self;
fn y0(&self) -> Self;
fn y1(&self) -> Self;
fn yn(&self, n: int) -> Self;
} ~~~
The constants in `Real` could be [associated items](http://smallcultfollowing.com/babysteps/blog/2013/04/03/associated-items-continued/) in the future (see issue #5527). At the moment I have left the constants in `{float|f32|f64}::consts` in case folks need to access these at compile time. There are also instances of `int` in `Real` and `RealExt`. In the future these could be replaced with an associated `INTEGER` type on `Real`.
`Natural` has also been renamed to `Integer`. This is because `Natural` normally means 'positive integer' in mathematics. It is therefore strange to implement it on signed integer types. `Integer` is probably a better choice.
I have also switched some of the `Integer` methods to take borrowed pointers as arguments. This brings them in line with the `Quot` and `Rem` traits, and is be better for large Integer types like `BigInt` and `BigUint` because they don't need to be copied unnecessarily.
There has also been considerable discussion on the mailing list and IRC about the renaming of the `Div` and `Modulo` traits to `Quot` and `Rem`. Depending on the outcome of these discussions they might be renamed again.
@borsbors closed this Apr 25, 2013
@bors
bors merged commit 225ac21 into rust-lang:incomingApr 25, 2013
@brendanzab
brendanzab deleted the numeric-traits branch April 25, 2013 19:49
bors added a commit that referenced this pull request Apr 28, 2013
This is a follow-up commit for #6041 (and depending on #6048).
Also adding `#[inline(always)]` for almost every methods in `std::bigint`.
flip1995 pushed a commit to flip1995/rust that referenced this pull request Sep 24, 2020
…, r=matthiaskrgr
Add note to `shadow_unrelated`
Fixrust-lang#5455.
This lint can be disabled at function level.
changelog: none
U007D pushed a commit to U007D/rust-mos that referenced this pull request Aug 21, 2026
6048: Code Docs r=matklad a=matklad
Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

@brendanzab@brson@pcwalton@bors