Skip to content

Commit 31ffdaa

Browse files
committed
Add doctests to existing f16 and f128 functions
The symbols that these tests rely on are not available on all platforms, so these tests are configured to only run on x86.
1 parent b190746 commit 31ffdaa

File tree

2 files changed

+60
-4
lines changed

2 files changed

+60
-4
lines changed

library/core/src/num/f128.rs

+32-2
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,21 @@ impl f128 {
221221
pub const MAX_10_EXP: i32 = 4_932;
222222

223223
/// Returns `true` if this value is NaN.
224+
///
225+
/// ```
226+
/// #![feature(f128)]
227+
/// # #[cfg(target_arch = "x86_64")] { // FIXME(f16_f128): remove when `unordtf2` is available
228+
///
229+
/// let nan = f128::NAN;
230+
/// let f = 7.0_f128;
231+
///
232+
/// assert!(nan.is_nan());
233+
/// assert!(!f.is_nan());
234+
/// # }
235+
/// ```
224236
#[inline]
225237
#[must_use]
238+
#[cfg(not(bootstrap))]
226239
#[unstable(feature = "f128", issue = "116909")]
227240
#[allow(clippy::eq_op)] // > if you intended to check if the operand is NaN, use `.is_nan()` instead :)
228241
pub const fn is_nan(self) -> bool {
@@ -234,7 +247,7 @@ impl f128 {
234247
/// meaning to the sign bit in case of a NaN, and as Rust doesn't guarantee that
235248
/// the bit pattern of NaNs are conserved over arithmetic operations, the result of
236249
/// `is_sign_positive` on a NaN might produce an unexpected result in some cases.
237-
/// See [explanation of NaN as a special value](f32) for more info.
250+
/// See [explanation of NaN as a special value](f128) for more info.
238251
///
239252
/// ```
240253
/// #![feature(f128)]
@@ -257,7 +270,7 @@ impl f128 {
257270
/// meaning to the sign bit in case of a NaN, and as Rust doesn't guarantee that
258271
/// the bit pattern of NaNs are conserved over arithmetic operations, the result of
259272
/// `is_sign_negative` on a NaN might produce an unexpected result in some cases.
260-
/// See [explanation of NaN as a special value](f32) for more info.
273+
/// See [explanation of NaN as a special value](f128) for more info.
261274
///
262275
/// ```
263276
/// #![feature(f128)]
@@ -287,6 +300,14 @@ impl f128 {
287300
///
288301
/// Note that this function is distinct from `as` casting, which attempts to
289302
/// preserve the *numeric* value, and not the bitwise value.
303+
///
304+
/// ```
305+
/// #![feature(f128)]
306+
///
307+
/// # // FIXME(f16_f128): enable this once const casting works
308+
/// # // assert_ne!((1f128).to_bits(), 1f128 as u128); // to_bits() is not casting!
309+
/// assert_eq!((12.5f128).to_bits(), 0x40029000000000000000000000000000);
310+
/// ```
290311
#[inline]
291312
#[unstable(feature = "f128", issue = "116909")]
292313
#[must_use = "this returns the result of the operation, without modifying the original"]
@@ -326,6 +347,15 @@ impl f128 {
326347
///
327348
/// Note that this function is distinct from `as` casting, which attempts to
328349
/// preserve the *numeric* value, and not the bitwise value.
350+
///
351+
/// ```
352+
/// #![feature(f128)]
353+
/// # #[cfg(target_arch = "x86_64")] { // FIXME(f16_f128): remove when `eqtf2` is available
354+
///
355+
/// let v = f128::from_bits(0x40029000000000000000000000000000);
356+
/// assert_eq!(v, 12.5);
357+
/// # }
358+
/// ```
329359
#[inline]
330360
#[must_use]
331361
#[unstable(feature = "f128", issue = "116909")]

library/core/src/num/f16.rs

+28-2
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,19 @@ impl f16 {
216216
pub const MAX_10_EXP: i32 = 4;
217217

218218
/// Returns `true` if this value is NaN.
219+
///
220+
/// ```
221+
/// #![feature(f16)]
222+
///
223+
/// let nan = f16::NAN;
224+
/// let f = 7.0_f16;
225+
///
226+
/// assert!(nan.is_nan());
227+
/// assert!(!f.is_nan());
228+
/// ```
219229
#[inline]
220230
#[must_use]
231+
#[cfg(not(bootstrap))]
221232
#[unstable(feature = "f16", issue = "116909")]
222233
#[allow(clippy::eq_op)] // > if you intended to check if the operand is NaN, use `.is_nan()` instead :)
223234
pub const fn is_nan(self) -> bool {
@@ -229,7 +240,7 @@ impl f16 {
229240
/// meaning to the sign bit in case of a NaN, and as Rust doesn't guarantee that
230241
/// the bit pattern of NaNs are conserved over arithmetic operations, the result of
231242
/// `is_sign_positive` on a NaN might produce an unexpected result in some cases.
232-
/// See [explanation of NaN as a special value](f32) for more info.
243+
/// See [explanation of NaN as a special value](f16) for more info.
233244
///
234245
/// ```
235246
/// #![feature(f16)]
@@ -252,7 +263,7 @@ impl f16 {
252263
/// meaning to the sign bit in case of a NaN, and as Rust doesn't guarantee that
253264
/// the bit pattern of NaNs are conserved over arithmetic operations, the result of
254265
/// `is_sign_negative` on a NaN might produce an unexpected result in some cases.
255-
/// See [explanation of NaN as a special value](f32) for more info.
266+
/// See [explanation of NaN as a special value](f16) for more info.
256267
///
257268
/// ```
258269
/// #![feature(f16)]
@@ -282,6 +293,14 @@ impl f16 {
282293
///
283294
/// Note that this function is distinct from `as` casting, which attempts to
284295
/// preserve the *numeric* value, and not the bitwise value.
296+
///
297+
/// ```
298+
/// #![feature(f16)]
299+
///
300+
/// # // FIXME(f16_f128): enable this once const casting works
301+
/// # // assert_ne!((1f16).to_bits(), 1f16 as u128); // to_bits() is not casting!
302+
/// assert_eq!((12.5f16).to_bits(), 0x4a40);
303+
/// ```
285304
#[inline]
286305
#[unstable(feature = "f16", issue = "116909")]
287306
#[must_use = "this returns the result of the operation, without modifying the original"]
@@ -321,6 +340,13 @@ impl f16 {
321340
///
322341
/// Note that this function is distinct from `as` casting, which attempts to
323342
/// preserve the *numeric* value, and not the bitwise value.
343+
///
344+
/// ```
345+
/// #![feature(f16)]
346+
///
347+
/// let v = f16::from_bits(0x4a40);
348+
/// assert_eq!(v, 12.5);
349+
/// ```
324350
#[inline]
325351
#[must_use]
326352
#[unstable(feature = "f16", issue = "116909")]

0 commit comments

Comments
 (0)