Skip to content

Commit dbcc3fe

Browse files
committedApr 29, 2013
auto merge of #6110 : bjz/rust/numeric-traits, r=pcwalton
As discussed on issue #4819, I have created four new traits: `Algebraic`, `Trigonometric`, `Exponential` and `Hyperbolic`, and moved the appropriate methods into them from `Real`. ~~~rust pub trait Algebraic { fn pow(&self, n: Self) -> Self; fn sqrt(&self) -> Self; fn rsqrt(&self) -> Self; fn cbrt(&self) -> Self; fn hypot(&self, other: Self) -> Self; } pub trait Trigonometric { fn sin(&self) -> Self; fn cos(&self) -> Self; fn tan(&self) -> Self; fn asin(&self) -> Self; fn acos(&self) -> Self; fn atan(&self) -> Self; fn atan2(&self, other: Self) -> Self; } pub trait Exponential { fn exp(&self) -> Self; fn exp2(&self) -> Self; fn expm1(&self) -> Self; fn log(&self) -> Self; fn log2(&self) -> Self; fn log10(&self) -> Self; } pub trait Hyperbolic: Exponential { fn sinh(&self) -> Self; fn cosh(&self) -> Self; fn tanh(&self) -> Self; } ~~~ There was some discussion over whether we should shorten the names, for example `Trig` and `Exp`. No abbreviations have been agreed on yet, but this could be considered in the future. Additionally, `Integer::divisible_by` has been renamed to `Integer::is_multiple_of`.
2 parents 76ec35a + 500078e commit dbcc3fe

File tree

10 files changed

+380
-258
lines changed

10 files changed

+380
-258
lines changed
 

‎src/libcore/core.rc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,9 @@ pub use old_iter::{ExtendedMutableIter};
105105
pub use iter::Times;
106106

107107
pub use num::{Num, NumCast};
108-
pub use num::{Orderable, Signed, Unsigned, Integer};
109-
pub use num::{Round, Fractional, Real, RealExt};
108+
pub use num::{Orderable, Signed, Unsigned, Round};
109+
pub use num::{Algebraic, Trigonometric, Exponential, Hyperbolic};
110+
pub use num::{Integer, Fractional, Real, RealExt};
110111
pub use num::{Bitwise, BitCount, Bounded};
111112
pub use num::{Primitive, Int, Float};
112113

‎src/libcore/num/f32.rs

Lines changed: 70 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
//! Operations and constants for `f32`
1212
1313
use from_str;
14-
use libc::c_int;
1514
use num::{Zero, One, strconv};
1615
use prelude::*;
1716

@@ -102,8 +101,8 @@ delegate!(
102101
fn sinh(n: c_float) -> c_float = c_float_utils::sinh,
103102
fn tan(n: c_float) -> c_float = c_float_utils::tan,
104103
fn tanh(n: c_float) -> c_float = c_float_utils::tanh,
105-
fn tgamma(n: c_float) -> c_float = c_float_utils::tgamma)
106-
104+
fn tgamma(n: c_float) -> c_float = c_float_utils::tgamma
105+
)
107106

108107
// These are not defined inside consts:: for consistency with
109108
// the integer types
@@ -368,154 +367,153 @@ impl Fractional for f32 {
368367
fn recip(&self) -> f32 { 1.0 / *self }
369368
}
370369

