]> git.proxmox.com Git - rustc.git/blame - library/std/src/f32/tests.rs
New upstream version 1.65.0+dfsg1
[rustc.git] / library / std / src / f32 / tests.rs
CommitLineData
1b1a35ee
XL
1use crate::f32::consts;
2use crate::num::FpCategory as Fp;
3use crate::num::*;
4
5#[test]
6fn test_num_f32() {
7 test_num(10f32, 2f32);
8}
9
10#[test]
11fn test_min_nan() {
12 assert_eq!(f32::NAN.min(2.0), 2.0);
13 assert_eq!(2.0f32.min(f32::NAN), 2.0);
14}
15
16#[test]
17fn test_max_nan() {
18 assert_eq!(f32::NAN.max(2.0), 2.0);
19 assert_eq!(2.0f32.max(f32::NAN), 2.0);
20}
21
3c0e092e
XL
22#[test]
23fn test_minimum() {
24 assert!(f32::NAN.minimum(2.0).is_nan());
25 assert!(2.0f32.minimum(f32::NAN).is_nan());
26}
27
28#[test]
29fn test_maximum() {
30 assert!(f32::NAN.maximum(2.0).is_nan());
31 assert!(2.0f32.maximum(f32::NAN).is_nan());
32}
33
1b1a35ee
XL
34#[test]
35fn test_nan() {
36 let nan: f32 = f32::NAN;
37 assert!(nan.is_nan());
38 assert!(!nan.is_infinite());
39 assert!(!nan.is_finite());
40 assert!(!nan.is_normal());
41 assert!(nan.is_sign_positive());
42 assert!(!nan.is_sign_negative());
43 assert_eq!(Fp::Nan, nan.classify());
44}
45
46#[test]
47fn test_infinity() {
48 let inf: f32 = f32::INFINITY;
49 assert!(inf.is_infinite());
50 assert!(!inf.is_finite());
51 assert!(inf.is_sign_positive());
52 assert!(!inf.is_sign_negative());
53 assert!(!inf.is_nan());
54 assert!(!inf.is_normal());
55 assert_eq!(Fp::Infinite, inf.classify());
56}
57
58#[test]
59fn test_neg_infinity() {
60 let neg_inf: f32 = f32::NEG_INFINITY;
61 assert!(neg_inf.is_infinite());
62 assert!(!neg_inf.is_finite());
63 assert!(!neg_inf.is_sign_positive());
64 assert!(neg_inf.is_sign_negative());
65 assert!(!neg_inf.is_nan());
66 assert!(!neg_inf.is_normal());
67 assert_eq!(Fp::Infinite, neg_inf.classify());
68}
69
70#[test]
71fn test_zero() {
72 let zero: f32 = 0.0f32;
73 assert_eq!(0.0, zero);
74 assert!(!zero.is_infinite());
75 assert!(zero.is_finite());
76 assert!(zero.is_sign_positive());
77 assert!(!zero.is_sign_negative());
78 assert!(!zero.is_nan());
79 assert!(!zero.is_normal());
80 assert_eq!(Fp::Zero, zero.classify());
81}
82
83#[test]
84fn test_neg_zero() {
85 let neg_zero: f32 = -0.0;
86 assert_eq!(0.0, neg_zero);
87 assert!(!neg_zero.is_infinite());
88 assert!(neg_zero.is_finite());
89 assert!(!neg_zero.is_sign_positive());
90 assert!(neg_zero.is_sign_negative());
91 assert!(!neg_zero.is_nan());
92 assert!(!neg_zero.is_normal());
93 assert_eq!(Fp::Zero, neg_zero.classify());
94}
95
96#[test]
97fn test_one() {
98 let one: f32 = 1.0f32;
99 assert_eq!(1.0, one);
100 assert!(!one.is_infinite());
101 assert!(one.is_finite());
102 assert!(one.is_sign_positive());
103 assert!(!one.is_sign_negative());
104 assert!(!one.is_nan());
105 assert!(one.is_normal());
106 assert_eq!(Fp::Normal, one.classify());
107}
108
109#[test]
110fn test_is_nan() {
111 let nan: f32 = f32::NAN;
112 let inf: f32 = f32::INFINITY;
113 let neg_inf: f32 = f32::NEG_INFINITY;
114 assert!(nan.is_nan());
115 assert!(!0.0f32.is_nan());
116 assert!(!5.3f32.is_nan());
117 assert!(!(-10.732f32).is_nan());
118 assert!(!inf.is_nan());
119 assert!(!neg_inf.is_nan());
120}
121
122#[test]
123fn test_is_infinite() {
124 let nan: f32 = f32::NAN;
125 let inf: f32 = f32::INFINITY;
126 let neg_inf: f32 = f32::NEG_INFINITY;
127 assert!(!nan.is_infinite());
128 assert!(inf.is_infinite());
129 assert!(neg_inf.is_infinite());
130 assert!(!0.0f32.is_infinite());
131 assert!(!42.8f32.is_infinite());
132 assert!(!(-109.2f32).is_infinite());
133}
134
135#[test]
136fn test_is_finite() {
137 let nan: f32 = f32::NAN;
138 let inf: f32 = f32::INFINITY;
139 let neg_inf: f32 = f32::NEG_INFINITY;
140 assert!(!nan.is_finite());
141 assert!(!inf.is_finite());
142 assert!(!neg_inf.is_finite());
143 assert!(0.0f32.is_finite());
144 assert!(42.8f32.is_finite());
145 assert!((-109.2f32).is_finite());
146}
147
148#[test]
149fn test_is_normal() {
150 let nan: f32 = f32::NAN;
151 let inf: f32 = f32::INFINITY;
152 let neg_inf: f32 = f32::NEG_INFINITY;
153 let zero: f32 = 0.0f32;
154 let neg_zero: f32 = -0.0;
155 assert!(!nan.is_normal());
156 assert!(!inf.is_normal());
157 assert!(!neg_inf.is_normal());
158 assert!(!zero.is_normal());
159 assert!(!neg_zero.is_normal());
160 assert!(1f32.is_normal());
161 assert!(1e-37f32.is_normal());
162 assert!(!1e-38f32.is_normal());
163}
164
165#[test]
166fn test_classify() {
167 let nan: f32 = f32::NAN;
168 let inf: f32 = f32::INFINITY;
169 let neg_inf: f32 = f32::NEG_INFINITY;
170 let zero: f32 = 0.0f32;
171 let neg_zero: f32 = -0.0;
172 assert_eq!(nan.classify(), Fp::Nan);
173 assert_eq!(inf.classify(), Fp::Infinite);
174 assert_eq!(neg_inf.classify(), Fp::Infinite);
175 assert_eq!(zero.classify(), Fp::Zero);
176 assert_eq!(neg_zero.classify(), Fp::Zero);
177 assert_eq!(1f32.classify(), Fp::Normal);
178 assert_eq!(1e-37f32.classify(), Fp::Normal);
179 assert_eq!(1e-38f32.classify(), Fp::Subnormal);
180}
181
182#[test]
183fn test_floor() {
184 assert_approx_eq!(1.0f32.floor(), 1.0f32);
185 assert_approx_eq!(1.3f32.floor(), 1.0f32);
186 assert_approx_eq!(1.5f32.floor(), 1.0f32);
187 assert_approx_eq!(1.7f32.floor(), 1.0f32);
188 assert_approx_eq!(0.0f32.floor(), 0.0f32);
189 assert_approx_eq!((-0.0f32).floor(), -0.0f32);
190 assert_approx_eq!((-1.0f32).floor(), -1.0f32);
191 assert_approx_eq!((-1.3f32).floor(), -2.0f32);
192 assert_approx_eq!((-1.5f32).floor(), -2.0f32);
193 assert_approx_eq!((-1.7f32).floor(), -2.0f32);
194}
195
196#[test]
197fn test_ceil() {
198 assert_approx_eq!(1.0f32.ceil(), 1.0f32);
199 assert_approx_eq!(1.3f32.ceil(), 2.0f32);
200 assert_approx_eq!(1.5f32.ceil(), 2.0f32);
201 assert_approx_eq!(1.7f32.ceil(), 2.0f32);
202 assert_approx_eq!(0.0f32.ceil(), 0.0f32);
203 assert_approx_eq!((-0.0f32).ceil(), -0.0f32);
204 assert_approx_eq!((-1.0f32).ceil(), -1.0f32);
205 assert_approx_eq!((-1.3f32).ceil(), -1.0f32);
206 assert_approx_eq!((-1.5f32).ceil(), -1.0f32);
207 assert_approx_eq!((-1.7f32).ceil(), -1.0f32);
208}
209
210#[test]
211fn test_round() {
212 assert_approx_eq!(1.0f32.round(), 1.0f32);
213 assert_approx_eq!(1.3f32.round(), 1.0f32);
214 assert_approx_eq!(1.5f32.round(), 2.0f32);
215 assert_approx_eq!(1.7f32.round(), 2.0f32);
216 assert_approx_eq!(0.0f32.round(), 0.0f32);
217 assert_approx_eq!((-0.0f32).round(), -0.0f32);
218 assert_approx_eq!((-1.0f32).round(), -1.0f32);
219 assert_approx_eq!((-1.3f32).round(), -1.0f32);
220 assert_approx_eq!((-1.5f32).round(), -2.0f32);
221 assert_approx_eq!((-1.7f32).round(), -2.0f32);
222}
223
224#[test]
225fn test_trunc() {
226 assert_approx_eq!(1.0f32.trunc(), 1.0f32);
227 assert_approx_eq!(1.3f32.trunc(), 1.0f32);
228 assert_approx_eq!(1.5f32.trunc(), 1.0f32);
229 assert_approx_eq!(1.7f32.trunc(), 1.0f32);
230 assert_approx_eq!(0.0f32.trunc(), 0.0f32);
231 assert_approx_eq!((-0.0f32).trunc(), -0.0f32);
232 assert_approx_eq!((-1.0f32).trunc(), -1.0f32);
233 assert_approx_eq!((-1.3f32).trunc(), -1.0f32);
234 assert_approx_eq!((-1.5f32).trunc(), -1.0f32);
235 assert_approx_eq!((-1.7f32).trunc(), -1.0f32);
236}
237
238#[test]
239fn test_fract() {
240 assert_approx_eq!(1.0f32.fract(), 0.0f32);
241 assert_approx_eq!(1.3f32.fract(), 0.3f32);
242 assert_approx_eq!(1.5f32.fract(), 0.5f32);
243 assert_approx_eq!(1.7f32.fract(), 0.7f32);
244 assert_approx_eq!(0.0f32.fract(), 0.0f32);
245 assert_approx_eq!((-0.0f32).fract(), -0.0f32);
246 assert_approx_eq!((-1.0f32).fract(), -0.0f32);
247 assert_approx_eq!((-1.3f32).fract(), -0.3f32);
248 assert_approx_eq!((-1.5f32).fract(), -0.5f32);
249 assert_approx_eq!((-1.7f32).fract(), -0.7f32);
250}
251
252#[test]
253fn test_abs() {
254 assert_eq!(f32::INFINITY.abs(), f32::INFINITY);
255 assert_eq!(1f32.abs(), 1f32);
256 assert_eq!(0f32.abs(), 0f32);
257 assert_eq!((-0f32).abs(), 0f32);
258 assert_eq!((-1f32).abs(), 1f32);
259 assert_eq!(f32::NEG_INFINITY.abs(), f32::INFINITY);
260 assert_eq!((1f32 / f32::NEG_INFINITY).abs(), 0f32);
261 assert!(f32::NAN.abs().is_nan());
262}
263
264#[test]
265fn test_signum() {
266 assert_eq!(f32::INFINITY.signum(), 1f32);
267 assert_eq!(1f32.signum(), 1f32);
268 assert_eq!(0f32.signum(), 1f32);
269 assert_eq!((-0f32).signum(), -1f32);
270 assert_eq!((-1f32).signum(), -1f32);
271 assert_eq!(f32::NEG_INFINITY.signum(), -1f32);
272 assert_eq!((1f32 / f32::NEG_INFINITY).signum(), -1f32);
273 assert!(f32::NAN.signum().is_nan());
274}
275
276#[test]
277fn test_is_sign_positive() {
278 assert!(f32::INFINITY.is_sign_positive());
279 assert!(1f32.is_sign_positive());
280 assert!(0f32.is_sign_positive());
281 assert!(!(-0f32).is_sign_positive());
282 assert!(!(-1f32).is_sign_positive());
283 assert!(!f32::NEG_INFINITY.is_sign_positive());
284 assert!(!(1f32 / f32::NEG_INFINITY).is_sign_positive());
285 assert!(f32::NAN.is_sign_positive());
286 assert!(!(-f32::NAN).is_sign_positive());
287}
288
289#[test]
290fn test_is_sign_negative() {
291 assert!(!f32::INFINITY.is_sign_negative());
292 assert!(!1f32.is_sign_negative());
293 assert!(!0f32.is_sign_negative());
294 assert!((-0f32).is_sign_negative());
295 assert!((-1f32).is_sign_negative());
296 assert!(f32::NEG_INFINITY.is_sign_negative());
297 assert!((1f32 / f32::NEG_INFINITY).is_sign_negative());
298 assert!(!f32::NAN.is_sign_negative());
299 assert!((-f32::NAN).is_sign_negative());
300}
301
f2b60f7d
FG
302#[allow(unused_macros)]
303macro_rules! assert_f32_biteq {
304 ($left : expr, $right : expr) => {
305 let l: &f32 = &$left;
306 let r: &f32 = &$right;
307 let lb = l.to_bits();
308 let rb = r.to_bits();
309 assert_eq!(lb, rb, "float {} ({:#x}) is not equal to {} ({:#x})", *l, lb, *r, rb);
310 };
311}
312
313// Ignore test on x87 floating point, these platforms do not guarantee NaN
314// payloads are preserved and flush denormals to zero, failing the tests.
315#[cfg(not(target_arch = "x86"))]
316#[test]
317fn test_next_up() {
318 let tiny = f32::from_bits(1);
319 let tiny_up = f32::from_bits(2);
320 let max_down = f32::from_bits(0x7f7f_fffe);
321 let largest_subnormal = f32::from_bits(0x007f_ffff);
322 let smallest_normal = f32::from_bits(0x0080_0000);
323 assert_f32_biteq!(f32::NEG_INFINITY.next_up(), f32::MIN);
324 assert_f32_biteq!(f32::MIN.next_up(), -max_down);
325 assert_f32_biteq!((-1.0 - f32::EPSILON).next_up(), -1.0);
326 assert_f32_biteq!((-smallest_normal).next_up(), -largest_subnormal);
327 assert_f32_biteq!((-tiny_up).next_up(), -tiny);
328 assert_f32_biteq!((-tiny).next_up(), -0.0f32);
329 assert_f32_biteq!((-0.0f32).next_up(), tiny);
330 assert_f32_biteq!(0.0f32.next_up(), tiny);
331 assert_f32_biteq!(tiny.next_up(), tiny_up);
332 assert_f32_biteq!(largest_subnormal.next_up(), smallest_normal);
333 assert_f32_biteq!(1.0f32.next_up(), 1.0 + f32::EPSILON);
334 assert_f32_biteq!(f32::MAX.next_up(), f32::INFINITY);
335 assert_f32_biteq!(f32::INFINITY.next_up(), f32::INFINITY);
336
337 // Check that NaNs roundtrip.
338 let nan0 = f32::NAN;
339 let nan1 = f32::from_bits(f32::NAN.to_bits() ^ 0x002a_aaaa);
340 let nan2 = f32::from_bits(f32::NAN.to_bits() ^ 0x0055_5555);
341 assert_f32_biteq!(nan0.next_up(), nan0);
342 assert_f32_biteq!(nan1.next_up(), nan1);
343 assert_f32_biteq!(nan2.next_up(), nan2);
344}
345
346// Ignore test on x87 floating point, these platforms do not guarantee NaN
347// payloads are preserved and flush denormals to zero, failing the tests.
348#[cfg(not(target_arch = "x86"))]
349#[test]
350fn test_next_down() {
351 let tiny = f32::from_bits(1);
352 let tiny_up = f32::from_bits(2);
353 let max_down = f32::from_bits(0x7f7f_fffe);
354 let largest_subnormal = f32::from_bits(0x007f_ffff);
355 let smallest_normal = f32::from_bits(0x0080_0000);
356 assert_f32_biteq!(f32::NEG_INFINITY.next_down(), f32::NEG_INFINITY);
357 assert_f32_biteq!(f32::MIN.next_down(), f32::NEG_INFINITY);
358 assert_f32_biteq!((-max_down).next_down(), f32::MIN);
359 assert_f32_biteq!((-1.0f32).next_down(), -1.0 - f32::EPSILON);
360 assert_f32_biteq!((-largest_subnormal).next_down(), -smallest_normal);
361 assert_f32_biteq!((-tiny).next_down(), -tiny_up);
362 assert_f32_biteq!((-0.0f32).next_down(), -tiny);
363 assert_f32_biteq!((0.0f32).next_down(), -tiny);
364 assert_f32_biteq!(tiny.next_down(), 0.0f32);
365 assert_f32_biteq!(tiny_up.next_down(), tiny);
366 assert_f32_biteq!(smallest_normal.next_down(), largest_subnormal);
367 assert_f32_biteq!((1.0 + f32::EPSILON).next_down(), 1.0f32);
368 assert_f32_biteq!(f32::MAX.next_down(), max_down);
369 assert_f32_biteq!(f32::INFINITY.next_down(), f32::MAX);
370
371 // Check that NaNs roundtrip.
372 let nan0 = f32::NAN;
373 let nan1 = f32::from_bits(f32::NAN.to_bits() ^ 0x002a_aaaa);
374 let nan2 = f32::from_bits(f32::NAN.to_bits() ^ 0x0055_5555);
375 assert_f32_biteq!(nan0.next_down(), nan0);
376 assert_f32_biteq!(nan1.next_down(), nan1);
377 assert_f32_biteq!(nan2.next_down(), nan2);
378}
379
1b1a35ee
XL
380#[test]
381fn test_mul_add() {
382 let nan: f32 = f32::NAN;
383 let inf: f32 = f32::INFINITY;
384 let neg_inf: f32 = f32::NEG_INFINITY;
385 assert_approx_eq!(12.3f32.mul_add(4.5, 6.7), 62.05);
386 assert_approx_eq!((-12.3f32).mul_add(-4.5, -6.7), 48.65);
387 assert_approx_eq!(0.0f32.mul_add(8.9, 1.2), 1.2);
388 assert_approx_eq!(3.4f32.mul_add(-0.0, 5.6), 5.6);
389 assert!(nan.mul_add(7.8, 9.0).is_nan());
390 assert_eq!(inf.mul_add(7.8, 9.0), inf);
391 assert_eq!(neg_inf.mul_add(7.8, 9.0), neg_inf);
392 assert_eq!(8.9f32.mul_add(inf, 3.2), inf);
393 assert_eq!((-3.2f32).mul_add(2.4, neg_inf), neg_inf);
394}
395
396#[test]
397fn test_recip() {
398 let nan: f32 = f32::NAN;
399 let inf: f32 = f32::INFINITY;
400 let neg_inf: f32 = f32::NEG_INFINITY;
401 assert_eq!(1.0f32.recip(), 1.0);
402 assert_eq!(2.0f32.recip(), 0.5);
403 assert_eq!((-0.4f32).recip(), -2.5);
404 assert_eq!(0.0f32.recip(), inf);
405 assert!(nan.recip().is_nan());
406 assert_eq!(inf.recip(), 0.0);
407 assert_eq!(neg_inf.recip(), 0.0);
408}
409
410#[test]
411fn test_powi() {
412 let nan: f32 = f32::NAN;
413 let inf: f32 = f32::INFINITY;
414 let neg_inf: f32 = f32::NEG_INFINITY;
415 assert_eq!(1.0f32.powi(1), 1.0);
416 assert_approx_eq!((-3.1f32).powi(2), 9.61);
417 assert_approx_eq!(5.9f32.powi(-2), 0.028727);
418 assert_eq!(8.3f32.powi(0), 1.0);
419 assert!(nan.powi(2).is_nan());
420 assert_eq!(inf.powi(3), inf);
421 assert_eq!(neg_inf.powi(2), inf);
422}
423
424#[test]
425fn test_powf() {
426 let nan: f32 = f32::NAN;
427 let inf: f32 = f32::INFINITY;
428 let neg_inf: f32 = f32::NEG_INFINITY;
429 assert_eq!(1.0f32.powf(1.0), 1.0);
430 assert_approx_eq!(3.4f32.powf(4.5), 246.408218);
431 assert_approx_eq!(2.7f32.powf(-3.2), 0.041652);
432 assert_approx_eq!((-3.1f32).powf(2.0), 9.61);
433 assert_approx_eq!(5.9f32.powf(-2.0), 0.028727);
434 assert_eq!(8.3f32.powf(0.0), 1.0);
435 assert!(nan.powf(2.0).is_nan());
436 assert_eq!(inf.powf(2.0), inf);
437 assert_eq!(neg_inf.powf(3.0), neg_inf);
438}
439
440#[test]
441fn test_sqrt_domain() {
442 assert!(f32::NAN.sqrt().is_nan());
443 assert!(f32::NEG_INFINITY.sqrt().is_nan());
444 assert!((-1.0f32).sqrt().is_nan());
445 assert_eq!((-0.0f32).sqrt(), -0.0);
446 assert_eq!(0.0f32.sqrt(), 0.0);
447 assert_eq!(1.0f32.sqrt(), 1.0);
448 assert_eq!(f32::INFINITY.sqrt(), f32::INFINITY);
449}
450
451#[test]
452fn test_exp() {
453 assert_eq!(1.0, 0.0f32.exp());
454 assert_approx_eq!(2.718282, 1.0f32.exp());
455 assert_approx_eq!(148.413162, 5.0f32.exp());
456
457 let inf: f32 = f32::INFINITY;
458 let neg_inf: f32 = f32::NEG_INFINITY;
459 let nan: f32 = f32::NAN;
460 assert_eq!(inf, inf.exp());
461 assert_eq!(0.0, neg_inf.exp());
462 assert!(nan.exp().is_nan());
463}
464
465#[test]
466fn test_exp2() {
467 assert_eq!(32.0, 5.0f32.exp2());
468 assert_eq!(1.0, 0.0f32.exp2());
469
470 let inf: f32 = f32::INFINITY;
471 let neg_inf: f32 = f32::NEG_INFINITY;
472 let nan: f32 = f32::NAN;
473 assert_eq!(inf, inf.exp2());
474 assert_eq!(0.0, neg_inf.exp2());
475 assert!(nan.exp2().is_nan());
476}
477
478#[test]
479fn test_ln() {
480 let nan: f32 = f32::NAN;
481 let inf: f32 = f32::INFINITY;
482 let neg_inf: f32 = f32::NEG_INFINITY;
483 assert_approx_eq!(1.0f32.exp().ln(), 1.0);
484 assert!(nan.ln().is_nan());
485 assert_eq!(inf.ln(), inf);
486 assert!(neg_inf.ln().is_nan());
487 assert!((-2.3f32).ln().is_nan());
488 assert_eq!((-0.0f32).ln(), neg_inf);
489 assert_eq!(0.0f32.ln(), neg_inf);
490 assert_approx_eq!(4.0f32.ln(), 1.386294);
491}
492
493#[test]
494fn test_log() {
495 let nan: f32 = f32::NAN;
496 let inf: f32 = f32::INFINITY;
497 let neg_inf: f32 = f32::NEG_INFINITY;
498 assert_eq!(10.0f32.log(10.0), 1.0);
499 assert_approx_eq!(2.3f32.log(3.5), 0.664858);
500 assert_eq!(1.0f32.exp().log(1.0f32.exp()), 1.0);
501 assert!(1.0f32.log(1.0).is_nan());
502 assert!(1.0f32.log(-13.9).is_nan());
503 assert!(nan.log(2.3).is_nan());
504 assert_eq!(inf.log(10.0), inf);
505 assert!(neg_inf.log(8.8).is_nan());
506 assert!((-2.3f32).log(0.1).is_nan());
507 assert_eq!((-0.0f32).log(2.0), neg_inf);
508 assert_eq!(0.0f32.log(7.0), neg_inf);
509}
510
511#[test]
512fn test_log2() {
513 let nan: f32 = f32::NAN;
514 let inf: f32 = f32::INFINITY;
515 let neg_inf: f32 = f32::NEG_INFINITY;
516 assert_approx_eq!(10.0f32.log2(), 3.321928);
517 assert_approx_eq!(2.3f32.log2(), 1.201634);
518 assert_approx_eq!(1.0f32.exp().log2(), 1.442695);
519 assert!(nan.log2().is_nan());
520 assert_eq!(inf.log2(), inf);
521 assert!(neg_inf.log2().is_nan());
522 assert!((-2.3f32).log2().is_nan());
523 assert_eq!((-0.0f32).log2(), neg_inf);
524 assert_eq!(0.0f32.log2(), neg_inf);
525}
526
527#[test]
528fn test_log10() {
529 let nan: f32 = f32::NAN;
530 let inf: f32 = f32::INFINITY;
531 let neg_inf: f32 = f32::NEG_INFINITY;
532 assert_eq!(10.0f32.log10(), 1.0);
533 assert_approx_eq!(2.3f32.log10(), 0.361728);
534 assert_approx_eq!(1.0f32.exp().log10(), 0.434294);
535 assert_eq!(1.0f32.log10(), 0.0);
536 assert!(nan.log10().is_nan());
537 assert_eq!(inf.log10(), inf);
538 assert!(neg_inf.log10().is_nan());
539 assert!((-2.3f32).log10().is_nan());
540 assert_eq!((-0.0f32).log10(), neg_inf);
541 assert_eq!(0.0f32.log10(), neg_inf);
542}
543
544#[test]
545fn test_to_degrees() {
546 let pi: f32 = consts::PI;
547 let nan: f32 = f32::NAN;
548 let inf: f32 = f32::INFINITY;
549 let neg_inf: f32 = f32::NEG_INFINITY;
550 assert_eq!(0.0f32.to_degrees(), 0.0);
551 assert_approx_eq!((-5.8f32).to_degrees(), -332.315521);
552 assert_eq!(pi.to_degrees(), 180.0);
553 assert!(nan.to_degrees().is_nan());
554 assert_eq!(inf.to_degrees(), inf);
555 assert_eq!(neg_inf.to_degrees(), neg_inf);
556 assert_eq!(1_f32.to_degrees(), 57.2957795130823208767981548141051703);
557}
558
559#[test]
560fn test_to_radians() {
561 let pi: f32 = consts::PI;
562 let nan: f32 = f32::NAN;
563 let inf: f32 = f32::INFINITY;
564 let neg_inf: f32 = f32::NEG_INFINITY;
565 assert_eq!(0.0f32.to_radians(), 0.0);
566 assert_approx_eq!(154.6f32.to_radians(), 2.698279);
567 assert_approx_eq!((-332.31f32).to_radians(), -5.799903);
568 assert_eq!(180.0f32.to_radians(), pi);
569 assert!(nan.to_radians().is_nan());
570 assert_eq!(inf.to_radians(), inf);
571 assert_eq!(neg_inf.to_radians(), neg_inf);
572}
573
574#[test]
575fn test_asinh() {
576 assert_eq!(0.0f32.asinh(), 0.0f32);
577 assert_eq!((-0.0f32).asinh(), -0.0f32);
578
579 let inf: f32 = f32::INFINITY;
580 let neg_inf: f32 = f32::NEG_INFINITY;
581 let nan: f32 = f32::NAN;
582 assert_eq!(inf.asinh(), inf);
583 assert_eq!(neg_inf.asinh(), neg_inf);
584 assert!(nan.asinh().is_nan());
585 assert!((-0.0f32).asinh().is_sign_negative()); // issue 63271
586 assert_approx_eq!(2.0f32.asinh(), 1.443635475178810342493276740273105f32);
587 assert_approx_eq!((-2.0f32).asinh(), -1.443635475178810342493276740273105f32);
588 // regression test for the catastrophic cancellation fixed in 72486
589 assert_approx_eq!((-3000.0f32).asinh(), -8.699514775987968673236893537700647f32);
590}
591
592#[test]
593fn test_acosh() {
594 assert_eq!(1.0f32.acosh(), 0.0f32);
595 assert!(0.999f32.acosh().is_nan());
596
597 let inf: f32 = f32::INFINITY;
598 let neg_inf: f32 = f32::NEG_INFINITY;
599 let nan: f32 = f32::NAN;
600 assert_eq!(inf.acosh(), inf);
601 assert!(neg_inf.acosh().is_nan());
602 assert!(nan.acosh().is_nan());
603 assert_approx_eq!(2.0f32.acosh(), 1.31695789692481670862504634730796844f32);
604 assert_approx_eq!(3.0f32.acosh(), 1.76274717403908605046521864995958461f32);
605}
606
607#[test]
608fn test_atanh() {
609 assert_eq!(0.0f32.atanh(), 0.0f32);
610 assert_eq!((-0.0f32).atanh(), -0.0f32);
611
612 let inf32: f32 = f32::INFINITY;
613 let neg_inf32: f32 = f32::NEG_INFINITY;
614 assert_eq!(1.0f32.atanh(), inf32);
615 assert_eq!((-1.0f32).atanh(), neg_inf32);
616
617 assert!(2f64.atanh().atanh().is_nan());
618 assert!((-2f64).atanh().atanh().is_nan());
619
620 let inf64: f32 = f32::INFINITY;
621 let neg_inf64: f32 = f32::NEG_INFINITY;
622 let nan32: f32 = f32::NAN;
623 assert!(inf64.atanh().is_nan());
624 assert!(neg_inf64.atanh().is_nan());
625 assert!(nan32.atanh().is_nan());
626
627 assert_approx_eq!(0.5f32.atanh(), 0.54930614433405484569762261846126285f32);
628 assert_approx_eq!((-0.5f32).atanh(), -0.54930614433405484569762261846126285f32);
629}
630
631#[test]
632fn test_real_consts() {
633 use super::consts;
634
635 let pi: f32 = consts::PI;
636 let frac_pi_2: f32 = consts::FRAC_PI_2;
637 let frac_pi_3: f32 = consts::FRAC_PI_3;
638 let frac_pi_4: f32 = consts::FRAC_PI_4;
639 let frac_pi_6: f32 = consts::FRAC_PI_6;
640 let frac_pi_8: f32 = consts::FRAC_PI_8;
641 let frac_1_pi: f32 = consts::FRAC_1_PI;
642 let frac_2_pi: f32 = consts::FRAC_2_PI;
643 let frac_2_sqrtpi: f32 = consts::FRAC_2_SQRT_PI;
644 let sqrt2: f32 = consts::SQRT_2;
645 let frac_1_sqrt2: f32 = consts::FRAC_1_SQRT_2;
646 let e: f32 = consts::E;
647 let log2_e: f32 = consts::LOG2_E;
648 let log10_e: f32 = consts::LOG10_E;
649 let ln_2: f32 = consts::LN_2;
650 let ln_10: f32 = consts::LN_10;
651
652 assert_approx_eq!(frac_pi_2, pi / 2f32);
653 assert_approx_eq!(frac_pi_3, pi / 3f32);
654 assert_approx_eq!(frac_pi_4, pi / 4f32);
655 assert_approx_eq!(frac_pi_6, pi / 6f32);
656 assert_approx_eq!(frac_pi_8, pi / 8f32);
657 assert_approx_eq!(frac_1_pi, 1f32 / pi);
658 assert_approx_eq!(frac_2_pi, 2f32 / pi);
659 assert_approx_eq!(frac_2_sqrtpi, 2f32 / pi.sqrt());
660 assert_approx_eq!(sqrt2, 2f32.sqrt());
661 assert_approx_eq!(frac_1_sqrt2, 1f32 / 2f32.sqrt());
662 assert_approx_eq!(log2_e, e.log2());
663 assert_approx_eq!(log10_e, e.log10());
664 assert_approx_eq!(ln_2, 2f32.ln());
665 assert_approx_eq!(ln_10, 10f32.ln());
666}
667
668#[test]
669fn test_float_bits_conv() {
670 assert_eq!((1f32).to_bits(), 0x3f800000);
671 assert_eq!((12.5f32).to_bits(), 0x41480000);
672 assert_eq!((1337f32).to_bits(), 0x44a72000);
673 assert_eq!((-14.25f32).to_bits(), 0xc1640000);
674 assert_approx_eq!(f32::from_bits(0x3f800000), 1.0);
675 assert_approx_eq!(f32::from_bits(0x41480000), 12.5);
676 assert_approx_eq!(f32::from_bits(0x44a72000), 1337.0);
677 assert_approx_eq!(f32::from_bits(0xc1640000), -14.25);
678
679 // Check that NaNs roundtrip their bits regardless of signaling-ness
680 // 0xA is 0b1010; 0x5 is 0b0101 -- so these two together clobbers all the mantissa bits
681 let masked_nan1 = f32::NAN.to_bits() ^ 0x002A_AAAA;
682 let masked_nan2 = f32::NAN.to_bits() ^ 0x0055_5555;
683 assert!(f32::from_bits(masked_nan1).is_nan());
684 assert!(f32::from_bits(masked_nan2).is_nan());
685
686 assert_eq!(f32::from_bits(masked_nan1).to_bits(), masked_nan1);
687 assert_eq!(f32::from_bits(masked_nan2).to_bits(), masked_nan2);
688}
689
690#[test]
691#[should_panic]
692fn test_clamp_min_greater_than_max() {
693 let _ = 1.0f32.clamp(3.0, 1.0);
694}
695
696#[test]
697#[should_panic]
698fn test_clamp_min_is_nan() {
699 let _ = 1.0f32.clamp(f32::NAN, 1.0);
700}
701
702#[test]
703#[should_panic]
704fn test_clamp_max_is_nan() {
705 let _ = 1.0f32.clamp(3.0, f32::NAN);
706}
707
708#[test]
709fn test_total_cmp() {
710 use core::cmp::Ordering;
711
712 fn quiet_bit_mask() -> u32 {
713 1 << (f32::MANTISSA_DIGITS - 2)
714 }
715
716 fn min_subnorm() -> f32 {
717 f32::MIN_POSITIVE / f32::powf(2.0, f32::MANTISSA_DIGITS as f32 - 1.0)
718 }
719
720 fn max_subnorm() -> f32 {
721 f32::MIN_POSITIVE - min_subnorm()
722 }
723
724 fn q_nan() -> f32 {
725 f32::from_bits(f32::NAN.to_bits() | quiet_bit_mask())
726 }
727
728 fn s_nan() -> f32 {
729 f32::from_bits((f32::NAN.to_bits() & !quiet_bit_mask()) + 42)
730 }
731
732 assert_eq!(Ordering::Equal, (-q_nan()).total_cmp(&-q_nan()));
733 assert_eq!(Ordering::Equal, (-s_nan()).total_cmp(&-s_nan()));
734 assert_eq!(Ordering::Equal, (-f32::INFINITY).total_cmp(&-f32::INFINITY));
735 assert_eq!(Ordering::Equal, (-f32::MAX).total_cmp(&-f32::MAX));
736 assert_eq!(Ordering::Equal, (-2.5_f32).total_cmp(&-2.5));
737 assert_eq!(Ordering::Equal, (-1.0_f32).total_cmp(&-1.0));
738 assert_eq!(Ordering::Equal, (-1.5_f32).total_cmp(&-1.5));
739 assert_eq!(Ordering::Equal, (-0.5_f32).total_cmp(&-0.5));
740 assert_eq!(Ordering::Equal, (-f32::MIN_POSITIVE).total_cmp(&-f32::MIN_POSITIVE));
741 assert_eq!(Ordering::Equal, (-max_subnorm()).total_cmp(&-max_subnorm()));
742 assert_eq!(Ordering::Equal, (-min_subnorm()).total_cmp(&-min_subnorm()));
743 assert_eq!(Ordering::Equal, (-0.0_f32).total_cmp(&-0.0));
744 assert_eq!(Ordering::Equal, 0.0_f32.total_cmp(&0.0));
745 assert_eq!(Ordering::Equal, min_subnorm().total_cmp(&min_subnorm()));
746 assert_eq!(Ordering::Equal, max_subnorm().total_cmp(&max_subnorm()));
747 assert_eq!(Ordering::Equal, f32::MIN_POSITIVE.total_cmp(&f32::MIN_POSITIVE));
748 assert_eq!(Ordering::Equal, 0.5_f32.total_cmp(&0.5));
749 assert_eq!(Ordering::Equal, 1.0_f32.total_cmp(&1.0));
750 assert_eq!(Ordering::Equal, 1.5_f32.total_cmp(&1.5));
751 assert_eq!(Ordering::Equal, 2.5_f32.total_cmp(&2.5));
752 assert_eq!(Ordering::Equal, f32::MAX.total_cmp(&f32::MAX));
753 assert_eq!(Ordering::Equal, f32::INFINITY.total_cmp(&f32::INFINITY));
754 assert_eq!(Ordering::Equal, s_nan().total_cmp(&s_nan()));
755 assert_eq!(Ordering::Equal, q_nan().total_cmp(&q_nan()));
756
757 assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-s_nan()));
758 assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&-f32::INFINITY));
759 assert_eq!(Ordering::Less, (-f32::INFINITY).total_cmp(&-f32::MAX));
760 assert_eq!(Ordering::Less, (-f32::MAX).total_cmp(&-2.5));
761 assert_eq!(Ordering::Less, (-2.5_f32).total_cmp(&-1.5));
762 assert_eq!(Ordering::Less, (-1.5_f32).total_cmp(&-1.0));
763 assert_eq!(Ordering::Less, (-1.0_f32).total_cmp(&-0.5));
764 assert_eq!(Ordering::Less, (-0.5_f32).total_cmp(&-f32::MIN_POSITIVE));
765 assert_eq!(Ordering::Less, (-f32::MIN_POSITIVE).total_cmp(&-max_subnorm()));
766 assert_eq!(Ordering::Less, (-max_subnorm()).total_cmp(&-min_subnorm()));
767 assert_eq!(Ordering::Less, (-min_subnorm()).total_cmp(&-0.0));
768 assert_eq!(Ordering::Less, (-0.0_f32).total_cmp(&0.0));
769 assert_eq!(Ordering::Less, 0.0_f32.total_cmp(&min_subnorm()));
770 assert_eq!(Ordering::Less, min_subnorm().total_cmp(&max_subnorm()));
771 assert_eq!(Ordering::Less, max_subnorm().total_cmp(&f32::MIN_POSITIVE));
772 assert_eq!(Ordering::Less, f32::MIN_POSITIVE.total_cmp(&0.5));
773 assert_eq!(Ordering::Less, 0.5_f32.total_cmp(&1.0));
774 assert_eq!(Ordering::Less, 1.0_f32.total_cmp(&1.5));
775 assert_eq!(Ordering::Less, 1.5_f32.total_cmp(&2.5));
776 assert_eq!(Ordering::Less, 2.5_f32.total_cmp(&f32::MAX));
777 assert_eq!(Ordering::Less, f32::MAX.total_cmp(&f32::INFINITY));
778 assert_eq!(Ordering::Less, f32::INFINITY.total_cmp(&s_nan()));
779 assert_eq!(Ordering::Less, s_nan().total_cmp(&q_nan()));
780
781 assert_eq!(Ordering::Greater, (-s_nan()).total_cmp(&-q_nan()));
782 assert_eq!(Ordering::Greater, (-f32::INFINITY).total_cmp(&-s_nan()));
783 assert_eq!(Ordering::Greater, (-f32::MAX).total_cmp(&-f32::INFINITY));
784 assert_eq!(Ordering::Greater, (-2.5_f32).total_cmp(&-f32::MAX));
785 assert_eq!(Ordering::Greater, (-1.5_f32).total_cmp(&-2.5));
786 assert_eq!(Ordering::Greater, (-1.0_f32).total_cmp(&-1.5));
787 assert_eq!(Ordering::Greater, (-0.5_f32).total_cmp(&-1.0));
788 assert_eq!(Ordering::Greater, (-f32::MIN_POSITIVE).total_cmp(&-0.5));
789 assert_eq!(Ordering::Greater, (-max_subnorm()).total_cmp(&-f32::MIN_POSITIVE));
790 assert_eq!(Ordering::Greater, (-min_subnorm()).total_cmp(&-max_subnorm()));
791 assert_eq!(Ordering::Greater, (-0.0_f32).total_cmp(&-min_subnorm()));
792 assert_eq!(Ordering::Greater, 0.0_f32.total_cmp(&-0.0));
793 assert_eq!(Ordering::Greater, min_subnorm().total_cmp(&0.0));
794 assert_eq!(Ordering::Greater, max_subnorm().total_cmp(&min_subnorm()));
795 assert_eq!(Ordering::Greater, f32::MIN_POSITIVE.total_cmp(&max_subnorm()));
796 assert_eq!(Ordering::Greater, 0.5_f32.total_cmp(&f32::MIN_POSITIVE));
797 assert_eq!(Ordering::Greater, 1.0_f32.total_cmp(&0.5));
798 assert_eq!(Ordering::Greater, 1.5_f32.total_cmp(&1.0));
799 assert_eq!(Ordering::Greater, 2.5_f32.total_cmp(&1.5));
800 assert_eq!(Ordering::Greater, f32::MAX.total_cmp(&2.5));
801 assert_eq!(Ordering::Greater, f32::INFINITY.total_cmp(&f32::MAX));
802 assert_eq!(Ordering::Greater, s_nan().total_cmp(&f32::INFINITY));
803 assert_eq!(Ordering::Greater, q_nan().total_cmp(&s_nan()));
804
805 assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-s_nan()));
806 assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-f32::INFINITY));
807 assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-f32::MAX));
808 assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-2.5));
809 assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-1.5));
810 assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-1.0));
811 assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-0.5));
812 assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-f32::MIN_POSITIVE));
813 assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-max_subnorm()));
814 assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-min_subnorm()));
815 assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&-0.0));
816 assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&0.0));
817 assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&min_subnorm()));
818 assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&max_subnorm()));
819 assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&f32::MIN_POSITIVE));
820 assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&0.5));
821 assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&1.0));
822 assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&1.5));
823 assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&2.5));
824 assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&f32::MAX));
825 assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&f32::INFINITY));
826 assert_eq!(Ordering::Less, (-q_nan()).total_cmp(&s_nan()));
827
828 assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&-f32::INFINITY));
829 assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&-f32::MAX));
830 assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&-2.5));
831 assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&-1.5));
832 assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&-1.0));
833 assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&-0.5));
834 assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&-f32::MIN_POSITIVE));
835 assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&-max_subnorm()));
836 assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&-min_subnorm()));
837 assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&-0.0));
838 assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&0.0));
839 assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&min_subnorm()));
840 assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&max_subnorm()));
841 assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&f32::MIN_POSITIVE));
842 assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&0.5));
843 assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&1.0));
844 assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&1.5));
845 assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&2.5));
846 assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&f32::MAX));
847 assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&f32::INFINITY));
848 assert_eq!(Ordering::Less, (-s_nan()).total_cmp(&s_nan()));
849}