math_test.cpp revision 6a8f00d49c30107903f2449ae563cfee51b48468
1/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define _DECLARE_C99_LDBL_MATH 1
18
19#include <gtest/gtest.h>
20
21#include <fenv.h>
22#include <limits.h>
23#include <math.h>
24#include <stdint.h>
25
26float float_subnormal() {
27  union {
28    float f;
29    uint32_t i;
30  } u;
31  u.i = 0x007fffff;
32  return u.f;
33}
34
35double double_subnormal() {
36  union {
37    double d;
38    uint64_t i;
39  } u;
40  u.i = 0x000fffffffffffffLL;
41  return u.d;
42}
43
44TEST(math, fpclassify) {
45  ASSERT_EQ(FP_INFINITE, fpclassify(INFINITY));
46  ASSERT_EQ(FP_INFINITE, fpclassify(HUGE_VALF));
47  ASSERT_EQ(FP_INFINITE, fpclassify(HUGE_VAL));
48
49  ASSERT_EQ(FP_NAN, fpclassify(nanf("")));
50  ASSERT_EQ(FP_NAN, fpclassify(nan("")));
51
52  ASSERT_EQ(FP_NORMAL, fpclassify(1.0f));
53  ASSERT_EQ(FP_NORMAL, fpclassify(1.0));
54
55  ASSERT_EQ(FP_SUBNORMAL, fpclassify(float_subnormal()));
56  ASSERT_EQ(FP_SUBNORMAL, fpclassify(double_subnormal()));
57
58  ASSERT_EQ(FP_ZERO, fpclassify(0.0f));
59  ASSERT_EQ(FP_ZERO, fpclassify(0.0));
60}
61
62/* TODO: stlport breaks the isfinite macro
63TEST(math, isfinite) {
64  ASSERT_TRUE(isfinite(123.0f));
65  ASSERT_TRUE(isfinite(123.0));
66  ASSERT_FALSE(isfinite(HUGE_VALF));
67  ASSERT_FALSE(isfinite(HUGE_VAL));
68}
69*/
70
71TEST(math, isinf) {
72  ASSERT_FALSE(isinf(123.0f));
73  ASSERT_FALSE(isinf(123.0));
74  ASSERT_TRUE(isinf(HUGE_VALF));
75  ASSERT_TRUE(isinf(HUGE_VAL));
76}
77
78TEST(math, isnan) {
79  ASSERT_FALSE(isnan(123.0f));
80  ASSERT_FALSE(isnan(123.0));
81  ASSERT_TRUE(isnan(nanf("")));
82  ASSERT_TRUE(isnan(nan("")));
83}
84
85TEST(math, isnormal) {
86  ASSERT_TRUE(isnormal(123.0f));
87  ASSERT_TRUE(isnormal(123.0));
88  ASSERT_FALSE(isnormal(float_subnormal()));
89  ASSERT_FALSE(isnormal(double_subnormal()));
90}
91
92// TODO: isgreater, isgreaterequals, isless, islessequal, islessgreater, isunordered
93
94/* TODO: stlport breaks the signbit macro
95TEST(math, signbit) {
96  ASSERT_EQ(0, signbit(0.0f));
97  ASSERT_EQ(0, signbit(0.0));
98
99  ASSERT_EQ(0, signbit(1.0f));
100  ASSERT_EQ(0, signbit(1.0));
101
102  ASSERT_NE(0, signbit(-1.0f));
103  ASSERT_NE(0, signbit(-1.0));
104}
105*/
106
107#if defined(__BIONIC__)
108TEST(math, __fpclassifyd) {
109  ASSERT_EQ(FP_INFINITE, __fpclassifyd(HUGE_VAL));
110  ASSERT_EQ(FP_NAN, __fpclassifyd(nan("")));
111  ASSERT_EQ(FP_NORMAL, __fpclassifyd(1.0));
112  ASSERT_EQ(FP_SUBNORMAL, __fpclassifyd(double_subnormal()));
113  ASSERT_EQ(FP_ZERO, __fpclassifyd(0.0));
114}
115#endif
116
117#if defined(__BIONIC__)
118TEST(math, __fpclassifyf) {
119  ASSERT_EQ(FP_INFINITE, __fpclassifyf(HUGE_VALF));
120  ASSERT_EQ(FP_NAN, __fpclassifyf(nanf("")));
121  ASSERT_EQ(FP_NORMAL, __fpclassifyf(1.0f));
122  ASSERT_EQ(FP_SUBNORMAL, __fpclassifyf(float_subnormal()));
123  ASSERT_EQ(FP_ZERO, __fpclassifyf(0.0f));
124}
125#endif
126
127#if defined(__BIONIC__)
128TEST(math, __fpclassifyl) {
129  EXPECT_EQ(FP_INFINITE, __fpclassifyl(HUGE_VALL));
130  EXPECT_EQ(FP_NAN, __fpclassifyl(nanl("")));
131  EXPECT_EQ(FP_NORMAL, __fpclassifyl(1.0));
132  EXPECT_EQ(FP_SUBNORMAL, __fpclassifyl(double_subnormal()));
133  EXPECT_EQ(FP_ZERO, __fpclassifyl(0.0));
134}
135#endif
136
137TEST(math, finitef) {
138  ASSERT_TRUE(finitef(123.0f));
139  ASSERT_FALSE(finitef(HUGE_VALF));
140}
141
142#if defined(__BIONIC__)
143TEST(math, __isfinite) {
144  ASSERT_TRUE(__isfinite(123.0));
145  ASSERT_FALSE(__isfinite(HUGE_VAL));
146}
147#endif
148
149#if defined(__BIONIC__)
150TEST(math, __isfinitef) {
151  ASSERT_TRUE(__isfinitef(123.0f));
152  ASSERT_FALSE(__isfinitef(HUGE_VALF));
153}
154#endif
155
156#if defined(__BIONIC__)
157TEST(math, __isfinitel) {
158  ASSERT_TRUE(__isfinitel(123.0f));
159  ASSERT_FALSE(__isfinitel(HUGE_VALL));
160}
161#endif
162
163TEST(math, finite) {
164  ASSERT_TRUE(finite(123.0));
165  ASSERT_FALSE(finite(HUGE_VAL));
166}
167
168TEST(math, __isinff) {
169  ASSERT_FALSE(__isinff(123.0f));
170  ASSERT_TRUE(__isinff(HUGE_VALF));
171}
172
173TEST(math, __isinfl) {
174  ASSERT_FALSE(__isinfl(123.0));
175  ASSERT_TRUE(__isinfl(HUGE_VALL));
176}
177
178TEST(math, __isnanf) {
179  ASSERT_FALSE(__isnanf(123.0f));
180  ASSERT_TRUE(__isnanf(nanf("")));
181}
182
183TEST(math, __isnanl) {
184  ASSERT_FALSE(__isnanl(123.0));
185  ASSERT_TRUE(__isnanl(nanl("")));
186}
187
188TEST(math, isnanf) {
189  ASSERT_FALSE(isnanf(123.0f));
190  ASSERT_TRUE(isnanf(nanf("")));
191}
192
193#if defined(__BIONIC__)
194TEST(math, __isnormal) {
195  ASSERT_TRUE(__isnormal(123.0));
196  ASSERT_FALSE(__isnormal(double_subnormal()));
197}
198#endif
199
200#if defined(__BIONIC__)
201TEST(math, __isnormalf) {
202  ASSERT_TRUE(__isnormalf(123.0f));
203  ASSERT_FALSE(__isnormalf(float_subnormal()));
204}
205#endif
206
207#if defined(__BIONIC__)
208TEST(math, __isnormall) {
209  ASSERT_TRUE(__isnormall(123.0));
210  ASSERT_FALSE(__isnormall(double_subnormal()));
211}
212#endif
213
214TEST(math, __signbit) {
215  ASSERT_EQ(0, __signbit(0.0));
216  ASSERT_EQ(0, __signbit(1.0));
217  ASSERT_NE(0, __signbit(-1.0));
218}
219
220TEST(math, __signbitf) {
221  ASSERT_EQ(0, __signbitf(0.0f));
222  ASSERT_EQ(0, __signbitf(1.0f));
223  ASSERT_NE(0, __signbitf(-1.0f));
224}
225
226TEST(math, __signbitl) {
227  ASSERT_EQ(0, __signbitl(0.0));
228  ASSERT_EQ(0, __signbitl(1.0));
229  ASSERT_NE(0, __signbitl(-1.0));
230}
231
232TEST(math, acos) {
233  ASSERT_FLOAT_EQ(M_PI/2.0, acos(0.0));
234}
235
236TEST(math, acosf) {
237  ASSERT_FLOAT_EQ(static_cast<float>(M_PI)/2.0f, acosf(0.0f));
238}
239
240TEST(math, acosl) {
241  ASSERT_FLOAT_EQ(M_PI/2.0, acosl(0.0));
242}
243
244TEST(math, asin) {
245  ASSERT_FLOAT_EQ(0.0, asin(0.0));
246}
247
248TEST(math, asinf) {
249  ASSERT_FLOAT_EQ(0.0f, asinf(0.0f));
250}
251
252TEST(math, asinl) {
253  ASSERT_FLOAT_EQ(0.0, asinl(0.0));
254}
255
256TEST(math, atan) {
257  ASSERT_FLOAT_EQ(0.0, atan(0.0));
258}
259
260TEST(math, atanf) {
261  ASSERT_FLOAT_EQ(0.0f, atanf(0.0f));
262}
263
264TEST(math, atanl) {
265  ASSERT_FLOAT_EQ(0.0, atanl(0.0));
266}
267
268TEST(math, atan2) {
269  ASSERT_FLOAT_EQ(0.0, atan2(0.0, 0.0));
270}
271
272TEST(math, atan2f) {
273  ASSERT_FLOAT_EQ(0.0f, atan2f(0.0f, 0.0f));
274}
275
276TEST(math, atan2l) {
277  ASSERT_FLOAT_EQ(0.0, atan2l(0.0, 0.0));
278}
279
280TEST(math, cos) {
281  ASSERT_FLOAT_EQ(1.0, cos(0.0));
282}
283
284TEST(math, cosf) {
285  ASSERT_FLOAT_EQ(1.0f, cosf(0.0f));
286}
287
288TEST(math, cosl) {
289  ASSERT_FLOAT_EQ(1.0, cosl(0.0));
290}
291
292TEST(math, sin) {
293  ASSERT_FLOAT_EQ(0.0, sin(0.0));
294}
295
296TEST(math, sinf) {
297  ASSERT_FLOAT_EQ(0.0f, sinf(0.0f));
298}
299
300TEST(math, sinl) {
301  ASSERT_FLOAT_EQ(0.0, sinl(0.0));
302}
303
304TEST(math, tan) {
305  ASSERT_FLOAT_EQ(0.0, tan(0.0));
306}
307
308TEST(math, tanf) {
309  ASSERT_FLOAT_EQ(0.0f, tanf(0.0f));
310}
311
312TEST(math, tanl) {
313  ASSERT_FLOAT_EQ(0.0, tanl(0.0));
314}
315
316TEST(math, acosh) {
317  ASSERT_FLOAT_EQ(0.0, acosh(1.0));
318}
319
320TEST(math, acoshf) {
321  ASSERT_FLOAT_EQ(0.0f, acoshf(1.0f));
322}
323
324TEST(math, acoshl) {
325  ASSERT_FLOAT_EQ(0.0, acoshl(1.0));
326}
327
328TEST(math, asinh) {
329  ASSERT_FLOAT_EQ(0.0, asinh(0.0));
330}
331
332TEST(math, asinhf) {
333  ASSERT_FLOAT_EQ(0.0f, asinhf(0.0f));
334}
335
336TEST(math, asinhl) {
337  ASSERT_FLOAT_EQ(0.0, asinhl(0.0));
338}
339
340TEST(math, atanh) {
341  ASSERT_FLOAT_EQ(0.0, atanh(0.0));
342}
343
344TEST(math, atanhf) {
345  ASSERT_FLOAT_EQ(0.0f, atanhf(0.0f));
346}
347
348TEST(math, atanhl) {
349  ASSERT_FLOAT_EQ(0.0, atanhl(0.0));
350}
351
352TEST(math, cosh) {
353  ASSERT_FLOAT_EQ(1.0, cosh(0.0));
354}
355
356TEST(math, coshf) {
357  ASSERT_FLOAT_EQ(1.0f, coshf(0.0f));
358}
359
360TEST(math, coshl) {
361  ASSERT_FLOAT_EQ(1.0, coshl(0.0));
362}
363
364TEST(math, sinh) {
365  ASSERT_FLOAT_EQ(0.0, sinh(0.0));
366}
367
368TEST(math, sinhf) {
369  ASSERT_FLOAT_EQ(0.0f, sinhf(0.0f));
370}
371
372TEST(math, sinhl) {
373  ASSERT_FLOAT_EQ(0.0, sinhl(0.0));
374}
375
376TEST(math, tanh) {
377  ASSERT_FLOAT_EQ(0.0, tanh(0.0));
378}
379
380TEST(math, tanhf) {
381  ASSERT_FLOAT_EQ(0.0f, tanhf(0.0f));
382}
383
384TEST(math, tanhl) {
385  ASSERT_FLOAT_EQ(0.0, tanhl(0.0));
386}
387
388TEST(math, log) {
389  ASSERT_FLOAT_EQ(1.0, log(M_E));
390}
391
392TEST(math, logf) {
393  ASSERT_FLOAT_EQ(1.0f, logf(static_cast<float>(M_E)));
394}
395
396TEST(math, logl) {
397  ASSERT_FLOAT_EQ(1.0, logl(M_E));
398}
399
400TEST(math, log2) {
401  ASSERT_FLOAT_EQ(12.0, log2(4096.0));
402}
403
404TEST(math, log2f) {
405  ASSERT_FLOAT_EQ(12.0f, log2f(4096.0f));
406}
407
408TEST(math, log2l) {
409  ASSERT_FLOAT_EQ(12.0, log2l(4096.0));
410}
411
412TEST(math, log10) {
413  ASSERT_FLOAT_EQ(3.0, log10(1000.0));
414}
415
416TEST(math, log10f) {
417  ASSERT_FLOAT_EQ(3.0f, log10f(1000.0f));
418}
419
420TEST(math, log10l) {
421  ASSERT_FLOAT_EQ(3.0, log10l(1000.0));
422}
423
424TEST(math, cbrt) {
425  ASSERT_FLOAT_EQ(3.0, cbrt(27.0));
426}
427
428TEST(math, cbrtf) {
429  ASSERT_FLOAT_EQ(3.0f, cbrtf(27.0f));
430}
431
432TEST(math, cbrtl) {
433  ASSERT_FLOAT_EQ(3.0, cbrtl(27.0));
434}
435
436TEST(math, sqrt) {
437  ASSERT_FLOAT_EQ(2.0, sqrt(4.0));
438}
439
440TEST(math, sqrtf) {
441  ASSERT_FLOAT_EQ(2.0f, sqrtf(4.0f));
442}
443
444TEST(math, sqrtl) {
445  ASSERT_FLOAT_EQ(2.0, sqrtl(4.0));
446}
447
448TEST(math, exp) {
449  ASSERT_FLOAT_EQ(1.0, exp(0.0));
450  ASSERT_FLOAT_EQ(M_E, exp(1.0));
451}
452
453TEST(math, expf) {
454  ASSERT_FLOAT_EQ(1.0f, expf(0.0f));
455  ASSERT_FLOAT_EQ(static_cast<float>(M_E), expf(1.0f));
456}
457
458TEST(math, expl) {
459  ASSERT_FLOAT_EQ(1.0, expl(0.0));
460  ASSERT_FLOAT_EQ(M_E, expl(1.0));
461}
462
463TEST(math, exp2) {
464  ASSERT_FLOAT_EQ(8.0, exp2(3.0));
465}
466
467TEST(math, exp2f) {
468  ASSERT_FLOAT_EQ(8.0f, exp2f(3.0f));
469}
470
471TEST(math, exp2l) {
472  ASSERT_FLOAT_EQ(8.0, exp2l(3.0));
473}
474
475TEST(math, expm1) {
476  ASSERT_FLOAT_EQ(M_E - 1.0, expm1(1.0));
477}
478
479TEST(math, expm1f) {
480  ASSERT_FLOAT_EQ(static_cast<float>(M_E) - 1.0f, expm1f(1.0f));
481}
482
483TEST(math, expm1l) {
484  ASSERT_FLOAT_EQ(M_E - 1.0, expm1l(1.0));
485}
486
487TEST(math, pow) {
488  ASSERT_TRUE(isnan(pow(nan(""), 3.0)));
489  ASSERT_TRUE(isnan(pow(2.0, nan(""))));
490  ASSERT_FLOAT_EQ(8.0, pow(2.0, 3.0));
491}
492
493TEST(math, powf) {
494  ASSERT_TRUE(isnanf(powf(nanf(""), 3.0f)));
495  ASSERT_TRUE(isnanf(powf(2.0f, nanf(""))));
496  ASSERT_FLOAT_EQ(8.0f, powf(2.0f, 3.0f));
497}
498
499TEST(math, powl) {
500  ASSERT_TRUE(__isnanl(powl(nanl(""), 3.0)));
501  ASSERT_TRUE(__isnanl(powl(2.0, nanl(""))));
502  ASSERT_FLOAT_EQ(8.0, powl(2.0, 3.0));
503}
504
505TEST(math, ceil) {
506  ASSERT_FLOAT_EQ(1.0, ceil(0.9));
507}
508
509TEST(math, ceilf) {
510  ASSERT_FLOAT_EQ(1.0f, ceilf(0.9f));
511}
512
513TEST(math, ceill) {
514  ASSERT_FLOAT_EQ(1.0, ceill(0.9));
515}
516
517TEST(math, floor) {
518  ASSERT_FLOAT_EQ(1.0, floor(1.1));
519}
520
521TEST(math, floorf) {
522  ASSERT_FLOAT_EQ(1.0f, floorf(1.1f));
523}
524
525TEST(math, floorl) {
526  ASSERT_FLOAT_EQ(1.0, floorl(1.1));
527}
528
529TEST(math, fabs) {
530  ASSERT_FLOAT_EQ(1.0, fabs(-1.0));
531}
532
533TEST(math, fabsf) {
534  ASSERT_FLOAT_EQ(1.0f, fabsf(-1.0f));
535}
536
537TEST(math, fabsl) {
538  ASSERT_FLOAT_EQ(1.0, fabsl(-1.0));
539}
540
541TEST(math, ldexp) {
542  ASSERT_FLOAT_EQ(16.0, ldexp(2.0, 3.0));
543}
544
545TEST(math, ldexpf) {
546  ASSERT_FLOAT_EQ(16.0f, ldexpf(2.0f, 3.0f));
547}
548
549TEST(math, ldexpl) {
550  ASSERT_FLOAT_EQ(16.0, ldexpl(2.0, 3.0));
551}
552
553TEST(math, fmod) {
554  ASSERT_FLOAT_EQ(2.0, fmod(12.0, 10.0));
555}
556
557TEST(math, fmodf) {
558  ASSERT_FLOAT_EQ(2.0f, fmodf(12.0f, 10.0f));
559}
560
561TEST(math, fmodl) {
562  ASSERT_FLOAT_EQ(2.0, fmodl(12.0, 10.0));
563}
564
565TEST(math, remainder) {
566  ASSERT_FLOAT_EQ(2.0, remainder(12.0, 10.0));
567}
568
569TEST(math, remainderf) {
570  ASSERT_FLOAT_EQ(2.0f, remainderf(12.0f, 10.0f));
571}
572
573TEST(math, remainderl) {
574  ASSERT_FLOAT_EQ(2.0, remainderl(12.0, 10.0));
575}
576
577TEST(math, drem) {
578  ASSERT_FLOAT_EQ(2.0, drem(12.0, 10.0));
579}
580
581TEST(math, dremf) {
582  ASSERT_FLOAT_EQ(2.0f, dremf(12.0f, 10.0f));
583}
584
585TEST(math, fmax) {
586  ASSERT_FLOAT_EQ(12.0, fmax(12.0, 10.0));
587  ASSERT_FLOAT_EQ(12.0, fmax(12.0, nan("")));
588  ASSERT_FLOAT_EQ(12.0, fmax(nan(""), 12.0));
589}
590
591TEST(math, fmaxf) {
592  ASSERT_FLOAT_EQ(12.0f, fmaxf(12.0f, 10.0f));
593  ASSERT_FLOAT_EQ(12.0f, fmaxf(12.0f, nanf("")));
594  ASSERT_FLOAT_EQ(12.0f, fmaxf(nanf(""), 12.0f));
595}
596
597TEST(math, fmaxl) {
598  ASSERT_FLOAT_EQ(12.0, fmaxl(12.0, 10.0));
599  ASSERT_FLOAT_EQ(12.0, fmaxl(12.0, nanl("")));
600  ASSERT_FLOAT_EQ(12.0, fmaxl(nanl(""), 12.0));
601}
602
603TEST(math, fmin) {
604  ASSERT_FLOAT_EQ(10.0, fmin(12.0, 10.0));
605  ASSERT_FLOAT_EQ(12.0, fmin(12.0, nan("")));
606  ASSERT_FLOAT_EQ(12.0, fmin(nan(""), 12.0));
607}
608
609TEST(math, fminf) {
610  ASSERT_FLOAT_EQ(10.0f, fminf(12.0f, 10.0f));
611  ASSERT_FLOAT_EQ(12.0f, fminf(12.0f, nanf("")));
612  ASSERT_FLOAT_EQ(12.0f, fminf(nanf(""), 12.0f));
613}
614
615TEST(math, fminl) {
616  ASSERT_FLOAT_EQ(10.0, fminl(12.0, 10.0));
617  ASSERT_FLOAT_EQ(12.0, fminl(12.0, nan("")));
618  ASSERT_FLOAT_EQ(12.0, fminl(nan(""), 12.0));
619}
620
621TEST(math, fma) {
622  ASSERT_FLOAT_EQ(10.0, fma(2.0, 3.0, 4.0));
623}
624
625TEST(math, fmaf) {
626  ASSERT_FLOAT_EQ(10.0f, fmaf(2.0f, 3.0f, 4.0f));
627}
628
629TEST(math, fmal) {
630  ASSERT_FLOAT_EQ(10.0, fmal(2.0, 3.0, 4.0));
631}
632
633TEST(math, hypot) {
634  ASSERT_FLOAT_EQ(5.0, hypot(3.0, 4.0));
635}
636
637TEST(math, hypotf) {
638  ASSERT_FLOAT_EQ(5.0f, hypotf(3.0f, 4.0f));
639}
640
641TEST(math, hypotl) {
642  ASSERT_FLOAT_EQ(5.0, hypotl(3.0, 4.0));
643}
644
645TEST(math, erf) {
646  ASSERT_FLOAT_EQ(0.84270078, erf(1.0));
647}
648
649TEST(math, erff) {
650  ASSERT_FLOAT_EQ(0.84270078f, erff(1.0f));
651}
652
653TEST(math, erfl) {
654  ASSERT_FLOAT_EQ(0.84270078, erfl(1.0));
655}
656
657TEST(math, erfc) {
658  ASSERT_FLOAT_EQ(0.15729921, erfc(1.0));
659}
660
661TEST(math, erfcf) {
662  ASSERT_FLOAT_EQ(0.15729921f, erfcf(1.0f));
663}
664
665TEST(math, erfcl) {
666  ASSERT_FLOAT_EQ(0.15729921, erfcl(1.0));
667}
668
669TEST(math, lrint) {
670  fesetround(FE_UPWARD); // lrint/lrintf/lrintl obey the rounding mode.
671  ASSERT_EQ(1235, lrint(1234.01));
672  ASSERT_EQ(1235, lrintf(1234.01f));
673  ASSERT_EQ(1235, lrintl(1234.01));
674  fesetround(FE_TOWARDZERO); // lrint/lrintf/lrintl obey the rounding mode.
675  ASSERT_EQ(1234, lrint(1234.01));
676  ASSERT_EQ(1234, lrintf(1234.01f));
677  ASSERT_EQ(1234, lrintl(1234.01));
678
679  fesetround(FE_UPWARD); // llrint/llrintf/llrintl obey the rounding mode.
680  ASSERT_EQ(1235L, llrint(1234.01));
681  ASSERT_EQ(1235L, llrintf(1234.01f));
682  ASSERT_EQ(1235L, llrintl(1234.01));
683  fesetround(FE_TOWARDZERO); // llrint/llrintf/llrintl obey the rounding mode.
684  ASSERT_EQ(1234L, llrint(1234.01));
685  ASSERT_EQ(1234L, llrintf(1234.01f));
686  ASSERT_EQ(1234L, llrintl(1234.01));
687}
688
689TEST(math, rint) {
690  fesetround(FE_UPWARD); // rint/rintf/rintl obey the rounding mode.
691  feclearexcept(FE_ALL_EXCEPT); // rint/rintf/rintl do set the FE_INEXACT flag.
692  ASSERT_EQ(1234.0, rint(1234.0));
693  ASSERT_TRUE((fetestexcept(FE_ALL_EXCEPT) & FE_INEXACT) == 0);
694  ASSERT_EQ(1235.0, rint(1234.01));
695  ASSERT_TRUE((fetestexcept(FE_ALL_EXCEPT) & FE_INEXACT) != 0);
696
697  feclearexcept(FE_ALL_EXCEPT); // rint/rintf/rintl do set the FE_INEXACT flag.
698  ASSERT_EQ(1234.0f, rintf(1234.0f));
699  ASSERT_TRUE((fetestexcept(FE_ALL_EXCEPT) & FE_INEXACT) == 0);
700  ASSERT_EQ(1235.0f, rintf(1234.01f));
701  ASSERT_TRUE((fetestexcept(FE_ALL_EXCEPT) & FE_INEXACT) != 0);
702
703  feclearexcept(FE_ALL_EXCEPT); // rint/rintf/rintl do set the FE_INEXACT flag.
704  ASSERT_EQ(1234.0, rintl(1234.0));
705  ASSERT_TRUE((fetestexcept(FE_ALL_EXCEPT) & FE_INEXACT) == 0);
706  ASSERT_EQ(1235.0, rintl(1234.01));
707  ASSERT_TRUE((fetestexcept(FE_ALL_EXCEPT) & FE_INEXACT) != 0);
708
709  fesetround(FE_TOWARDZERO); // rint/rintf obey the rounding mode.
710  ASSERT_EQ(1234.0, rint(1234.01));
711  ASSERT_EQ(1234.0f, rintf(1234.01f));
712  ASSERT_EQ(1234.0, rintl(1234.01));
713}
714
715TEST(math, nearbyint) {
716  fesetround(FE_UPWARD); // nearbyint/nearbyintf/nearbyintl obey the rounding mode.
717  feclearexcept(FE_ALL_EXCEPT); // nearbyint/nearbyintf/nearbyintl don't set the FE_INEXACT flag.
718  ASSERT_EQ(1234.0, nearbyint(1234.0));
719  ASSERT_TRUE((fetestexcept(FE_ALL_EXCEPT) & FE_INEXACT) == 0);
720  ASSERT_EQ(1235.0, nearbyint(1234.01));
721  ASSERT_TRUE((fetestexcept(FE_ALL_EXCEPT) & FE_INEXACT) == 0);
722
723  feclearexcept(FE_ALL_EXCEPT);
724  ASSERT_EQ(1234.0f, nearbyintf(1234.0f));
725  ASSERT_TRUE((fetestexcept(FE_ALL_EXCEPT) & FE_INEXACT) == 0);
726  ASSERT_EQ(1235.0f, nearbyintf(1234.01f));
727  ASSERT_TRUE((fetestexcept(FE_ALL_EXCEPT) & FE_INEXACT) == 0);
728
729  feclearexcept(FE_ALL_EXCEPT); // nearbyint/nearbyintf/nearbyintl don't set the FE_INEXACT flag.
730  ASSERT_EQ(1234.0, nearbyintl(1234.0));
731  ASSERT_TRUE((fetestexcept(FE_ALL_EXCEPT) & FE_INEXACT) == 0);
732  ASSERT_EQ(1235.0, nearbyintl(1234.01));
733  ASSERT_TRUE((fetestexcept(FE_ALL_EXCEPT) & FE_INEXACT) == 0);
734
735  fesetround(FE_TOWARDZERO); // nearbyint/nearbyintf/nearbyintl obey the rounding mode.
736  ASSERT_EQ(1234.0, nearbyint(1234.01));
737  ASSERT_EQ(1234.0f, nearbyintf(1234.01f));
738  ASSERT_EQ(1234.0, nearbyintl(1234.01));
739}
740
741TEST(math, lround) {
742  fesetround(FE_UPWARD); // lround ignores the rounding mode.
743  ASSERT_EQ(1234, lround(1234.01));
744  ASSERT_EQ(1234, lroundf(1234.01f));
745  ASSERT_EQ(1234, lroundl(1234.01));
746}
747
748TEST(math, llround) {
749  fesetround(FE_UPWARD); // llround ignores the rounding mode.
750  ASSERT_EQ(1234L, llround(1234.01));
751  ASSERT_EQ(1234L, llroundf(1234.01f));
752  ASSERT_EQ(1234L, llroundl(1234.01));
753}
754
755TEST(math, ilogb) {
756  ASSERT_EQ(FP_ILOGB0, ilogb(0.0));
757  ASSERT_EQ(FP_ILOGBNAN, ilogb(nan("")));
758  ASSERT_EQ(INT_MAX, ilogb(HUGE_VAL));
759  ASSERT_EQ(0, ilogb(1.0));
760  ASSERT_EQ(3, ilogb(10.0));
761}
762
763TEST(math, ilogbf) {
764  ASSERT_EQ(FP_ILOGB0, ilogbf(0.0f));
765  ASSERT_EQ(FP_ILOGBNAN, ilogbf(nanf("")));
766  ASSERT_EQ(INT_MAX, ilogbf(HUGE_VALF));
767  ASSERT_EQ(0, ilogbf(1.0f));
768  ASSERT_EQ(3, ilogbf(10.0f));
769}
770
771TEST(math, ilogbl) {
772  ASSERT_EQ(FP_ILOGB0, ilogbl(0.0));
773  ASSERT_EQ(FP_ILOGBNAN, ilogbl(nanl("")));
774  ASSERT_EQ(INT_MAX, ilogbl(HUGE_VALL));
775  ASSERT_EQ(0, ilogbl(1.0));
776  ASSERT_EQ(3, ilogbl(10.0));
777}
778
779TEST(math, logb) {
780  ASSERT_EQ(-HUGE_VAL, logb(0.0));
781  ASSERT_TRUE(isnan(logb(nan(""))));
782  ASSERT_TRUE(isinf(logb(HUGE_VAL)));
783  ASSERT_EQ(0.0, logb(1.0));
784  ASSERT_EQ(3.0, logb(10.0));
785}
786
787TEST(math, logbf) {
788  ASSERT_EQ(-HUGE_VALF, logbf(0.0f));
789  ASSERT_TRUE(isnanf(logbf(nanf(""))));
790  ASSERT_TRUE(__isinff(logbf(HUGE_VALF)));
791  ASSERT_EQ(0.0f, logbf(1.0f));
792  ASSERT_EQ(3.0f, logbf(10.0f));
793}
794
795TEST(math, logbl) {
796  ASSERT_EQ(-HUGE_VAL, logbl(0.0));
797  ASSERT_TRUE(isnan(logbl(nanl(""))));
798  ASSERT_TRUE(isinf(logbl(HUGE_VALL)));
799  ASSERT_EQ(0.0, logbl(1.0));
800  ASSERT_EQ(3.0, logbl(10.0));
801}
802
803TEST(math, log1p) {
804  ASSERT_EQ(-HUGE_VAL, log1p(-1.0));
805  ASSERT_TRUE(isnan(log1p(nan(""))));
806  ASSERT_TRUE(isinf(log1p(HUGE_VAL)));
807  ASSERT_FLOAT_EQ(1.0, log1p(M_E - 1.0));
808}
809
810TEST(math, log1pf) {
811  ASSERT_EQ(-HUGE_VALF, log1pf(-1.0f));
812  ASSERT_TRUE(isnanf(log1pf(nanf(""))));
813  ASSERT_TRUE(__isinff(log1pf(HUGE_VALF)));
814  ASSERT_FLOAT_EQ(1.0f, log1pf(static_cast<float>(M_E) - 1.0f));
815}
816
817TEST(math, log1pl) {
818  ASSERT_EQ(-HUGE_VALL, log1pl(-1.0));
819  ASSERT_TRUE(isnan(log1pl(nanl(""))));
820  ASSERT_TRUE(isinf(log1pl(HUGE_VALL)));
821  ASSERT_FLOAT_EQ(1.0, log1pl(M_E - 1.0));
822}
823
824TEST(math, fdim) {
825  ASSERT_FLOAT_EQ(0.0, fdim(1.0, 1.0));
826  ASSERT_FLOAT_EQ(1.0, fdim(2.0, 1.0));
827  ASSERT_FLOAT_EQ(0.0, fdim(1.0, 2.0));
828}
829
830TEST(math, fdimf) {
831  ASSERT_FLOAT_EQ(0.0f, fdimf(1.0f, 1.0f));
832  ASSERT_FLOAT_EQ(1.0f, fdimf(2.0f, 1.0f));
833  ASSERT_FLOAT_EQ(0.0f, fdimf(1.0f, 2.0f));
834}
835
836TEST(math, fdiml) {
837  ASSERT_FLOAT_EQ(0.0, fdiml(1.0, 1.0));
838  ASSERT_FLOAT_EQ(1.0, fdiml(2.0, 1.0));
839  ASSERT_FLOAT_EQ(0.0, fdiml(1.0, 2.0));
840}
841
842TEST(math, round) {
843  fesetround(FE_TOWARDZERO); // round ignores the rounding mode and always rounds away from zero.
844  ASSERT_FLOAT_EQ(1.0, round(0.5));
845  ASSERT_FLOAT_EQ(-1.0, round(-0.5));
846  ASSERT_FLOAT_EQ(0.0, round(0.0));
847  ASSERT_FLOAT_EQ(-0.0, round(-0.0));
848  ASSERT_TRUE(isnan(round(nan(""))));
849  ASSERT_FLOAT_EQ(HUGE_VAL, round(HUGE_VAL));
850}
851
852TEST(math, roundf) {
853  fesetround(FE_TOWARDZERO); // roundf ignores the rounding mode and always rounds away from zero.
854  ASSERT_FLOAT_EQ(1.0f, roundf(0.5f));
855  ASSERT_FLOAT_EQ(-1.0f, roundf(-0.5f));
856  ASSERT_FLOAT_EQ(0.0f, roundf(0.0f));
857  ASSERT_FLOAT_EQ(-0.0f, roundf(-0.0f));
858  ASSERT_TRUE(isnanf(roundf(nanf(""))));
859  ASSERT_FLOAT_EQ(HUGE_VALF, roundf(HUGE_VALF));
860}
861
862TEST(math, roundl) {
863  fesetround(FE_TOWARDZERO); // roundl ignores the rounding mode and always rounds away from zero.
864  ASSERT_FLOAT_EQ(1.0, roundl(0.5));
865  ASSERT_FLOAT_EQ(-1.0, roundl(-0.5));
866  ASSERT_FLOAT_EQ(0.0, roundl(0.0));
867  ASSERT_FLOAT_EQ(-0.0, roundl(-0.0));
868  ASSERT_TRUE(isnan(roundl(nanl(""))));
869  ASSERT_FLOAT_EQ(HUGE_VALL, roundl(HUGE_VALL));
870}
871
872TEST(math, trunc) {
873  fesetround(FE_UPWARD); // trunc ignores the rounding mode and always rounds toward zero.
874  ASSERT_FLOAT_EQ(1.0, trunc(1.5));
875  ASSERT_FLOAT_EQ(-1.0, trunc(-1.5));
876  ASSERT_FLOAT_EQ(0.0, trunc(0.0));
877  ASSERT_FLOAT_EQ(-0.0, trunc(-0.0));
878  ASSERT_TRUE(isnan(trunc(nan(""))));
879  ASSERT_FLOAT_EQ(HUGE_VAL, trunc(HUGE_VAL));
880}
881
882TEST(math, truncf) {
883  fesetround(FE_UPWARD); // truncf ignores the rounding mode and always rounds toward zero.
884  ASSERT_FLOAT_EQ(1.0f, truncf(1.5f));
885  ASSERT_FLOAT_EQ(-1.0f, truncf(-1.5f));
886  ASSERT_FLOAT_EQ(0.0f, truncf(0.0f));
887  ASSERT_FLOAT_EQ(-0.0f, truncf(-0.0f));
888  ASSERT_TRUE(isnan(truncf(nanf(""))));
889  ASSERT_FLOAT_EQ(HUGE_VALF, truncf(HUGE_VALF));
890}
891
892TEST(math, truncl) {
893  fesetround(FE_UPWARD); // truncl ignores the rounding mode and always rounds toward zero.
894  ASSERT_FLOAT_EQ(1.0, truncl(1.5));
895  ASSERT_FLOAT_EQ(-1.0, truncl(-1.5));
896  ASSERT_FLOAT_EQ(0.0, truncl(0.0));
897  ASSERT_FLOAT_EQ(-0.0, truncl(-0.0));
898  ASSERT_TRUE(isnan(truncl(nan(""))));
899  ASSERT_FLOAT_EQ(HUGE_VALL, truncl(HUGE_VALL));
900}
901
902TEST(math, nextafter) {
903  ASSERT_FLOAT_EQ(0.0, nextafter(0.0, 0.0));
904  ASSERT_FLOAT_EQ(1.4012985e-45, nextafter(0.0, 1.0));
905  ASSERT_FLOAT_EQ(0.0, nextafter(0.0, -1.0));
906}
907
908TEST(math, nextafterf) {
909  ASSERT_FLOAT_EQ(0.0f, nextafterf(0.0f, 0.0f));
910  ASSERT_FLOAT_EQ(1.4012985e-45f, nextafterf(0.0f, 1.0f));
911  ASSERT_FLOAT_EQ(0.0f, nextafterf(0.0f, -1.0f));
912}
913
914TEST(math, nextafterl) {
915  ASSERT_FLOAT_EQ(0.0, nextafterl(0.0, 0.0));
916  ASSERT_FLOAT_EQ(1.4012985e-45, nextafterl(0.0, 1.0));
917  ASSERT_FLOAT_EQ(0.0, nextafterl(0.0, -1.0));
918}
919
920// TODO: nexttoward
921// TODO: nexttowardf
922// TODO: nexttowardl
923
924TEST(math, copysign) {
925  ASSERT_FLOAT_EQ(0.0, copysign(0.0, 1.0));
926  ASSERT_FLOAT_EQ(-0.0, copysign(0.0, -1.0));
927  ASSERT_FLOAT_EQ(2.0, copysign(2.0, 1.0));
928  ASSERT_FLOAT_EQ(-2.0, copysign(2.0, -1.0));
929}
930
931TEST(math, copysignf) {
932  ASSERT_FLOAT_EQ(0.0f, copysignf(0.0f, 1.0f));
933  ASSERT_FLOAT_EQ(-0.0f, copysignf(0.0f, -1.0f));
934  ASSERT_FLOAT_EQ(2.0f, copysignf(2.0f, 1.0f));
935  ASSERT_FLOAT_EQ(-2.0f, copysignf(2.0f, -1.0f));
936}
937
938TEST(math, copysignl) {
939  ASSERT_FLOAT_EQ(0.0f, copysignl(0.0, 1.0));
940  ASSERT_FLOAT_EQ(-0.0f, copysignl(0.0, -1.0));
941  ASSERT_FLOAT_EQ(2.0f, copysignl(2.0, 1.0));
942  ASSERT_FLOAT_EQ(-2.0f, copysignl(2.0, -1.0));
943}
944
945TEST(math, significand) {
946  ASSERT_FLOAT_EQ(0.0, significand(0.0));
947  ASSERT_FLOAT_EQ(1.2, significand(1.2));
948  ASSERT_FLOAT_EQ(1.5375, significand(12.3));
949}
950
951TEST(math, significandf) {
952  ASSERT_FLOAT_EQ(0.0f, significandf(0.0f));
953  ASSERT_FLOAT_EQ(1.2f, significandf(1.2f));
954  ASSERT_FLOAT_EQ(1.5375f, significandf(12.3f));
955}
956
957extern "C" long double significandl(long double); // BSD's <math.h> doesn't declare this.
958
959TEST(math, significandl) {
960  ASSERT_FLOAT_EQ(0.0, significandl(0.0));
961  ASSERT_FLOAT_EQ(1.2, significandl(1.2));
962  ASSERT_FLOAT_EQ(1.5375, significandl(12.3));
963}
964
965TEST(math, scalb) {
966  ASSERT_FLOAT_EQ(12.0, scalb(3.0, 2.0));
967}
968
969TEST(math, scalbf) {
970  ASSERT_FLOAT_EQ(12.0f, scalbf(3.0f, 2.0f));
971}
972
973TEST(math, scalbln) {
974  ASSERT_FLOAT_EQ(12.0, scalbln(3.0, 2L));
975}
976
977TEST(math, scalblnf) {
978  ASSERT_FLOAT_EQ(12.0f, scalblnf(3.0f, 2L));
979}
980
981TEST(math, scalblnl) {
982  ASSERT_FLOAT_EQ(12.0, scalblnl(3.0, 2L));
983}
984
985TEST(math, scalbn) {
986  ASSERT_FLOAT_EQ(12.0, scalbn(3.0, 2));
987}
988
989TEST(math, scalbnf) {
990  ASSERT_FLOAT_EQ(12.0f, scalbnf(3.0f, 2));
991}
992
993TEST(math, scalbnl) {
994  ASSERT_FLOAT_EQ(12.0, scalbnl(3.0, 2));
995}
996
997TEST(math, gamma) {
998  ASSERT_FLOAT_EQ(log(24.0), gamma(5.0));
999}
1000
1001TEST(math, gammaf) {
1002  ASSERT_FLOAT_EQ(logf(24.0f), gammaf(5.0f));
1003}
1004
1005#if defined(__BIONIC__)
1006TEST(math, gamma_r) {
1007  int sign;
1008  ASSERT_FLOAT_EQ(log(24.0), gamma_r(5.0, &sign));
1009  ASSERT_EQ(1, sign);
1010}
1011#endif
1012
1013#if defined(__BIONIC__)
1014TEST(math, gammaf_r) {
1015  int sign;
1016  ASSERT_FLOAT_EQ(logf(24.0f), gammaf_r(5.0f, &sign));
1017  ASSERT_EQ(1, sign);
1018}
1019#endif
1020
1021TEST(math, lgamma) {
1022  ASSERT_FLOAT_EQ(log(24.0), lgamma(5.0));
1023}
1024
1025TEST(math, lgammaf) {
1026  ASSERT_FLOAT_EQ(logf(24.0f), lgammaf(5.0f));
1027}
1028
1029TEST(math, lgammal) {
1030  ASSERT_FLOAT_EQ(logl(24.0), lgammal(5.0));
1031}
1032
1033TEST(math, lgamma_r) {
1034  int sign;
1035  ASSERT_FLOAT_EQ(log(24.0), lgamma_r(5.0, &sign));
1036  ASSERT_EQ(1, sign);
1037}
1038
1039TEST(math, lgammaf_r) {
1040  int sign;
1041  ASSERT_FLOAT_EQ(logf(24.0f), lgammaf_r(5.0f, &sign));
1042  ASSERT_EQ(1, sign);
1043}
1044
1045TEST(math, tgamma) {
1046  ASSERT_FLOAT_EQ(24.0, tgamma(5.0));
1047}
1048
1049TEST(math, tgammaf) {
1050  ASSERT_FLOAT_EQ(24.0f, tgammaf(5.0f));
1051}
1052
1053TEST(math, tgammal) {
1054  ASSERT_FLOAT_EQ(24.0, tgammal(5.0));
1055}
1056
1057TEST(math, j0) {
1058  ASSERT_FLOAT_EQ(1.0, j0(0.0));
1059  ASSERT_FLOAT_EQ(0.76519769, j0(1.0));
1060}
1061
1062TEST(math, j0f) {
1063  ASSERT_FLOAT_EQ(1.0f, j0f(0.0f));
1064  ASSERT_FLOAT_EQ(0.76519769f, j0f(1.0f));
1065}
1066
1067TEST(math, j1) {
1068  ASSERT_FLOAT_EQ(0.0, j1(0.0));
1069  ASSERT_FLOAT_EQ(0.44005057, j1(1.0));
1070}
1071
1072TEST(math, j1f) {
1073  ASSERT_FLOAT_EQ(0.0f, j1f(0.0f));
1074  ASSERT_FLOAT_EQ(0.44005057f, j1f(1.0f));
1075}
1076
1077TEST(math, jn) {
1078  ASSERT_FLOAT_EQ(0.0, jn(4, 0.0));
1079  ASSERT_FLOAT_EQ(0.0024766389, jn(4, 1.0));
1080}
1081
1082TEST(math, jnf) {
1083  ASSERT_FLOAT_EQ(0.0f, jnf(4, 0.0f));
1084  ASSERT_FLOAT_EQ(0.0024766389f, jnf(4, 1.0f));
1085}
1086
1087TEST(math, y0) {
1088  ASSERT_FLOAT_EQ(-HUGE_VAL, y0(0.0));
1089  ASSERT_FLOAT_EQ(0.088256963, y0(1.0));
1090}
1091
1092TEST(math, y0f) {
1093  ASSERT_FLOAT_EQ(-HUGE_VALF, y0f(0.0f));
1094  ASSERT_FLOAT_EQ(0.088256963f, y0f(1.0f));
1095}
1096
1097TEST(math, y1) {
1098  ASSERT_FLOAT_EQ(-HUGE_VAL, y1(0.0));
1099  ASSERT_FLOAT_EQ(-0.78121281, y1(1.0));
1100}
1101
1102TEST(math, y1f) {
1103  ASSERT_FLOAT_EQ(-HUGE_VALF, y1f(0.0f));
1104  ASSERT_FLOAT_EQ(-0.78121281f, y1f(1.0f));
1105}
1106
1107TEST(math, yn) {
1108  ASSERT_FLOAT_EQ(-HUGE_VAL, yn(4, 0.0));
1109  ASSERT_FLOAT_EQ(-33.278423, yn(4, 1.0));
1110}
1111
1112TEST(math, ynf) {
1113  ASSERT_FLOAT_EQ(-HUGE_VALF, ynf(4, 0.0f));
1114  ASSERT_FLOAT_EQ(-33.278423f, ynf(4, 1.0f));
1115}
1116
1117TEST(math, frexp) {
1118  int exp;
1119  double dr = frexp(1024.0, &exp);
1120  ASSERT_FLOAT_EQ(1024.0, scalbn(dr, exp));
1121}
1122
1123TEST(math, frexpf) {
1124  int exp;
1125  float fr = frexpf(1024.0f, &exp);
1126  ASSERT_FLOAT_EQ(1024.0f, scalbnf(fr, exp));
1127}
1128
1129TEST(math, frexpl) {
1130  int exp;
1131  long double ldr = frexpl(1024.0, &exp);
1132  ASSERT_FLOAT_EQ(1024.0, scalbnl(ldr, exp));
1133}
1134
1135TEST(math, modf) {
1136  double di;
1137  double df = modf(123.456, &di);
1138  ASSERT_FLOAT_EQ(123.0, di);
1139  ASSERT_FLOAT_EQ(0.456, df);
1140}
1141
1142TEST(math, modff) {
1143  float fi;
1144  float ff = modff(123.456f, &fi);
1145  ASSERT_FLOAT_EQ(123.0f, fi);
1146  ASSERT_FLOAT_EQ(0.45600128f, ff);
1147}
1148
1149TEST(math, modfl) {
1150  long double ldi;
1151  long double ldf = modfl(123.456, &ldi);
1152  ASSERT_FLOAT_EQ(123.0, ldi);
1153  ASSERT_FLOAT_EQ(0.456, ldf);
1154}
1155
1156TEST(math, remquo) {
1157  int q;
1158  double d = remquo(13.0, 4.0, &q);
1159  ASSERT_EQ(3, q);
1160  ASSERT_FLOAT_EQ(1.0, d);
1161}
1162
1163TEST(math, remquof) {
1164  int q;
1165  float f = remquof(13.0f, 4.0f, &q);
1166  ASSERT_EQ(3, q);
1167  ASSERT_FLOAT_EQ(1.0, f);
1168}
1169
1170TEST(math, remquol) {
1171  int q;
1172  long double ld = remquol(13.0, 4.0, &q);
1173  ASSERT_EQ(3, q);
1174  ASSERT_FLOAT_EQ(1.0, ld);
1175}
1176
1177// https://code.google.com/p/android/issues/detail?id=6697
1178TEST(math, frexpf_public_bug_6697) {
1179  int exp;
1180  float fr = frexpf(14.1f, &exp);
1181  ASSERT_FLOAT_EQ(14.1f, scalbnf(fr, exp));
1182}
1183