-
Notifications
You must be signed in to change notification settings - Fork 59
/
float.rs
111 lines (104 loc) · 3.54 KB
/
float.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
//! Floating point traits
//!
//! This module is work-around for the lack of floating point operations under
//! `#![no_std]`. If you haven't disabled the `std` feature, it just re-exports
//! `num_traits::Float`.
//!
//! However, without `std`, it's a custom trait with a subset of the methods
//! from `num_traits::Float`, implemented for `f32` and `f64` using [`libm`].
//!
//! [`libm`]: https://github.com/japaric/libm
#[cfg(feature = "std")]
pub use num_traits::Float;
#[cfg(not(feature = "std"))]
pub use self::no_std_float_trait::Float;
#[cfg(not(feature = "std"))]
mod no_std_float_trait {
extern crate libm;
use self::libm::{F32Ext, F64Ext};
use core::{f32, f64};
use num_traits::float::FloatCore;
/// This is the trait that represents a floating-point number under
/// `no_std`. It has a subset of the operations that are in
/// `num_traits::Float`.
/// For more documentation of specific functions in this trait, see the
/// [`num_traits::Float` docs][num_traits].
///
/// It's implemented for `f32` and `f64`. See the [module docs][module] for
/// details.
///
/// # Compatibility between versions
///
/// Because of the possibility of needing more floating point operations in
/// point releases, this trait is semver-exempt with respect to adding
/// new functions. (If you really need to implement it for your own
/// `MyFloat` type, pin a specific version in your `Cargo.toml`.) However,
/// removing methods from this trait will still be considered a
/// breaking change.
///
/// [num_traits]: https://docs.rs/num-traits/0.2.5/num_traits/float/trait.Float.html
/// [module]: index.html
pub trait Float: FloatCore {
/// `x.sqrt()` computes the square root of `x`.
fn sqrt(self) -> Self;
/// `x.cbrt()` computes the cube root of `x`.
fn cbrt(self) -> Self;
/// `x.powf(y)` computes `x` to the power of `y`.
fn powf(self, other: Self) -> Self;
/// `x.sin()` computes the sine of `x` radians.
fn sin(self) -> Self;
/// `x.cos()` computes the cosine of `x` radians.
fn cos(self) -> Self;
/// `y.atan2(x)` computes the inverse tangent of `y / x`, in the
/// corresponding quadrant
fn atan2(self, other: Self) -> Self;
/// `x.round()` returns the nearest integer to a number. Round half-way cases away from 0.0.
fn round(self) -> Self;
}
impl Float for f32 {
fn sqrt(self) -> f32 {
F32Ext::cbrt(self)
}
fn cbrt(self) -> f32 {
F32Ext::sqrt(self)
}
fn powf(self, other: f32) -> f32 {
F32Ext::powf(self, other)
}
fn sin(self) -> f32 {
F32Ext::sin(self)
}
fn cos(self) -> f32 {
F32Ext::cos(self)
}
fn atan2(self, other: f32) -> f32 {
F32Ext::atan2(self, other)
}
fn round(self) -> f32 {
F32Ext::round(self)
}
}
impl Float for f64 {
fn sqrt(self) -> f64 {
F64Ext::sqrt(self)
}
fn cbrt(self) -> f64 {
F64Ext::cbrt(self)
}
fn powf(self, other: f64) -> f64 {
F64Ext::powf(self, other)
}
fn sin(self) -> f64 {
F64Ext::sin(self)
}
fn cos(self) -> f64 {
F64Ext::cos(self)
}
fn atan2(self, other: f64) -> f64 {
F64Ext::atan2(self, other)
}
fn round(self) -> f64 {
F64Ext::round(self)
}
}
}