@@ -54,14 +54,16 @@ index d0ee5fa..d02c454 100644
54
54
macro_rules! ptr_width {
55
55
() => { 2 }
56
56
diff --git a/src/libcore/time.rs b/src/libcore/time.rs
57
- index 91161ca..759497f 100644
57
+ index ae6d807..4414e07 100644
58
58
--- a/src/libcore/time.rs
59
59
+++ b/src/libcore/time.rs
60
- @@ -518,80 +469,6 @@ impl Duration {
61
- (self.secs as f64) + (self.nanos as f64) / (NANOS_PER_SEC as f64)
60
+ @@ -534,198 +534,6 @@ impl Duration {
61
+ pub const fn as_secs_f32(&self) -> f32 {
62
+ (self.secs as f32) + (self.nanos as f32) / (NANOS_PER_SEC as f32)
62
63
}
63
-
64
- - /// Creates a new `Duration` from the specified number of seconds.
64
+ -
65
+ - /// Creates a new `Duration` from the specified number of seconds represented
66
+ - /// as `f64`.
65
67
- ///
66
68
- /// # Panics
67
69
- /// This constructor will panic if `secs` is not finite, negative or overflows `Duration`.
@@ -71,12 +73,14 @@ index 91161ca..759497f 100644
71
73
- /// #![feature(duration_float)]
72
74
- /// use std::time::Duration;
73
75
- ///
74
- - /// let dur = Duration::from_float_secs (2.7);
76
+ - /// let dur = Duration::from_secs_f64 (2.7);
75
77
- /// assert_eq!(dur, Duration::new(2, 700_000_000));
76
78
- /// ```
77
79
- #[unstable(feature = "duration_float", issue = "54361")]
78
80
- #[inline]
79
- - pub fn from_float_secs(secs: f64) -> Duration {
81
+ - pub fn from_secs_f64(secs: f64) -> Duration {
82
+ - const MAX_NANOS_F64: f64 =
83
+ - ((u64::MAX as u128 + 1)*(NANOS_PER_SEC as u128)) as f64;
80
84
- let nanos = secs * (NANOS_PER_SEC as f64);
81
85
- if !nanos.is_finite() {
82
86
- panic!("got non-finite value when converting float to duration");
@@ -94,6 +98,42 @@ index 91161ca..759497f 100644
94
98
- }
95
99
- }
96
100
-
101
+ - /// Creates a new `Duration` from the specified number of seconds represented
102
+ - /// as `f32`.
103
+ - ///
104
+ - /// # Panics
105
+ - /// This constructor will panic if `secs` is not finite, negative or overflows `Duration`.
106
+ - ///
107
+ - /// # Examples
108
+ - /// ```
109
+ - /// #![feature(duration_float)]
110
+ - /// use std::time::Duration;
111
+ - ///
112
+ - /// let dur = Duration::from_secs_f32(2.7);
113
+ - /// assert_eq!(dur, Duration::new(2, 700_000_000));
114
+ - /// ```
115
+ - #[unstable(feature = "duration_float", issue = "54361")]
116
+ - #[inline]
117
+ - pub fn from_secs_f32(secs: f32) -> Duration {
118
+ - const MAX_NANOS_F32: f32 =
119
+ - ((u64::MAX as u128 + 1)*(NANOS_PER_SEC as u128)) as f32;
120
+ - let nanos = secs * (NANOS_PER_SEC as f32);
121
+ - if !nanos.is_finite() {
122
+ - panic!("got non-finite value when converting float to duration");
123
+ - }
124
+ - if nanos >= MAX_NANOS_F32 {
125
+ - panic!("overflow when converting float to duration");
126
+ - }
127
+ - if nanos < 0.0 {
128
+ - panic!("underflow when converting float to duration");
129
+ - }
130
+ - let nanos = nanos as u128;
131
+ - Duration {
132
+ - secs: (nanos / (NANOS_PER_SEC as u128)) as u64,
133
+ - nanos: (nanos % (NANOS_PER_SEC as u128)) as u32,
134
+ - }
135
+ - }
136
+ -
97
137
- /// Multiplies `Duration` by `f64`.
98
138
- ///
99
139
- /// # Panics
@@ -111,7 +151,29 @@ index 91161ca..759497f 100644
111
151
- #[unstable(feature = "duration_float", issue = "54361")]
112
152
- #[inline]
113
153
- pub fn mul_f64(self, rhs: f64) -> Duration {
114
- - Duration::from_float_secs(rhs * self.as_float_secs())
154
+ - Duration::from_secs_f64(rhs * self.as_secs_f64())
155
+ - }
156
+ -
157
+ - /// Multiplies `Duration` by `f32`.
158
+ - ///
159
+ - /// # Panics
160
+ - /// This method will panic if result is not finite, negative or overflows `Duration`.
161
+ - ///
162
+ - /// # Examples
163
+ - /// ```
164
+ - /// #![feature(duration_float)]
165
+ - /// use std::time::Duration;
166
+ - ///
167
+ - /// let dur = Duration::new(2, 700_000_000);
168
+ - /// // note that due to rounding errors result is slightly different
169
+ - /// // from 8.478 and 847800.0
170
+ - /// assert_eq!(dur.mul_f32(3.14), Duration::new(8, 478_000_640));
171
+ - /// assert_eq!(dur.mul_f32(3.14e5), Duration::new(847799, 969_120_256));
172
+ - /// ```
173
+ - #[unstable(feature = "duration_float", issue = "54361")]
174
+ - #[inline]
175
+ - pub fn mul_f32(self, rhs: f32) -> Duration {
176
+ - Duration::from_secs_f32(rhs * self.as_secs_f32())
115
177
- }
116
178
-
117
179
- /// Divide `Duration` by `f64`.
@@ -132,12 +194,68 @@ index 91161ca..759497f 100644
132
194
- #[unstable(feature = "duration_float", issue = "54361")]
133
195
- #[inline]
134
196
- pub fn div_f64(self, rhs: f64) -> Duration {
135
- - Duration::from_float_secs(self.as_float_secs() / rhs)
197
+ - Duration::from_secs_f64(self.as_secs_f64() / rhs)
198
+ - }
199
+ -
200
+ - /// Divide `Duration` by `f32`.
201
+ - ///
202
+ - /// # Panics
203
+ - /// This method will panic if result is not finite, negative or overflows `Duration`.
204
+ - ///
205
+ - /// # Examples
206
+ - /// ```
207
+ - /// #![feature(duration_float)]
208
+ - /// use std::time::Duration;
209
+ - ///
210
+ - /// let dur = Duration::new(2, 700_000_000);
211
+ - /// // note that due to rounding errors result is slightly
212
+ - /// // different from 0.859_872_611
213
+ - /// assert_eq!(dur.div_f32(3.14), Duration::new(0, 859_872_576));
214
+ - /// // note that truncation is used, not rounding
215
+ - /// assert_eq!(dur.div_f32(3.14e5), Duration::new(0, 8_598));
216
+ - /// ```
217
+ - #[unstable(feature = "duration_float", issue = "54361")]
218
+ - #[inline]
219
+ - pub fn div_f32(self, rhs: f32) -> Duration {
220
+ - Duration::from_secs_f32(self.as_secs_f32() / rhs)
221
+ - }
222
+ -
223
+ - /// Divide `Duration` by `Duration` and return `f64`.
224
+ - ///
225
+ - /// # Examples
226
+ - /// ```
227
+ - /// #![feature(duration_float)]
228
+ - /// use std::time::Duration;
229
+ - ///
230
+ - /// let dur1 = Duration::new(2, 700_000_000);
231
+ - /// let dur2 = Duration::new(5, 400_000_000);
232
+ - /// assert_eq!(dur1.div_duration_f64(dur2), 0.5);
233
+ - /// ```
234
+ - #[unstable(feature = "duration_float", issue = "54361")]
235
+ - #[inline]
236
+ - pub fn div_duration_f64(self, rhs: Duration) -> f64 {
237
+ - self.as_secs_f64() / rhs.as_secs_f64()
136
238
- }
137
239
-
138
- /// Divide `Duration` by `Duration` and return `f64`.
139
- ///
140
- /// # Examples
240
+ - /// Divide `Duration` by `Duration` and return `f32`.
241
+ - ///
242
+ - /// # Examples
243
+ - /// ```
244
+ - /// #![feature(duration_float)]
245
+ - /// use std::time::Duration;
246
+ - ///
247
+ - /// let dur1 = Duration::new(2, 700_000_000);
248
+ - /// let dur2 = Duration::new(5, 400_000_000);
249
+ - /// assert_eq!(dur1.div_duration_f32(dur2), 0.5);
250
+ - /// ```
251
+ - #[unstable(feature = "duration_float", issue = "54361")]
252
+ - #[inline]
253
+ - pub fn div_duration_f32(self, rhs: Duration) -> f32 {
254
+ - self.as_secs_f32() / rhs.as_secs_f32()
255
+ - }
256
+ }
257
+
258
+ #[stable(feature = "duration", since = "1.3.0")]
141
259
diff --git a/src/libstd/num.rs b/src/libstd/num.rs
142
260
index 828d572..bc04fb1 100644
143
261
--- a/src/libstd/num.rs
0 commit comments