371-
impl Real for f32 {
372-
/// Archimedes' constant
370+
impl Algebraic for f32 {
373371
#[inline(always)]
374-
fn pi() -> f32 { 3.14159265358979323846264338327950288 }
375-
376-
/// 2.0 * pi
377-
#[inline(always)]
378-
fn two_pi() -> f32 { 6.28318530717958647692528676655900576 }
379-
380-
/// pi / 2.0
381-
#[inline(always)]
382-
fn frac_pi_2() -> f32 { 1.57079632679489661923132169163975144 }
383-
384-
/// pi / 3.0
385-
#[inline(always)]
386-
fn frac_pi_3() -> f32 { 1.04719755119659774615421446109316763 }
372+
fn pow(&self, n: f32) -> f32 { pow(*self, n) }
387373

388-
/// pi / 4.0
389374
#[inline(always)]
390-
fn frac_pi_4() -> f32 { 0.785398163397448309615660845819875721 }
375+
fn sqrt(&self) -> f32 { sqrt(*self) }
391376

392-
/// pi / 6.0
393377
#[inline(always)]
394-
fn frac_pi_6() -> f32 { 0.52359877559829887307710723054658381 }
378+
fn rsqrt(&self) -> f32 { self.sqrt().recip() }
395379

396-
/// pi / 8.0
397380
#[inline(always)]
398-
fn frac_pi_8() -> f32 { 0.39269908169872415480783042290993786 }
381+
fn cbrt(&self) -> f32 { cbrt(*self) }
399382

400-
/// 1 .0/ pi
401383
#[inline(always)]
402-
fn frac_1_pi() -> f32 { 0.318309886183790671537767526745028724 }
384+
fn hypot(&self, other: f32) -> f32 { hypot(*self, other) }
385+
}
403386

404-
/// 2.0 / pi
387+
impl Trigonometric for f32 {
405388
#[inline(always)]
406-
fn frac_2_pi() -> f32 { 0.636619772367581343075535053490057448 }
389+
fn sin(&self) -> f32 { sin(*self) }
407390

408-
/// 2.0 / sqrt(pi)
409391
#[inline(always)]
410-
fn frac_2_sqrtpi() -> f32 { 1.12837916709551257389615890312154517 }
392+
fn cos(&self) -> f32 { cos(*self) }
411393

412-
/// sqrt(2.0)
413394
#[inline(always)]
414-
fn sqrt2() -> f32 { 1.41421356237309504880168872420969808 }
395+
fn tan(&self) -> f32 { tan(*self) }
415396

416-
/// 1.0 / sqrt(2.0)
417397
#[inline(always)]
418-
fn frac_1_sqrt2() -> f32 { 0.707106781186547524400844362104849039 }
398+
fn asin(&self) -> f32 { asin(*self) }
419399

420-
/// Euler's number
421400
#[inline(always)]
422-
fn e() -> f32 { 2.71828182845904523536028747135266250 }
401+
fn acos(&self) -> f32 { acos(*self) }
423402

424-
/// log2(e)
425403
#[inline(always)]
426-
fn log2_e() -> f32 { 1.44269504088896340735992468100189214 }
404+
fn atan(&self) -> f32 { atan(*self) }
427405

428-
/// log10(e)
429406
#[inline(always)]
430-
fn log10_e() -> f32 { 0.434294481903251827651128918916605082 }
407+
fn atan2(&self, other: f32) -> f32 { atan2(*self, other) }
408+
}
431409

432-
/// log(2.0)
410+
impl Exponential for f32 {
433411
#[inline(always)]
434-
fn log_2() -> f32 { 0.693147180559945309417232121458176568 }
412+
fn exp(&self) -> f32 { exp(*self) }
435413

436-
/// log(10.0)
437414
#[inline(always)]
438-
fn log_10() -> f32 { 2.30258509299404568401799145468436421 }
415+
fn exp2(&self) -> f32 { exp2(*self) }
439416

440417
#[inline(always)]
441-
fn pow(&self, n: f32) -> f32 { pow(*self, n) }
418+
fn expm1(&self) -> f32 { expm1(*self) }
442419

443420
#[inline(always)]
444-
fn exp(&self) -> f32 { exp(*self) }
421+
fn log(&self) -> f32 { ln(*self) }
445422

446423
#[inline(always)]
447-
fn exp2(&self) -> f32 { exp2(*self) }
424+
fn log2(&self) -> f32 { log2(*self) }
448425

449426
#[inline(always)]
450-
fn expm1(&self) -> f32 { expm1(*self) }
427+
fn log10(&self) -> f32 { log10(*self) }
428+
}
451429

430+
impl Hyperbolic for f32 {
452431
#[inline(always)]
453-
fn ldexp(&self, n: int) -> f32 { ldexp(*self, n as c_int) }
432+
fn sinh(&self) -> f32 { sinh(*self) }
454433

455434
#[inline(always)]
456-
fn log(&self) -> f32 { ln(*self) }
435+
fn cosh(&self) -> f32 { cosh(*self) }
457436

458437
#[inline(always)]
459-
fn log2(&self) -> f32 { log2(*self) }
438+
fn tanh(&self) -> f32 { tanh(*self) }
439+
}
460440

441+
impl Real for f32 {
442+
/// Archimedes' constant
461443
#[inline(always)]
462-
fn log10(&self) -> f32 { log10(*self) }
444+
fn pi() -> f32 { 3.14159265358979323846264338327950288 }
463445

446+
/// 2.0 * pi
464447
#[inline(always)]
465-
fn log_radix(&self) -> f32 { log_radix(*self) as f32 }
448+
fn two_pi() -> f32 { 6.28318530717958647692528676655900576 }
466449

450+
/// pi / 2.0
467451
#[inline(always)]
468-
fn ilog_radix(&self) -> int { ilog_radix(*self) as int }
452+
fn frac_pi_2() -> f32 { 1.57079632679489661923132169163975144 }
469453

454+
/// pi / 3.0
470455
#[inline(always)]
471-
fn sqrt(&self) -> f32 { sqrt(*self) }
456+
fn frac_pi_3() -> f32 { 1.04719755119659774615421446109316763 }
472457

458+
/// pi / 4.0
473459
#[inline(always)]
474-
fn rsqrt(&self) -> f32 { self.sqrt().recip() }
460+
fn frac_pi_4() -> f32 { 0.785398163397448309615660845819875721 }
475461

462+
/// pi / 6.0
476463
#[inline(always)]
477-
fn cbrt(&self) -> f32 { cbrt(*self) }
464+
fn frac_pi_6() -> f32 { 0.52359877559829887307710723054658381 }
478465

479-
/// Converts to degrees, assuming the number is in radians
466+
/// pi / 8.0
480467
#[inline(always)]
481-
fn to_degrees(&self) -> f32 { *self * (180.0 / Real::pi::<f32>()) }
468+
fn frac_pi_8() -> f32 { 0.39269908169872415480783042290993786 }
482469

483-
/// Converts to radians, assuming the number is in degrees
470+
/// 1 .0/ pi
484471
#[inline(always)]
485-
fn to_radians(&self) -> f32 { *self * (Real::pi::<f32>() / 180.0) }
472+
fn frac_1_pi() -> f32 { 0.318309886183790671537767526745028724 }
486473

474+
/// 2.0 / pi
487475
#[inline(always)]
488-
fn hypot(&self, other: f32) -> f32 { hypot(*self, other) }
476+
fn frac_2_pi() -> f32 { 0.636619772367581343075535053490057448 }
489477

478+
/// 2.0 / sqrt(pi)
490479
#[inline(always)]
491-
fn sin(&self) -> f32 { sin(*self) }
480+
fn frac_2_sqrtpi() -> f32 { 1.12837916709551257389615890312154517 }
492481

482+
/// sqrt(2.0)
493483
#[inline(always)]
494-
fn cos(&self) -> f32 { cos(*self) }
484+
fn sqrt2() -> f32 { 1.41421356237309504880168872420969808 }
495485

486+
/// 1.0 / sqrt(2.0)
496487
#[inline(always)]
497-
fn tan(&self) -> f32 { tan(*self) }
488+
fn frac_1_sqrt2() -> f32 { 0.707106781186547524400844362104849039 }
498489

490+
/// Euler's number
499491
#[inline(always)]
500-
fn asin(&self) -> f32 { asin(*self) }
492+
fn e() -> f32 { 2.71828182845904523536028747135266250 }
501493

494+
/// log2(e)
502495
#[inline(always)]
503-
fn acos(&self) -> f32 { acos(*self) }
496+
fn log2_e() -> f32 { 1.44269504088896340735992468100189214 }
504497

498+
/// log10(e)
505499
#[inline(always)]
506-
fn atan(&self) -> f32 { atan(*self) }
500+
fn log10_e() -> f32 { 0.434294481903251827651128918916605082 }
507501

502+
/// log(2.0)
508503
#[inline(always)]
509-
fn atan2(&self, other: f32) -> f32 { atan2(*self, other) }
504+
fn log_2() -> f32 { 0.693147180559945309417232121458176568 }
510505

506+
/// log(10.0)
511507
#[inline(always)]
512-
fn sinh(&self) -> f32 { sinh(*self) }
508+
fn log_10() -> f32 { 2.30258509299404568401799145468436421 }
513509

510+
/// Converts to degrees, assuming the number is in radians
514511
#[inline(always)]
515-
fn cosh(&self) -> f32 { cosh(*self) }
512+
fn to_degrees(&self) -> f32 { *self * (180.0 / Real::pi::<f32>()) }
516513

514+
/// Converts to radians, assuming the number is in degrees
517515
#[inline(always)]
518-
fn tanh(&self) -> f32 { tanh(*self) }
516+
fn to_radians(&self) -> f32 { *self * (Real::pi::<f32>() / 180.0) }
519517
}
520518

521519
impl Bounded for f32 {

‎src/libcore/num/f64.rs

Lines changed: 70 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ delegate!(
109109
fn jn(i: c_int, n: c_double) -> c_double = c_double_utils::jn,
110110
fn y0(n: c_double) -> c_double = c_double_utils::y0,
111111
fn y1(n: c_double) -> c_double = c_double_utils::y1,
112-
fn yn(i: c_int, n: c_double) -> c_double = c_double_utils::yn)
112+
fn yn(i: c_int, n: c_double) -> c_double = c_double_utils::yn
113+
)
113114

114115
// FIXME (#1433): obtain these in a different way
115116

@@ -378,154 +379,153 @@ impl Fractional for f64 {
378379
fn recip(&self) -> f64 { 1.0 / *self }
379380
}
380381

381-
impl Real for f64 {
382-
/// Archimedes' constant
382+
impl Algebraic for f64 {
383383
#[inline(always)]
384-
fn pi() -> f64 { 3.14159265358979323846264338327950288 }
385-
386-
/// 2.0 * pi
387-
#[inline(always)]
388-
fn two_pi() -> f64 { 6.28318530717958647692528676655900576 }
389-
390-
/// pi / 2.0
391-
#[inline(always)]
392-
fn frac_pi_2() -> f64 { 1.57079632679489661923132169163975144 }
384+
fn pow(&self, n: f64) -> f64 { pow(*self, n) }
393385

394-
/// pi / 3.0
395386
#[inline(always)]
396-
fn frac_pi_3() -> f64 { 1.04719755119659774615421446109316763 }
397-
398-
/// pi / 4.0
399-
#[inline(always)]
400-
fn frac_pi_4() -> f64 { 0.785398163397448309615660845819875721 }
387+
fn sqrt(&self) -> f64 { sqrt(*self) }
401388

402-
/// pi / 6.0
403389
#[inline(always)]
404-
fn frac_pi_6() -> f64 { 0.52359877559829887307710723054658381 }
390+
fn rsqrt(&self) -> f64 { self.sqrt().recip() }
405391

406-
/// pi / 8.0
407392
#[inline(always)]
408-
fn frac_pi_8() -> f64 { 0.39269908169872415480783042290993786 }
393+
fn cbrt(&self) -> f64 { cbrt(*self) }
409394

410-
/// 1.0 / pi
411395
#[inline(always)]
412-
fn frac_1_pi() -> f64 { 0.318309886183790671537767526745028724 }
396+
fn hypot(&self, other: f64) -> f64 { hypot(*self, other) }
397+
}
413398

414-
/// 2.0 / pi
399+
impl Trigonometric for f64 {
415400
#[inline(always)]
416-
fn frac_2_pi() -> f64 { 0.636619772367581343075535053490057448 }
401+
fn sin(&self) -> f64 { sin(*self) }
417402

418-
/// 2.0 / sqrt(pi)
419403
#[inline(always)]
420-
fn frac_2_sqrtpi() -> f64 { 1.12837916709551257389615890312154517 }
404+
fn cos(&self) -> f64 { cos(*self) }
421405

422-
/// sqrt(2.0)
423406
#[inline(always)]
424-
fn sqrt2() -> f64 { 1.41421356237309504880168872420969808 }
407+
fn tan(&self) -> f64 { tan(*self) }
425408

426-
/// 1.0 / sqrt(2.0)
427409
#[inline(always)]
428-
fn frac_1_sqrt2() -> f64 { 0.707106781186547524400844362104849039 }
410+
fn asin(&self) -> f64 { asin(*self) }
429411

430-
/// Euler's number
431412
#[inline(always)]
432-
fn e() -> f64 { 2.71828182845904523536028747135266250 }
413+
fn acos(&self) -> f64 { acos(*self) }
433414

434-
/// log2(e)
435415
#[inline(always)]
436-
fn log2_e() -> f64 { 1.44269504088896340735992468100189214 }
416+
fn atan(&self) -> f64 { atan(*self) }
437417

438-
/// log10(e)
439418
#[inline(always)]
440-
fn log10_e() -> f64 { 0.434294481903251827651128918916605082 }
419+
fn atan2(&self, other: f64) -> f64 { atan2(*self, other) }
420+
}
441421

442-
/// log(2.0)
422+
impl Exponential for f64 {
443423
#[inline(always)]
444-
fn log_2() -> f64 { 0.693147180559945309417232121458176568 }
424+
fn exp(&self) -> f64 { exp(*self) }
445425

446-
/// log(10.0)
447426
#[inline(always)]
448-
fn log_10() -> f64 { 2.30258509299404568401799145468436421 }
427+
fn exp2(&self) -> f64 { exp2(*self) }
449428

450429
#[inline(always)]
451-
fn pow(&self, n: f64) -> f64 { pow(*self, n) }
430+
fn expm1(&self) -> f64 { expm1(*self) }
452431

453432
#[inline(always)]
454-
fn exp(&self) -> f64 { exp(*self) }
433+
fn log(&self) -> f64 { ln(*self) }
455434

456435
#[inline(always)]
457-
fn exp2(&self) -> f64 { exp2(*self) }
436+
fn log2(&self) -> f64 { log2(*self) }
458437

459438
#[inline(always)]
460-
fn expm1(&self) -> f64 { expm1(*self) }
439+
fn log10(&self) -> f64 { log10(*self) }
440+
}
461441

442+
impl Hyperbolic for f64 {
462443
#[inline(always)]
463-
fn ldexp(&self, n: int) -> f64 { ldexp(*self, n as c_int) }
444+
fn sinh(&self) -> f64 { sinh(*self) }
464445

465446
#[inline(always)]
466-
fn log(&self) -> f64 { ln(*self) }
447+
fn cosh(&self) -> f64 { cosh(*self) }
467448

468449
#[inline(always)]
469-
fn log2(&self) -> f64 { log2(*self) }
450+
fn tanh(&self) -> f64 { tanh(*self) }
451+
}
470452

453+
impl Real for f64 {
454+
/// Archimedes' constant
471455
#[inline(always)]
472-
fn log10(&self) -> f64 { log10(*self) }
456+
fn pi() -> f64 { 3.14159265358979323846264338327950288 }
473457

458+
/// 2.0 * pi
474459
#[inline(always)]
475-
fn log_radix(&self) -> f64 { log_radix(*self) }
460+
fn two_pi() -> f64 { 6.28318530717958647692528676655900576 }
476461

462+
/// pi / 2.0
477463
#[inline(always)]
478-
fn ilog_radix(&self) -> int { ilog_radix(*self) as int }
464+
fn frac_pi_2() -> f64 { 1.57079632679489661923132169163975144 }
479465

466+
/// pi / 3.0
480467
#[inline(always)]
481-
fn sqrt(&self) -> f64 { sqrt(*self) }
468+
fn frac_pi_3() -> f64 { 1.04719755119659774615421446109316763 }
482469

470+
/// pi / 4.0
483471
#[inline(always)]
484-
fn rsqrt(&self) -> f64 { self.sqrt().recip() }
472+
fn frac_pi_4() -> f64 { 0.785398163397448309615660845819875721 }
485473

474+
/// pi / 6.0
486475
#[inline(always)]
487-
fn cbrt(&self) -> f64 { cbrt(*self) }
476+
fn frac_pi_6() -> f64 { 0.52359877559829887307710723054658381 }
488477

489-
/// Converts to degrees, assuming the number is in radians
478+
/// pi / 8.0
490479
#[inline(always)]
491-
fn to_degrees(&self) -> f64 { *self * (180.0 / Real::pi::<f64>()) }
480+
fn frac_pi_8() -> f64 { 0.39269908169872415480783042290993786 }
492481

493-
/// Converts to radians, assuming the number is in degrees
482+
/// 1.0 / pi
494483
#[inline(always)]
495-
fn to_radians(&self) -> f64 { *self * (Real::pi::<f64>() / 180.0) }
484+
fn frac_1_pi() -> f64 { 0.318309886183790671537767526745028724 }
496485

486+
/// 2.0 / pi
497487
#[inline(always)]
498-
fn hypot(&self, other: f64) -> f64 { hypot(*self, other) }
488+
fn frac_2_pi() -> f64 { 0.636619772367581343075535053490057448 }
499489

490+
/// 2.0 / sqrt(pi)
500491
#[inline(always)]
501-
fn sin(&self) -> f64 { sin(*self) }
492+
fn frac_2_sqrtpi() -> f64 { 1.12837916709551257389615890312154517 }
502493

494+
/// sqrt(2.0)
503495
#[inline(always)]
504-
fn cos(&self) -> f64 { cos(*self) }
496+
fn sqrt2() -> f64 { 1.41421356237309504880168872420969808 }
505497

498+
/// 1.0 / sqrt(2.0)
506499
#[inline(always)]
507-
fn tan(&self) -> f64 { tan(*self) }
500+
fn frac_1_sqrt2() -> f64 { 0.707106781186547524400844362104849039 }
508501

502+
/// Euler's number
509503
#[inline(always)]
510-
fn asin(&self) -> f64 { asin(*self) }
504+
fn e() -> f64 { 2.71828182845904523536028747135266250 }
511505

506+
/// log2(e)
512507
#[inline(always)]
513-
fn acos(&self) -> f64 { acos(*self) }
508+
fn log2_e() -> f64 { 1.44269504088896340735992468100189214 }
514509

510+
/// log10(e)
515511
#[inline(always)]
516-
fn atan(&self) -> f64 { atan(*self) }
512+
fn log10_e() -> f64 { 0.434294481903251827651128918916605082 }
517513

514+
/// log(2.0)
518515
#[inline(always)]
519-
fn atan2(&self, other: f64) -> f64 { atan2(*self, other) }
516+
fn log_2() -> f64 { 0.693147180559945309417232121458176568 }
520517

518+
/// log(10.0)
521519
#[inline(always)]
522-
fn sinh(&self) -> f64 { sinh(*self) }
520+
fn log_10() -> f64 { 2.30258509299404568401799145468436421 }
523521

522+
/// Converts to degrees, assuming the number is in radians
524523
#[inline(always)]
525-
fn cosh(&self) -> f64 { cosh(*self) }
524+
fn to_degrees(&self) -> f64 { *self * (180.0 / Real::pi::<f64>()) }
526525

526+
/// Converts to radians, assuming the number is in degrees
527527
#[inline(always)]
528-
fn tanh(&self) -> f64 { tanh(*self) }
528+
fn to_radians(&self) -> f64 { *self * (Real::pi::<f64>() / 180.0) }
529529
}
530530

531531
impl RealExt for f64 {

‎src/libcore/num/float.rs

Lines changed: 110 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -453,154 +453,195 @@ impl Fractional for float {
453453
fn recip(&self) -> float { 1.0 / *self }
454454
}
455455
456-
impl Real for float {
457-
/// Archimedes' constant
458-
#[inline(always)]
459-
fn pi() -> float { 3.14159265358979323846264338327950288 }
460-
461-
/// 2.0 * pi
462-
#[inline(always)]
463-
fn two_pi() -> float { 6.28318530717958647692528676655900576 }
464-
465-
/// pi / 2.0
466-
#[inline(always)]
467-
fn frac_pi_2() -> float { 1.57079632679489661923132169163975144 }
468-
469-
/// pi / 3.0
456+
impl Algebraic for float {
470457
#[inline(always)]
471-
fn frac_pi_3() -> float { 1.04719755119659774615421446109316763 }
458+
fn pow(&self, n: float) -> float {
459+
(*self as f64).pow(n as f64) as float
460+
}
472461
473-
/// pi / 4.0
474462
#[inline(always)]
475-
fn frac_pi_4() -> float { 0.785398163397448309615660845819875721 }
463+
fn sqrt(&self) -> float {
464+
(*self as f64).sqrt() as float
465+
}
476466
477-
/// pi / 6.0
478467
#[inline(always)]
479-
fn frac_pi_6() -> float { 0.52359877559829887307710723054658381 }
468+
fn rsqrt(&self) -> float {
469+
(*self as f64).rsqrt() as float
470+
}
480471
481-
/// pi / 8.0
482472
#[inline(always)]
483-
fn frac_pi_8() -> float { 0.39269908169872415480783042290993786 }
473+
fn cbrt(&self) -> float {
474+
(*self as f64).cbrt() as float
475+
}
484476
485-
/// 1.0 / pi
486477
#[inline(always)]
487-
fn frac_1_pi() -> float { 0.318309886183790671537767526745028724 }
478+
fn hypot(&self, other: float) -> float {
479+
(*self as f64).hypot(other as f64) as float
480+
}
481+
}
488482
489-
/// 2.0 / pi
483+
impl Trigonometric for float {
490484
#[inline(always)]
491-
fn frac_2_pi() -> float { 0.636619772367581343075535053490057448 }
485+
fn sin(&self) -> float {
486+
(*self as f64).sin() as float
487+
}
492488
493-
/// 2 .0/ sqrt(pi)
494489
#[inline(always)]
495-
fn frac_2_sqrtpi() -> float { 1.12837916709551257389615890312154517 }
490+
fn cos(&self) -> float {
491+
(*self as f64).cos() as float
492+
}
496493
497-
/// sqrt(2.0)
498494
#[inline(always)]
499-
fn sqrt2() -> float { 1.41421356237309504880168872420969808 }
495+
fn tan(&self) -> float {
496+
(*self as f64).tan() as float
497+
}
500498
501-
/// 1.0 / sqrt(2.0)
502499
#[inline(always)]
503-
fn frac_1_sqrt2() -> float { 0.707106781186547524400844362104849039 }
500+
fn asin(&self) -> float {
501+
(*self as f64).asin() as float
502+
}
504503
505-
/// Euler's number
506504
#[inline(always)]
507-
fn e() -> float { 2.71828182845904523536028747135266250 }
505+
fn acos(&self) -> float {
506+
(*self as f64).acos() as float
507+
}
508508
509-
/// log2(e)
510509
#[inline(always)]
511-
fn log2_e() -> float { 1.44269504088896340735992468100189214 }
510+
fn atan(&self) -> float {
511+
(*self as f64).atan() as float
512+
}
512513
513-
/// log10(e)
514514
#[inline(always)]
515-
fn log10_e() -> float { 0.434294481903251827651128918916605082 }
515+
fn atan2(&self, other: float) -> float {
516+
(*self as f64).atan2(other as f64) as float
517+
}
518+
}
516519
517-
/// log(2.0)
520+
impl Exponential for float {
518521
#[inline(always)]
519-
fn log_2() -> float { 0.693147180559945309417232121458176568 }
522+
fn exp(&self) -> float {
523+
(*self as f64).exp() as float
524+
}
520525
521-
/// log(10.0)
522526
#[inline(always)]
523-
fn log_10() -> float { 2.30258509299404568401799145468436421 }
527+
fn exp2(&self) -> float {
528+
(*self as f64).exp2() as float
529+
}
524530
525531
#[inline(always)]
526-
fn pow(&self, n: float) -> float { pow(*self as f64, n as f64) as float }
532+
fn expm1(&self) -> float {
533+
(*self as f64).expm1() as float
534+
}
527535
528536
#[inline(always)]
529-
fn exp(&self) -> float { exp(*self as f64) as float }
537+
fn log(&self) -> float {
538+
(*self as f64).log() as float
539+
}
530540
531541
#[inline(always)]
532-
fn exp2(&self) -> float { exp2(*self as f64) as float }
542+
fn log2(&self) -> float {
543+
(*self as f64).log2() as float
544+
}
533545
534546
#[inline(always)]
535-
fn expm1(&self) -> float { expm1(*self as f64) as float }
547+
fn log10(&self) -> float {
548+
(*self as f64).log10() as float
549+
}
550+
}
536551
552+
impl Hyperbolic for float {
537553
#[inline(always)]
538-
fn ldexp(&self, n: int) -> float { ldexp(*self as f64, n as c_int) as float }
554+
fn sinh(&self) -> float {
555+
(*self as f64).sinh() as float
556+
}
539557
540558
#[inline(always)]
541-
fn log(&self) -> float { ln(*self as f64) as float }
559+
fn cosh(&self) -> float {
560+
(*self as f64).cosh() as float
561+
}
542562
543563
#[inline(always)]
544-
fn log2(&self) -> float { log2(*self as f64) as float }
564+
fn tanh(&self) -> float {
565+
(*self as f64).tanh() as float
566+
}
567+
}
545568
569+
impl Real for float {
570+
/// Archimedes' constant
546571
#[inline(always)]
547-
fn log10(&self) -> float { log10(*self as f64) as float }
572+
fn pi() -> float { 3.14159265358979323846264338327950288 }
548573
574+
/// 2.0 * pi
549575
#[inline(always)]
550-
fn log_radix(&self) -> float { log_radix(*self as f64) as float }
576+
fn two_pi() -> float { 6.28318530717958647692528676655900576 }
551577
578+
/// pi / 2.0
552579
#[inline(always)]
553-
fn ilog_radix(&self) -> int { ilog_radix(*self as f64) as int }
580+
fn frac_pi_2() -> float { 1.57079632679489661923132169163975144 }
554581
582+
/// pi / 3.0
555583
#[inline(always)]
556-
fn sqrt(&self) -> float { sqrt(*self) }
584+
fn frac_pi_3() -> float { 1.04719755119659774615421446109316763 }
557585
586+
/// pi / 4.0
558587
#[inline(always)]
559-
fn rsqrt(&self) -> float { self.sqrt().recip() }
588+
fn frac_pi_4() -> float { 0.785398163397448309615660845819875721 }
560589
590+
/// pi / 6.0
561591
#[inline(always)]
562-
fn cbrt(&self) -> float { cbrt(*self as f64) as float }
592+
fn frac_pi_6() -> float { 0.52359877559829887307710723054658381 }
563593
564-
/// Converts to degrees, assuming the number is in radians
594+
/// pi / 8.0
565595
#[inline(always)]
566-
fn to_degrees(&self) -> float { *self * (180.0 / Real::pi::<float>()) }
596+
fn frac_pi_8() -> float { 0.39269908169872415480783042290993786 }
567597
568-
/// Converts to radians, assuming the number is in degrees
598+
/// 1.0 / pi
569599
#[inline(always)]
570-
fn to_radians(&self) -> float { *self * (Real::pi::<float>() / 180.0) }
600+
fn frac_1_pi() -> float { 0.318309886183790671537767526745028724 }
571601
602+
/// 2.0 / pi
572603
#[inline(always)]
573-
fn hypot(&self, other: float) -> float { hypot(*self as f64, other as f64) as float }
604+
fn frac_2_pi() -> float { 0.636619772367581343075535053490057448 }
574605
606+
/// 2 .0/ sqrt(pi)
575607
#[inline(always)]
576-
fn sin(&self) -> float { sin(*self) }
608+
fn frac_2_sqrtpi() -> float { 1.12837916709551257389615890312154517 }
577609
610+
/// sqrt(2.0)
578611
#[inline(always)]
579-
fn cos(&self) -> float { cos(*self) }
612+
fn sqrt2() -> float { 1.41421356237309504880168872420969808 }
580613
614+
/// 1.0 / sqrt(2.0)
581615
#[inline(always)]
582-
fn tan(&self) -> float { tan(*self) }
616+
fn frac_1_sqrt2() -> float { 0.707106781186547524400844362104849039 }
583617
618+
/// Euler's number
584619
#[inline(always)]
585-
fn asin(&self) -> float { asin(*self as f64) as float }
620+
fn e() -> float { 2.71828182845904523536028747135266250 }
586621
622+
/// log2(e)
587623
#[inline(always)]
588-
fn acos(&self) -> float { acos(*self as f64) as float }
624+
fn log2_e() -> float { 1.44269504088896340735992468100189214 }
589625
626+
/// log10(e)
590627
#[inline(always)]
591-
fn atan(&self) -> float { atan(*self) }
628+
fn log10_e() -> float { 0.434294481903251827651128918916605082 }
592629
630+
/// log(2.0)
593631
#[inline(always)]
594-
fn atan2(&self, other: float) -> float { atan2(*self as f64, other as f64) as float }
632+
fn log_2() -> float { 0.693147180559945309417232121458176568 }
595633
634+
/// log(10.0)
596635
#[inline(always)]
597-
fn sinh(&self) -> float { sinh(*self as f64) as float }
636+
fn log_10() -> float { 2.30258509299404568401799145468436421 }
598637
638+
/// Converts to degrees, assuming the number is in radians
599639
#[inline(always)]
600-
fn cosh(&self) -> float { cosh(*self as f64) as float }
640+
fn to_degrees(&self) -> float { (*self as f64).to_degrees() as float }
601641
642+
/// Converts to radians, assuming the number is in degrees
602643
#[inline(always)]
603-
fn tanh(&self) -> float { tanh(*self as f64) as float }
644+
fn to_radians(&self) -> float { (*self as f64).to_radians() as float }
604645
}
605646
606647
impl RealExt for float {

‎src/libcore/num/int-template.rs

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -406,11 +406,11 @@ impl Integer for T {
406406
407407
/// Returns `true` if the number can be divided by `other` without leaving a remainder
408408
#[inline(always)]
409-
fn divisible_by(&self, other: &T) -> bool { *self % *other == 0 }
409+
fn is_multiple_of(&self, other: &T) -> bool { *self % *other == 0 }
410410
411411
/// Returns `true` if the number is divisible by `2`
412412
#[inline(always)]
413-
fn is_even(&self) -> bool { self.divisible_by(&2) }
413+
fn is_even(&self) -> bool { self.is_multiple_of(&2) }
414414
415415
/// Returns `true` if the number is not divisible by `2`
416416
#[inline(always)]
@@ -682,6 +682,42 @@ mod tests {
682682
assert_eq!(-(0b11 as T) - (1 as T), (0b11 as T).not());
683683
}
684684
685+
#[test]
686+
fn test_multiple_of() {
687+
assert!((6 as T).is_multiple_of(&(6 as T)));
688+
assert!((6 as T).is_multiple_of(&(3 as T)));
689+
assert!((6 as T).is_multiple_of(&(1 as T)));
690+
assert!((-8 as T).is_multiple_of(&(4 as T)));
691+
assert!((8 as T).is_multiple_of(&(-1 as T)));
692+
assert!((-8 as T).is_multiple_of(&(-2 as T)));
693+
}
694+
695+
#[test]
696+
fn test_even() {
697+
assert_eq!((-4 as T).is_even(), true);
698+
assert_eq!((-3 as T).is_even(), false);
699+
assert_eq!((-2 as T).is_even(), true);
700+
assert_eq!((-1 as T).is_even(), false);
701+
assert_eq!((0 as T).is_even(), true);
702+
assert_eq!((1 as T).is_even(), false);
703+
assert_eq!((2 as T).is_even(), true);
704+
assert_eq!((3 as T).is_even(), false);
705+
assert_eq!((4 as T).is_even(), true);
706+
}
707+
708+
#[test]
709+
fn test_odd() {
710+
assert_eq!((-4 as T).is_odd(), false);
711+
assert_eq!((-3 as T).is_odd(), true);
712+
assert_eq!((-2 as T).is_odd(), false);
713+
assert_eq!((-1 as T).is_odd(), true);
714+
assert_eq!((0 as T).is_odd(), false);
715+
assert_eq!((1 as T).is_odd(), true);
716+
assert_eq!((2 as T).is_odd(), false);
717+
assert_eq!((3 as T).is_odd(), true);
718+
assert_eq!((4 as T).is_odd(), false);
719+
}
720+
685721
#[test]
686722
fn test_bitcount() {
687723
assert_eq!((0b010101 as T).population_count(), 3);

‎src/libcore/num/num.rs

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ pub trait Integer: Num
8585

8686
fn gcd(&self, other: &Self) -> Self;
8787
fn lcm(&self, other: &Self) -> Self;
88-
fn divisible_by(&self, other: &Self) -> bool;
88+
89+
fn is_multiple_of(&self, other: &Self) -> bool;
8990
fn is_even(&self) -> bool;
9091
fn is_odd(&self) -> bool;
9192
}
@@ -105,14 +106,47 @@ pub trait Fractional: Num
105106
fn recip(&self) -> Self;
106107
}
107108

109+
pub trait Algebraic {
110+
fn pow(&self, n: Self) -> Self;
111+
fn sqrt(&self) -> Self;
112+
fn rsqrt(&self) -> Self;
113+
fn cbrt(&self) -> Self;
114+
fn hypot(&self, other: Self) -> Self;
115+
}
116+
117+
pub trait Trigonometric {
118+
fn sin(&self) -> Self;
119+
fn cos(&self) -> Self;
120+
fn tan(&self) -> Self;
121+
fn asin(&self) -> Self;
122+
fn acos(&self) -> Self;
123+
fn atan(&self) -> Self;
124+
fn atan2(&self, other: Self) -> Self;
125+
}
126+
127+
pub trait Exponential {
128+
fn exp(&self) -> Self;
129+
fn exp2(&self) -> Self;
130+
fn expm1(&self) -> Self;
131+
fn log(&self) -> Self;
132+
fn log2(&self) -> Self;
133+
fn log10(&self) -> Self;
134+
}
135+
136+
pub trait Hyperbolic: Exponential {
137+
fn sinh(&self) -> Self;
138+
fn cosh(&self) -> Self;
139+
fn tanh(&self) -> Self;
140+
}
141+
108142
///
109143
/// Defines constants and methods common to real numbers
110144
///
111145
pub trait Real: Signed
112-
+ Fractional {
113-
// FIXME (#5527): usages of `int` should be replaced with an associated
114-
// integer type once these are implemented
115-
146+
+ Fractional
147+
+ Algebraic
148+
+ Trigonometric
149+
+ Hyperbolic {
116150
// Common Constants
117151
// FIXME (#5527): These should be associated constants
118152
fn pi() -> Self;
@@ -133,41 +167,9 @@ pub trait Real: Signed
133167
fn log_2() -> Self;
134168
fn log_10() -> Self;
135169

136-
// Exponential functions
137-
fn pow(&self, n: Self) -> Self;
138-
fn exp(&self) -> Self;
139-
fn exp2(&self) -> Self;
140-
fn expm1(&self) -> Self;
141-
fn ldexp(&self, n: int) -> Self;
142-
fn log(&self) -> Self;
143-
fn log2(&self) -> Self;
144-
fn log10(&self) -> Self;
145-
fn log_radix(&self) -> Self;
146-
fn ilog_radix(&self) -> int;
147-
fn sqrt(&self) -> Self;
148-
fn rsqrt(&self) -> Self;
149-
fn cbrt(&self) -> Self;
150-
151170
// Angular conversions
152171
fn to_degrees(&self) -> Self;
153172
fn to_radians(&self) -> Self;
154-
155-
// Triganomic functions
156-
fn hypot(&self, other: Self) -> Self;
157-
fn sin(&self) -> Self;
158-
fn cos(&self) -> Self;
159-
fn tan(&self) -> Self;
160-
161-
// Inverse triganomic functions
162-
fn asin(&self) -> Self;
163-
fn acos(&self) -> Self;
164-
fn atan(&self) -> Self;
165-
fn atan2(&self, other: Self) -> Self;
166-
167-
// Hyperbolic triganomic functions
168-
fn sinh(&self) -> Self;
169-
fn cosh(&self) -> Self;
170-
fn tanh(&self) -> Self;
171173
}
172174

173175
///

‎src/libcore/num/uint-template.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,11 +238,11 @@ impl Integer for T {
238238
239239
/// Returns `true` if the number can be divided by `other` without leaving a remainder
240240
#[inline(always)]
241-
fn divisible_by(&self, other: &T) -> bool { *self % *other == 0 }
241+
fn is_multiple_of(&self, other: &T) -> bool { *self % *other == 0 }
242242
243243
/// Returns `true` if the number is divisible by `2`
244244
#[inline(always)]
245-
fn is_even(&self) -> bool { self.divisible_by(&2) }
245+
fn is_even(&self) -> bool { self.is_multiple_of(&2) }
246246
247247
/// Returns `true` if the number is not divisible by `2`
248248
#[inline(always)]
@@ -415,6 +415,31 @@ mod tests {
415415
assert_eq!((99 as T).lcm(&17), 1683 as T);
416416
}
417417
418+
#[test]
419+
fn test_multiple_of() {
420+
assert!((6 as T).is_multiple_of(&(6 as T)));
421+
assert!((6 as T).is_multiple_of(&(3 as T)));
422+
assert!((6 as T).is_multiple_of(&(1 as T)));
423+
}
424+
425+
#[test]
426+
fn test_even() {
427+
assert_eq!((0 as T).is_even(), true);
428+
assert_eq!((1 as T).is_even(), false);
429+
assert_eq!((2 as T).is_even(), true);
430+
assert_eq!((3 as T).is_even(), false);
431+
assert_eq!((4 as T).is_even(), true);
432+
}
433+
434+
#[test]
435+
fn test_odd() {
436+
assert_eq!((0 as T).is_odd(), false);
437+
assert_eq!((1 as T).is_odd(), true);
438+
assert_eq!((2 as T).is_odd(), false);
439+
assert_eq!((3 as T).is_odd(), true);
440+
assert_eq!((4 as T).is_odd(), false);
441+
}
442+
418443
#[test]
419444
fn test_bitwise() {
420445
assert_eq!(0b1110 as T, (0b1100 as T).bitor(&(0b1010 as T)));

‎src/libcore/prelude.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@ pub use old_iter::{CopyableIter, CopyableOrderedIter, CopyableNonstrictIter};
3939
pub use old_iter::{ExtendedMutableIter};
4040
pub use iter::Times;
4141
pub use num::{Num, NumCast};
42-
pub use num::{Orderable, Signed, Unsigned, Integer};
43-
pub use num::{Round, Fractional, Real, RealExt};
42+
pub use num::{Orderable, Signed, Unsigned, Round};
43+
pub use num::{Algebraic, Trigonometric, Exponential, Hyperbolic};
44+
pub use num::{Integer, Fractional, Real, RealExt};
4445
pub use num::{Bitwise, BitCount, Bounded};
4546
pub use num::{Primitive, Int, Float};
4647
pub use path::GenericPath;

‎src/libstd/num/bigint.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ impl Integer for BigUint {
428428

429429
/// Returns `true` if the number can be divided by `other` without leaving a remainder
430430
#[inline(always)]
431-
fn divisible_by(&self, other: &BigUint) -> bool { (*self % *other).is_zero() }
431+
fn is_multiple_of(&self, other: &BigUint) -> bool { (*self % *other).is_zero() }
432432

433433
/// Returns `true` if the number is divisible by `2`
434434
#[inline(always)]
@@ -973,7 +973,7 @@ impl Integer for BigInt {
973973

974974
/// Returns `true` if the number can be divided by `other` without leaving a remainder
975975
#[inline(always)]
976-
fn divisible_by(&self, other: &BigInt) -> bool { self.data.divisible_by(&other.data) }
976+
fn is_multiple_of(&self, other: &BigInt) -> bool { self.data.is_multiple_of(&other.data) }
977977

978978
/// Returns `true` if the number is divisible by `2`
979979
#[inline(always)]

‎src/libstd/num/rational.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,9 @@ impl<T: Copy + Num + Ord>
203203
}
204204
}
205205
206+
impl<T: Copy + Num + Ord>
207+
Num for Ratio<T> {}
208+
206209
/* Utils */
207210
impl<T: Copy + Num + Ord>
208211
Round for Ratio<T> {
@@ -242,6 +245,12 @@ impl<T: Copy + Num + Ord>
242245
}
243246
}
244247
248+
impl<T: Copy + Num + Ord> Fractional for Ratio<T> {
249+
#[inline]
250+
fn recip(&self) -> Ratio<T> {
251+
Ratio::new_raw(self.denom, self.numer)
252+
}
253+
}
245254
246255
/* String conversions */
247256
impl<T: ToStr> ToStr for Ratio<T> {
@@ -446,6 +455,15 @@ mod test {
446455
assert_eq!(_3_2.fract(), _1_2);
447456
}
448457
458+
#[test]
459+
fn test_recip() {
460+
assert_eq!(_1 * _1.recip(), _1);
461+
assert_eq!(_2 * _2.recip(), _1);
462+
assert_eq!(_1_2 * _1_2.recip(), _1);
463+
assert_eq!(_3_2 * _3_2.recip(), _1);
464+
assert_eq!(_neg1_2 * _neg1_2.recip(), _1);
465+
}
466+
449467
#[test]
450468
fn test_to_from_str() {
451469
fn test(r: Rational, s: ~str) {

0 commit comments

Comments
 (0)
Please sign in to comment.