1efee5258Smrg /* Test file for mpfr_add_ui
2efee5258Smrg
3*ba125506Smrg Copyright 2000-2023 Free Software Foundation, Inc.
4efdec83bSmrg Contributed by the AriC and Caramba projects, INRIA.
5efee5258Smrg
6efee5258Smrg This file is part of the GNU MPFR Library.
7efee5258Smrg
8efee5258Smrg The GNU MPFR Library is free software; you can redistribute it and/or modify
9efee5258Smrg it under the terms of the GNU Lesser General Public License as published by
10efee5258Smrg the Free Software Foundation; either version 3 of the License, or (at your
11efee5258Smrg option) any later version.
12efee5258Smrg
13efee5258Smrg The GNU MPFR Library is distributed in the hope that it will be useful, but
14efee5258Smrg WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15efee5258Smrg or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16efee5258Smrg License for more details.
17efee5258Smrg
18efee5258Smrg You should have received a copy of the GNU Lesser General Public License
19efee5258Smrg along with the GNU MPFR Library; see the file COPYING.LESSER. If not, see
202ba2404bSmrg https://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
21efee5258Smrg 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
22efee5258Smrg
23efee5258Smrg #include <float.h>
24efee5258Smrg
25efee5258Smrg #include "mpfr-test.h"
26efee5258Smrg
27efee5258Smrg /* checks that x+y gives the right results with 53 bits of precision */
28efee5258Smrg static void
check3(const char * xs,unsigned long y,mpfr_rnd_t rnd_mode,const char * zs)29efee5258Smrg check3 (const char *xs, unsigned long y, mpfr_rnd_t rnd_mode, const char *zs)
30efee5258Smrg {
31efee5258Smrg mpfr_t xx, zz;
32efee5258Smrg
33efee5258Smrg mpfr_inits2 (53, xx, zz, (mpfr_ptr) 0);
34efee5258Smrg mpfr_set_str1 (xx, xs);
35efee5258Smrg mpfr_add_ui (zz, xx, y, rnd_mode);
36efee5258Smrg if (mpfr_cmp_str1(zz, zs) )
37efee5258Smrg {
38efee5258Smrg printf ("expected sum is %s, got ",zs);
39efee5258Smrg mpfr_out_str(stdout, 10, 0, zz, MPFR_RNDN);
40efee5258Smrg printf ("\nmpfr_add_ui failed for x=%s y=%lu with rnd_mode=%s\n",
41efee5258Smrg xs, y, mpfr_print_rnd_mode(rnd_mode));
42efee5258Smrg exit (1);
43efee5258Smrg }
44efee5258Smrg mpfr_clears (xx, zz, (mpfr_ptr) 0);
45efee5258Smrg }
46efee5258Smrg
47efee5258Smrg static void
special(void)48efee5258Smrg special (void)
49efee5258Smrg {
50efee5258Smrg mpfr_t x, y;
51efee5258Smrg
52efee5258Smrg mpfr_init2 (x, 63);
53efee5258Smrg mpfr_init2 (y, 63);
54efee5258Smrg mpfr_set_str_binary (x, "0.110100000000000001110001110010111111000000000101100011100100011");
55efee5258Smrg mpfr_add_ui (y, x, 1, MPFR_RNDD);
56efee5258Smrg mpfr_clear (x);
57efee5258Smrg mpfr_clear (y);
58efee5258Smrg }
59efee5258Smrg
60efee5258Smrg static void
check_nans(void)61efee5258Smrg check_nans (void)
62efee5258Smrg {
63efee5258Smrg mpfr_t x, y;
64efee5258Smrg
65efee5258Smrg mpfr_init2 (x, 123L);
66efee5258Smrg mpfr_init2 (y, 123L);
67efee5258Smrg
68efee5258Smrg /* nan + 2394875 == nan */
69efee5258Smrg mpfr_set_nan (x);
70efdec83bSmrg mpfr_clear_nanflag ();
71efee5258Smrg mpfr_add_ui (y, x, 2394875L, MPFR_RNDN);
72efdec83bSmrg MPFR_ASSERTN (mpfr_nanflag_p ());
73efee5258Smrg MPFR_ASSERTN (mpfr_nan_p (y));
74efee5258Smrg
75efee5258Smrg /* +inf + 2394875 == +inf */
76efee5258Smrg mpfr_set_inf (x, 1);
77efee5258Smrg mpfr_add_ui (y, x, 2394875L, MPFR_RNDN);
78efee5258Smrg MPFR_ASSERTN (mpfr_inf_p (y));
79efee5258Smrg MPFR_ASSERTN (mpfr_sgn (y) > 0);
80efee5258Smrg
81efee5258Smrg /* -inf + 2394875 == -inf */
82efee5258Smrg mpfr_set_inf (x, -1);
83efee5258Smrg mpfr_add_ui (y, x, 2394875L, MPFR_RNDN);
84efee5258Smrg MPFR_ASSERTN (mpfr_inf_p (y));
85efee5258Smrg MPFR_ASSERTN (mpfr_sgn (y) < 0);
86efee5258Smrg
87efee5258Smrg mpfr_clear (x);
88efee5258Smrg mpfr_clear (y);
89efee5258Smrg }
90efee5258Smrg
91efee5258Smrg #define TEST_FUNCTION mpfr_add_ui
92efdec83bSmrg #define ULONG_ARG2
93efee5258Smrg #define RAND_FUNCTION(x) mpfr_random2(x, MPFR_LIMB_SIZE (x), 1, RANDS)
94efdec83bSmrg #include "tgeneric.c"
95efee5258Smrg
96efee5258Smrg int
main(int argc,char * argv[])97efee5258Smrg main (int argc, char *argv[])
98efee5258Smrg {
99efee5258Smrg tests_start_mpfr ();
100efee5258Smrg
101efee5258Smrg check_nans ();
102efee5258Smrg
103efee5258Smrg special ();
104efee5258Smrg check3 ("-1.716113812768534e-140", 1271212614, MPFR_RNDZ,
105efee5258Smrg "1.27121261399999976e9");
106efee5258Smrg check3 ("1.22191250737771397120e+20", 948002822, MPFR_RNDN,
107efee5258Smrg "122191250738719408128.0");
108efee5258Smrg check3 ("-6.72658901114033715233e-165", 2000878121, MPFR_RNDZ,
109efee5258Smrg "2.0008781209999997615e9");
110efee5258Smrg check3 ("-2.0769715792901673e-5", 880524, MPFR_RNDN,
111efee5258Smrg "8.8052399997923023e5");
112efdec83bSmrg
113299c6f0cSmrg test_generic (MPFR_PREC_MIN, 1000, 100);
114efee5258Smrg
115efee5258Smrg tests_end_mpfr ();
116efee5258Smrg return 0;
117efee5258Smrg }
118