rust/src/libcore
bors ac69ee418b auto merge of #6048 : bjz/rust/numeric-traits, r=pcwalton
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.
2013-04-25 11:36:36 -07:00
..
num Restore Round trait and move appropriate methods out of Real 2013-04-25 11:53:04 +10:00
rt Fixing some various warnings about unused imports 2013-04-23 19:59:14 -04:00
str Fixed typo... And a billion other things. 2013-04-24 22:28:02 +02:00
task core, rustc: Warning police 2013-04-23 10:17:38 -07:00
unstable Fixed typo... And a billion other things. 2013-04-24 22:28:02 +02:00
at_vec.rs Replaced many instances of reinterpret_cast with transmute 2013-04-20 22:05:50 +02:00
bool.rs
cast.rs
cell.rs auto merge of #5966 : alexcrichton/rust/issue-3083, r=graydon 2013-04-22 15:36:51 -07:00
char.rs Removed ascii functions from other modules 2013-04-24 22:26:09 +02:00
cleanup.rs
clone.rs
cmp.rs
comm.rs core: clean up tests (mostly unused unsafe blocks) 2013-04-19 23:23:23 -04:00
condition.rs
container.rs
core.rc Restore Round trait and move appropriate methods out of Real 2013-04-25 11:53:04 +10:00
either.rs
flate.rs libcore: remove @Rng from rand, and use traits instead. 2013-04-24 22:34:10 +10:00
from_str.rs
gc.rs Removing more unnecessary unsafe blocks throughout 2013-04-23 19:59:13 -04:00
hash.rs
hashmap.rs libcore: unify gen_<type> methods on rand::RngUtil into the generic gen. 2013-04-24 22:34:19 +10:00
io.rs
iter.rs test: Rewrite nbody and spectralnorm shootout benchmarks 2013-04-19 11:56:52 -07:00
iterator.rs add a Counter iterator 2013-04-24 19:57:02 -04:00
kinds.rs
libc.rs Fix issue #5976 - HANDLE leaks and undefined/bad behavour 2013-04-23 21:23:15 +01:00
logging.rs
managed.rs
nil.rs
ops.rs Use #[cfg(not(stage0))] to exclude items from stage0 2013-04-25 08:20:00 +10:00
option.rs
os.rs libcore: remove @Rng from rand, and use traits instead. 2013-04-24 22:34:10 +10:00
owned.rs
path.rs Fixed typo... And a billion other things. 2013-04-24 22:28:02 +02:00
pipes.rs Removing more unnecessary unsafe blocks throughout 2013-04-23 19:59:13 -04:00
prelude.rs Restore Round trait and move appropriate methods out of Real 2013-04-25 11:53:04 +10:00
ptr.rs Replaced many instances of reinterpret_cast with transmute 2013-04-20 22:05:50 +02:00
rand.rs libcore: remove unnecessary deref 2013-04-24 23:03:04 +10:00
reflect.rs
repr.rs core: remove unused 'mut' variables 2013-04-20 21:02:38 -04:00
result.rs
run.rs Refactor core::run to remove the ProgRepr struct, which was 2013-04-24 21:06:26 +01:00
stackwalk.rs Replaced many instances of reinterpret_cast with transmute 2013-04-20 22:05:50 +02:00
str.rs Fixed typo... And a billion other things. 2013-04-24 22:28:02 +02:00
sys.rs libcore: wrappers for size/align_of to act on values without needing explicit ::<type> annotations 2013-04-20 15:05:36 +10:00
to_bytes.rs
to_str.rs Added Ascii type 2013-04-20 22:51:55 +02:00
trie.rs
tuple.rs
unicode.rs
unstable.rs Removing more unnecessary unsafe blocks throughout 2013-04-23 19:59:13 -04:00
util.rs
vec.rs core: remove unused 'mut' variables 2013-04-20 21:02:38 -04:00