1 /* set_ld.c -- Assign a floating-point long double to an interval.
2 
3 Copyright 2018
4                      AriC project, Inria Grenoble - Rhone-Alpes, France
5 
6 This file is part of the MPFI Library.
7 
8 The MPFI 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 2.1 of the License, or (at your
11 option) any later version.
12 
13 The MPFI 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 MPFI Library; see the file COPYING.LIB.  If not, write to
20 the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
21 MA 02110-1301, USA. */
22 
23 #include "mpfi-impl.h"
24 
25 int
mpfi_set_ld(mpfi_ptr a,const long double b)26 mpfi_set_ld (mpfi_ptr a, const long double b)
27 {
28   int inexact_left, inexact_right, inexact=0;
29 
30   inexact_left = mpfr_set_ld (&(a->left), b, MPFI_RNDD);
31   inexact_right = mpfr_set_ld (&(a->right), b, MPFI_RNDU);
32 
33   if ( MPFI_NAN_P (a) )
34     MPFR_RET_NAN;
35 
36   if (b == 0.0) {
37     /* fix signed zero so as to return [+0, -0] */
38     mpfr_setsign (&(a->left), &(a->left), 0, MPFI_RNDU);
39     mpfr_setsign (&(a->right), &(a->right), 1, MPFI_RNDD);
40   }
41 
42   if (inexact_left)
43     inexact += 1;
44   if (inexact_right)
45     inexact += 2;
46 
47   return inexact;
48 }
49 
50 /* Combined initialization and assignment      */
51 
52 int
mpfi_init_set_ld(mpfi_ptr a,const long double b)53 mpfi_init_set_ld (mpfi_ptr a, const long double b)
54 {
55   int inexact_left, inexact_right, inexact = 0;
56 
57   inexact_left = mpfr_init_set_ld (&(a->left), b, MPFI_RNDD);
58   inexact_right = mpfr_init_set_ld (&(a->right), b, MPFI_RNDU);
59 
60   if ( MPFI_NAN_P (a) )
61     MPFR_RET_NAN;
62 
63   if (b == 0.0) {
64     /* fix signed zero so as to return [+0, -0] */
65     mpfr_setsign (&(a->left), &(a->left), 0, MPFI_RNDU);
66     mpfr_setsign (&(a->right), &(a->right), 1, MPFI_RNDD);
67   }
68 
69   if (inexact_left)
70     inexact += 1;
71   if (inexact_right)
72     inexact += 2;
73 
74   return inexact;
75 }
76