1 /* Test file for mpfr_log10. 2 3 Copyright 2001-2023 Free Software Foundation, Inc. 4 Contributed by the AriC and Caramba projects, INRIA. 5 6 This file is part of the GNU MPFR Library. 7 8 The GNU MPFR Library is free software; you can redistribute it and/or modify 9 it under the terms of the GNU Lesser General Public License as published by 10 the Free Software Foundation; either version 3 of the License, or (at your 11 option) any later version. 12 13 The GNU MPFR Library is distributed in the hope that it will be useful, but 14 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 15 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 16 License for more details. 17 18 You should have received a copy of the GNU Lesser General Public License 19 along with the GNU MPFR Library; see the file COPYING.LESSER. If not, see 20 https://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc., 21 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */ 22 23 #include "mpfr-test.h" 24 25 #ifdef CHECK_EXTERNAL 26 static int 27 test_log10 (mpfr_ptr a, mpfr_srcptr b, mpfr_rnd_t rnd_mode) 28 { 29 int res; 30 int ok = rnd_mode == MPFR_RNDN && mpfr_number_p (b) && mpfr_get_prec (a)>=53; 31 if (ok) 32 { 33 mpfr_print_raw (b); 34 } 35 res = mpfr_log10 (a, b, rnd_mode); 36 if (ok) 37 { 38 printf (" "); 39 mpfr_print_raw (a); 40 printf ("\n"); 41 } 42 return res; 43 } 44 #else 45 #define test_log10 mpfr_log10 46 #endif 47 48 #define TEST_FUNCTION test_log10 49 #define TEST_RANDOM_POS 8 50 #include "tgeneric.c" 51 52 int 53 main (int argc, char *argv[]) 54 { 55 mpfr_t x, y; 56 unsigned int n; 57 int inex; 58 59 tests_start_mpfr (); 60 61 test_generic (MPFR_PREC_MIN, 100, 20); 62 63 mpfr_init2 (x, 53); 64 mpfr_init2 (y, 53); 65 66 /* check NaN */ 67 mpfr_set_nan (x); 68 inex = test_log10 (y, x, MPFR_RNDN); 69 MPFR_ASSERTN (mpfr_nan_p (y) && inex == 0); 70 71 /* check Inf */ 72 mpfr_set_inf (x, -1); 73 inex = test_log10 (y, x, MPFR_RNDN); 74 MPFR_ASSERTN (mpfr_nan_p (y) && inex == 0); 75 76 mpfr_set_inf (x, 1); 77 inex = test_log10 (y, x, MPFR_RNDN); 78 MPFR_ASSERTN (mpfr_inf_p (y) && mpfr_sgn (y) > 0 && inex == 0); 79 80 mpfr_set_ui (x, 0, MPFR_RNDN); 81 inex = test_log10 (x, x, MPFR_RNDN); 82 MPFR_ASSERTN (mpfr_inf_p (x) && mpfr_sgn (x) < 0 && inex == 0); 83 mpfr_set_ui (x, 0, MPFR_RNDN); 84 mpfr_neg (x, x, MPFR_RNDN); 85 inex = test_log10 (x, x, MPFR_RNDN); 86 MPFR_ASSERTN (mpfr_inf_p (x) && mpfr_sgn (x) < 0 && inex == 0); 87 88 /* check negative argument */ 89 mpfr_set_si (x, -1, MPFR_RNDN); 90 inex = test_log10 (y, x, MPFR_RNDN); 91 MPFR_ASSERTN (mpfr_nan_p (y) && inex == 0); 92 93 /* check log10(1) = 0 */ 94 mpfr_set_ui (x, 1, MPFR_RNDN); 95 inex = test_log10 (y, x, MPFR_RNDN); 96 MPFR_ASSERTN (mpfr_cmp_ui (y, 0) == 0 && MPFR_IS_POS (y) && inex == 0); 97 98 /* check log10(10^n)=n */ 99 mpfr_set_ui (x, 1, MPFR_RNDN); 100 for (n = 1; n <= 15; n++) 101 { 102 mpfr_mul_ui (x, x, 10, MPFR_RNDN); /* x = 10^n */ 103 inex = test_log10 (y, x, MPFR_RNDN); 104 if (mpfr_cmp_ui (y, n)) 105 { 106 printf ("log10(10^n) <> n for n=%u\n", n); 107 exit (1); 108 } 109 MPFR_ASSERTN (inex == 0); 110 } 111 112 mpfr_clear (x); 113 mpfr_clear (y); 114 115 data_check ("data/log10", mpfr_log10, "mpfr_log10"); 116 117 tests_end_mpfr (); 118 return 0; 119 } 120