~ubuntu-branches/ubuntu/intrepid/ecl/intrepid

« back to all changes in this revision

Viewing changes to src/gmp/mpfr/exp2.c

  • Committer: Bazaar Package Importer
  • Author(s): Peter Van Eynde
  • Date: 2006-05-17 02:46:26 UTC
  • Revision ID: james.westby@ubuntu.com-20060517024626-lljr08ftv9g9vefl
Tags: upstream-0.9h-20060510
ImportĀ upstreamĀ versionĀ 0.9h-20060510

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* mpfr_exp2 -- power of 2 function 2^y 
 
2
 
 
3
Copyright 2001, 2002 Free Software Foundation.
 
4
 
 
5
This file is part of the MPFR Library.
 
6
 
 
7
The MPFR Library is free software; you can redistribute it and/or modify
 
8
it under the terms of the GNU Lesser General Public License as published by
 
9
the Free Software Foundation; either version 2.1 of the License, or (at your
 
10
option) any later version.
 
11
 
 
12
The MPFR Library is distributed in the hope that it will be useful, but
 
13
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 
14
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 
15
License for more details.
 
16
 
 
17
You should have received a copy of the GNU Lesser General Public License
 
18
along with the MPFR Library; see the file COPYING.LIB.  If not, write to
 
19
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 
20
MA 02111-1307, USA. */
 
21
 
 
22
#include "gmp.h"
 
23
#include "gmp-impl.h"
 
24
#include "mpfr.h"
 
25
#include "mpfr-impl.h"
 
26
 
 
27
 /* The computation of y=pow(2,z) is done by
 
28
 
 
29
    y=exp(z*log(2))=2^z
 
30
 */
 
31
 
 
32
int
 
33
mpfr_exp2 (mpfr_ptr y, mpfr_srcptr x, mp_rnd_t rnd_mode) 
 
34
{    
 
35
 
 
36
    int inexact =0;
 
37
 
 
38
    if (MPFR_IS_NAN(x))
 
39
      {
 
40
        MPFR_SET_NAN(y);
 
41
        MPFR_RET_NAN;
 
42
      }
 
43
 
 
44
    MPFR_CLEAR_NAN(y);
 
45
 
 
46
    if (MPFR_IS_INF(x))
 
47
      {
 
48
        if (MPFR_SIGN(x) > 0)
 
49
          {
 
50
            MPFR_SET_INF(y);
 
51
          }
 
52
        else
 
53
          {
 
54
            MPFR_CLEAR_INF(y);
 
55
            MPFR_SET_ZERO(y);
 
56
          }
 
57
        MPFR_SET_POS(y);
 
58
        MPFR_RET(0);
 
59
      }
 
60
 
 
61
    /* 2^0 = 1 */
 
62
    if (MPFR_IS_ZERO(x))
 
63
      return mpfr_set_ui (y, 1, rnd_mode);
 
64
 
 
65
    /* since the smallest representable non-zero float is 1/2*2^__mpfr_emin,
 
66
       if x < __mpfr_emin - 1, the result is either 1/2*2^__mpfr_emin or 0 */
 
67
    if (mpfr_cmp_si_2exp (x, __mpfr_emin - 1, 0) < 0)
 
68
      return mpfr_set_underflow (y, rnd_mode, 1);
 
69
 
 
70
    /* General case */
 
71
    {
 
72
    /* Declaration of the intermediary variable */
 
73
      mpfr_t t, te;
 
74
 
 
75
      /* Declaration of the size variable */
 
76
      mp_prec_t Nx = MPFR_PREC(x);   /* Precision of input variable */
 
77
      mp_prec_t Ny = MPFR_PREC(y);   /* Precision of input variable */
 
78
 
 
79
      mp_prec_t Nt;   /* Precision of the intermediary variable */
 
80
      long int err;  /* Precision of error */
 
81
                
 
82
      /* compute the precision of intermediary variable */
 
83
      Nt = MAX(Nx, Ny);
 
84
      /* the optimal number of bits : see algorithms.ps */
 
85
      Nt = Nt + 5 + _mpfr_ceil_log2 (Nt);
 
86
 
 
87
 
 
88
      /* initialise of intermediary     variable */
 
89
      mpfr_init (t);
 
90
      mpfr_init (te);
 
91
 
 
92
      /* First computation */
 
93
      do {
 
94
 
 
95
        /* reactualisation of the precision */
 
96
        mpfr_set_prec (t, Nt);             
 
97
        mpfr_set_prec (te, Nt);             
 
98
 
 
99
        /* compute   exp(x*ln(2))*/
 
100
        mpfr_const_log2 (t, GMP_RNDU);    /* ln(2) */
 
101
        mpfr_mul (te, x, t, GMP_RNDU);    /* x*ln(2) */
 
102
        mpfr_exp (t, te, GMP_RNDN);       /* exp(x*ln(2))*/
 
103
 
 
104
        /* estimate of the error -- see pow function in algorithms.ps*/
 
105
        err = Nt - (MPFR_EXP(te) + 2);
 
106
 
 
107
        /* actualisation of the precision */
 
108
        Nt += _mpfr_isqrt (Nt) + 10;
 
109
 
 
110
      } while ((err < 0) || !mpfr_can_round (t, err, GMP_RNDN, rnd_mode, Ny));
 
111
 
 
112
      inexact = mpfr_set (y, t, rnd_mode);
 
113
 
 
114
      mpfr_clear (t);
 
115
      mpfr_clear (te);
 
116
    }
 
117
    return inexact;
 
118
 
 
119
}