Skip to content

Commit 2d9dfc6

Browse files
committed
Reorder Float methods in trait definition and make consistent in impls
1 parent 42450ef commit 2d9dfc6

File tree

3 files changed

+268
-293
lines changed

3 files changed

+268
-293
lines changed

src/libstd/num/f32.rs

+105-100
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,9 @@ impl Signed for f32 {
218218
unsafe { intrinsics::fabsf32(*self) }
219219
}
220220

221-
/// The positive difference of two numbers. Returns `0.0` if the number is less than or
222-
/// equal to `other`, otherwise the difference between`self` and `other` is returned.
221+
/// The positive difference of two numbers. Returns `0.0` if the number is
222+
/// less than or equal to `other`, otherwise the difference between`self`
223+
/// and `other` is returned.
223224
#[inline]
224225
fn abs_sub(&self, other: &f32) -> f32 {
225226
unsafe { cmath::fdimf(*self, *other) }
@@ -257,20 +258,6 @@ impl Bounded for f32 {
257258
impl Primitive for f32 {}
258259

259260
impl Float for f32 {
260-
fn powi(self, n: i32) -> f32 {
261-
unsafe { intrinsics::powif32(self, n) }
262-
}
263-
264-
#[inline]
265-
fn max(self, other: f32) -> f32 {
266-
unsafe { cmath::fmaxf(self, other) }
267-
}
268-
269-
#[inline]
270-
fn min(self, other: f32) -> f32 {
271-
unsafe { cmath::fminf(self, other) }
272-
}
273-
274261
#[inline]
275262
fn nan() -> f32 { 0.0 / 0.0 }
276263

@@ -305,8 +292,9 @@ impl Float for f32 {
305292
self.classify() == FPNormal
306293
}
307294

308-
/// Returns the floating point category of the number. If only one property is going to
309-
/// be tested, it is generally faster to use the specific predicate instead.
295+
/// Returns the floating point category of the number. If only one property
296+
/// is going to be tested, it is generally faster to use the specific
297+
/// predicate instead.
310298
fn classify(self) -> FPCategory {
311299
static EXP_MASK: u32 = 0x7f800000;
312300
static MAN_MASK: u32 = 0x007fffff;
@@ -342,13 +330,15 @@ impl Float for f32 {
342330
#[inline]
343331
fn max_10_exp(_: Option<f32>) -> int { 38 }
344332

345-
/// Constructs a floating point number by multiplying `x` by 2 raised to the power of `exp`
333+
/// Constructs a floating point number by multiplying `x` by 2 raised to the
334+
/// power of `exp`
346335
#[inline]
347336
fn ldexp(x: f32, exp: int) -> f32 {
348337
unsafe { cmath::ldexpf(x, exp as c_int) }
349338
}
350339

351-
/// Breaks the number into a normalized fraction and a base-2 exponent, satisfying:
340+
/// Breaks the number into a normalized fraction and a base-2 exponent,
341+
/// satisfying:
352342
///
353343
/// - `self = x * pow(2, exp)`
354344
/// - `0.5 <= abs(x) < 1.0`
@@ -361,34 +351,6 @@ impl Float for f32 {
361351
}
362352
}
363353

364-
/// Returns the exponential of the number, minus `1`, in a way that is accurate
365-
/// even if the number is close to zero
366-
#[inline]
367-
fn exp_m1(self) -> f32 {
368-
unsafe { cmath::expm1f(self) }
369-
}
370-
371-
/// Returns the natural logarithm of the number plus `1` (`ln(1+n)`) more accurately
372-
/// than if the operations were performed separately
373-
#[inline]
374-
fn ln_1p(self) -> f32 {
375-
unsafe { cmath::log1pf(self) }
376-
}
377-
378-
/// Fused multiply-add. Computes `(self * a) + b` with only one rounding error. This
379-
/// produces a more accurate result with better performance than a separate multiplication
380-
/// operation followed by an add.
381-
#[inline]
382-
fn mul_add(self, a: f32, b: f32) -> f32 {
383-
unsafe { intrinsics::fmaf32(self, a, b) }
384-
}
385-
386-
/// Returns the next representable floating-point value in the direction of `other`
387-
#[inline]
388-
fn next_after(self, other: f32) -> f32 {
389-
unsafe { cmath::nextafterf(self, other) }
390-
}
391-
392354
/// Returns the mantissa, exponent and sign as integers.
393355
fn integer_decode(self) -> (u64, i16, i8) {
394356
let bits: u32 = unsafe { cast::transmute(self) };
@@ -404,6 +366,13 @@ impl Float for f32 {
404366
(mantissa as u64, exponent, sign)
405367
}
406368

369+
/// Returns the next representable floating-point value in the direction of
370+
/// `other`.
371+
#[inline]
372+
fn next_after(self, other: f32) -> f32 {
373+
unsafe { cmath::nextafterf(self, other) }
374+
}
375+
407376
/// Round half-way cases toward `NEG_INFINITY`
408377
#[inline]
409378
fn floor(self) -> f32 {
@@ -437,100 +406,102 @@ impl Float for f32 {
437406
#[inline]
438407
fn fract(self) -> f32 { self - self.trunc() }
439408

440-
/// Archimedes' constant
441-
#[inline]
442-
fn pi() -> f32 { 3.14159265358979323846264338327950288 }
443-
444-
/// 2.0 * pi
445409
#[inline]
446-
fn two_pi() -> f32 { 6.28318530717958647692528676655900576 }
410+
fn max(self, other: f32) -> f32 {
411+
unsafe { cmath::fmaxf(self, other) }
412+
}
447413

448-
/// pi / 2.0
449414
#[inline]
450-
fn frac_pi_2() -> f32 { 1.57079632679489661923132169163975144 }
415+
fn min(self, other: f32) -> f32 {
416+
unsafe { cmath::fminf(self, other) }
417+
}
451418

452-
/// pi / 3.0
419+
/// Fused multiply-add. Computes `(self * a) + b` with only one rounding
420+
/// error. This produces a more accurate result with better performance than
421+
/// a separate multiplication operation followed by an add.
453422
#[inline]
454-
fn frac_pi_3() -> f32 { 1.04719755119659774615421446109316763 }
423+
fn mul_add(self, a: f32, b: f32) -> f32 {
424+
unsafe { intrinsics::fmaf32(self, a, b) }
425+
}
455426

456-
/// pi / 4.0
427+
/// The reciprocal (multiplicative inverse) of the number
457428
#[inline]
458-
fn frac_pi_4() -> f32 { 0.785398163397448309615660845819875721 }
429+
fn recip(self) -> f32 { 1.0 / self }
459430

460-
/// pi / 6.0
461-
#[inline]
462-
fn frac_pi_6() -> f32 { 0.52359877559829887307710723054658381 }
431+
fn powi(self, n: i32) -> f32 {
432+
unsafe { intrinsics::powif32(self, n) }
433+
}
463434

464-
/// pi / 8.0
465435
#[inline]
466-
fn frac_pi_8() -> f32 { 0.39269908169872415480783042290993786 }
436+
fn powf(self, n: f32) -> f32 {
437+
unsafe { intrinsics::powf32(self, n) }
438+
}
467439

468-
/// 1 .0/ pi
440+
/// sqrt(2.0)
469441
#[inline]
470-
fn frac_1_pi() -> f32 { 0.318309886183790671537767526745028724 }
442+
fn sqrt2() -> f32 { 1.41421356237309504880168872420969808 }
471443

472-
/// 2.0 / pi
444+
/// 1.0 / sqrt(2.0)
473445
#[inline]
474-
fn frac_2_pi() -> f32 { 0.636619772367581343075535053490057448 }
446+
fn frac_1_sqrt2() -> f32 { 0.707106781186547524400844362104849039 }
475447

476-
/// 2.0 / sqrt(pi)
477448
#[inline]
478-
fn frac_2_sqrtpi() -> f32 { 1.12837916709551257389615890312154517 }
449+
fn sqrt(self) -> f32 {
450+
unsafe { intrinsics::sqrtf32(self) }
451+
}
479452

480-
/// sqrt(2.0)
481453
#[inline]
482-
fn sqrt2() -> f32 { 1.41421356237309504880168872420969808 }
454+
fn rsqrt(self) -> f32 { self.sqrt().recip() }
483455

484-
/// 1.0 / sqrt(2.0)
485456
#[inline]
486-
fn frac_1_sqrt2() -> f32 { 0.707106781186547524400844362104849039 }
457+
fn cbrt(self) -> f32 {
458+
unsafe { cmath::cbrtf(self) }
459+
}
487460

488-
/// Euler's number
489461
#[inline]
490-
fn e() -> f32 { 2.71828182845904523536028747135266250 }
462+
fn hypot(self, other: f32) -> f32 {
463+
unsafe { cmath::hypotf(self, other) }
464+
}
491465

492-
/// log2(e)
466+
/// Archimedes' constant
493467
#[inline]
494-
fn log2_e() -> f32 { 1.44269504088896340735992468100189214 }
468+
fn pi() -> f32 { 3.14159265358979323846264338327950288 }
495469

496-
/// log10(e)
470+
/// 2.0 * pi
497471
#[inline]
498-
fn log10_e() -> f32 { 0.434294481903251827651128918916605082 }
472+
fn two_pi() -> f32 { 6.28318530717958647692528676655900576 }
499473

500-
/// ln(2.0)
474+
/// pi / 2.0
501475
#[inline]
502-
fn ln_2() -> f32 { 0.693147180559945309417232121458176568 }
476+
fn frac_pi_2() -> f32 { 1.57079632679489661923132169163975144 }
503477

504-
/// ln(10.0)
478+
/// pi / 3.0
505479
#[inline]
506-
fn ln_10() -> f32 { 2.30258509299404568401799145468436421 }
480+
fn frac_pi_3() -> f32 { 1.04719755119659774615421446109316763 }
507481

508-
/// The reciprocal (multiplicative inverse) of the number
482+
/// pi / 4.0
509483
#[inline]
510-
fn recip(self) -> f32 { 1.0 / self }
484+
fn frac_pi_4() -> f32 { 0.785398163397448309615660845819875721 }
511485

486+
/// pi / 6.0
512487
#[inline]
513-
fn powf(self, n: f32) -> f32 {
514-
unsafe { intrinsics::powf32(self, n) }
515-
}
488+
fn frac_pi_6() -> f32 { 0.52359877559829887307710723054658381 }
516489

490+
/// pi / 8.0
517491
#[inline]
518-
fn sqrt(self) -> f32 {
519-
unsafe { intrinsics::sqrtf32(self) }
520-
}
492+
fn frac_pi_8() -> f32 { 0.39269908169872415480783042290993786 }
521493

494+
/// 1 .0/ pi
522495
#[inline]
523-
fn rsqrt(self) -> f32 { self.sqrt().recip() }
496+
fn frac_1_pi() -> f32 { 0.318309886183790671537767526745028724 }
524497

498+
/// 2.0 / pi
525499
#[inline]
526-
fn cbrt(self) -> f32 {
527-
unsafe { cmath::cbrtf(self) }
528-
}
500+
fn frac_2_pi() -> f32 { 0.636619772367581343075535053490057448 }
529501

502+
/// 2.0 / sqrt(pi)
530503
#[inline]
531-
fn hypot(self, other: f32) -> f32 {
532-
unsafe { cmath::hypotf(self, other) }
533-
}
504+
fn frac_2_sqrtpi() -> f32 { 1.12837916709551257389615890312154517 }
534505

535506
#[inline]
536507
fn sin(self) -> f32 {
@@ -573,6 +544,26 @@ impl Float for f32 {
573544
(self.sin(), self.cos())
574545
}
575546

547+
/// Euler's number
548+
#[inline]
549+
fn e() -> f32 { 2.71828182845904523536028747135266250 }
550+
551+
/// log2(e)
552+
#[inline]
553+
fn log2_e() -> f32 { 1.44269504088896340735992468100189214 }
554+
555+
/// log10(e)
556+
#[inline]
557+
fn log10_e() -> f32 { 0.434294481903251827651128918916605082 }
558+
559+
/// ln(2.0)
560+
#[inline]
561+
fn ln_2() -> f32 { 0.693147180559945309417232121458176568 }
562+
563+
/// ln(10.0)
564+
#[inline]
565+
fn ln_10() -> f32 { 2.30258509299404568401799145468436421 }
566+
576567
/// Returns the exponential of the number
577568
#[inline]
578569
fn exp(self) -> f32 {
@@ -585,6 +576,13 @@ impl Float for f32 {
585576
unsafe { intrinsics::exp2f32(self) }
586577
}
587578

579+
/// Returns the exponential of the number, minus `1`, in a way that is
580+
/// accurate even if the number is close to zero
581+
#[inline]
582+
fn exp_m1(self) -> f32 {
583+
unsafe { cmath::expm1f(self) }
584+
}
585+
588586
/// Returns the natural logarithm of the number
589587
#[inline]
590588
fn ln(self) -> f32 {
@@ -607,6 +605,13 @@ impl Float for f32 {
607605
unsafe { intrinsics::log10f32(self) }
608606
}
609607

608+
/// Returns the natural logarithm of the number plus `1` (`ln(1+n)`) more
609+
/// accurately than if the operations were performed separately
610+
#[inline]
611+
fn ln_1p(self) -> f32 {
612+
unsafe { cmath::log1pf(self) }
613+
}
614+
610615
#[inline]
611616
fn sinh(self) -> f32 {
612617
unsafe { cmath::sinhf(self) }

0 commit comments

Comments
 (0)