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

+3-2
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

+70-72
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 {

0 commit comments

Comments
 (0)
Please sign in to comment.