xref: /netbsd-src/external/lgpl3/mpfr/dist/src/powr.c (revision ba125506a622fe649968631a56eba5d42ff57863)
1 /* mpfr_powr -- powr(x,y) = exp(y*log(x))
2 
3 Copyright 2021-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-impl.h"
24 
25 int
mpfr_powr(mpfr_ptr z,mpfr_srcptr x,mpfr_srcptr y,mpfr_rnd_t rnd_mode)26 mpfr_powr (mpfr_ptr z, mpfr_srcptr x, mpfr_srcptr y, mpfr_rnd_t rnd_mode)
27 {
28   /* powr(x,y) is NaN for x=NaN or x < 0 (a)
29      powr(+/-0,+/-0) is NaN whereas pow(x,+/-0) = 1 if x is not NaN (b)
30      powr(+Inf,+/-0) is NaN whereas pow(x,+/-0) = 1 if x is not NaN (b)
31      We can use MPFR_IS_SINGULAR since we already excluded x=NaN and x=-Inf.
32      powr(1,+/-Inf) = NaN whereas pow(1,+/-Inf) = 1 (c) */
33   if (MPFR_IS_NAN (x) || MPFR_IS_STRICTNEG(x) ||      /* case (a) */
34       (MPFR_IS_SINGULAR(x) && MPFR_IS_ZERO(y)) ||     /* case (b) */
35       (mpfr_cmp_ui (x, 1) == 0 && MPFR_IS_INF(y)))    /* case (c) */
36     {
37       MPFR_SET_NAN (z);
38       MPFR_RET_NAN;
39     }
40   /* for finite y < 0, powr(-0,y) is +Inf, whereas pow(-0,y) is -Inf for y an
41      odd negativeinteger, and +Inf otherwise */
42   if (MPFR_IS_ZERO(x) && MPFR_SIGN(x) < 0 && !MPFR_IS_SINGULAR (y)
43       && MPFR_IS_NEG(y))
44     {
45       MPFR_SET_INF (z);
46       MPFR_SET_POS (z);
47       MPFR_SET_DIVBY0 ();
48       MPFR_RET (0);
49     }
50   /* for y > 0, powr(-0,y) is +0, whereas pow gives -0 for y an odd integer */
51   if (MPFR_IS_ZERO(x) && MPFR_SIGN(x) < 0 && !MPFR_IS_SINGULAR (y)
52       && MPFR_IS_POS(y))
53     {
54       MPFR_SET_ZERO (z);
55       MPFR_SET_POS (z);
56       MPFR_RET (0);
57     }
58   /* otherwise powr coincides with pow */
59   return mpfr_pow (z, x, y, rnd_mode);
60 }
61