~ubuntu-branches/debian/sid/genius/sid

« back to all changes in this revision

Viewing changes to mpfr/log1p.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2006-08-21 12:57:45 UTC
  • Revision ID: james.westby@ubuntu.com-20060821125745-sl9ks8v7fq324bdf
Tags: upstream-0.7.6.1
ImportĀ upstreamĀ versionĀ 0.7.6.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* mpfr_log1p -- Compute log(1+x)
 
2
 
 
3
Copyright 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
 
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., 51 Franklin Place, Fifth Floor, Boston,
 
20
MA 02110-1301, USA. */
 
21
 
 
22
#define MPFR_NEED_LONGLONG_H
 
23
#include "mpfr-impl.h"
 
24
 
 
25
 /* The computation of log1p is done by
 
26
    log1p(x)=log(1+x)                      */
 
27
 
 
28
int
 
29
mpfr_log1p (mpfr_ptr y, mpfr_srcptr x, mp_rnd_t rnd_mode)
 
30
{
 
31
  int comp, inexact;
 
32
  MPFR_SAVE_EXPO_DECL (expo);
 
33
 
 
34
  if (MPFR_UNLIKELY (MPFR_IS_SINGULAR (x)))
 
35
    {
 
36
      if (MPFR_IS_NAN (x))
 
37
        {
 
38
          MPFR_SET_NAN (y);
 
39
          MPFR_RET_NAN;
 
40
        }
 
41
      /* check for inf or -inf (result is not defined) */
 
42
      else if (MPFR_IS_INF (x))
 
43
        {
 
44
          if (MPFR_IS_POS (x))
 
45
            {
 
46
              MPFR_SET_INF (y);
 
47
              MPFR_SET_POS (y);
 
48
              MPFR_RET (0);
 
49
            }
 
50
          else
 
51
            {
 
52
              MPFR_SET_NAN (y);
 
53
              MPFR_RET_NAN;
 
54
            }
 
55
        }
 
56
      else /* x is zero */
 
57
        {
 
58
          MPFR_ASSERTD (MPFR_IS_ZERO (x));
 
59
          MPFR_SET_ZERO (y);   /* log1p(+/- 0) = +/- 0 */
 
60
          MPFR_SET_SAME_SIGN (y, x);
 
61
          MPFR_RET (0);
 
62
        }
 
63
    }
 
64
 
 
65
  /* log(1+x) = x-x^2/2 + ... so the error is < 2^(2*EXP(x)-1) */
 
66
  MPFR_FAST_COMPUTE_IF_SMALL_INPUT (y, x, -MPFR_GET_EXP (x)+1,0,rnd_mode,{});
 
67
 
 
68
  comp = mpfr_cmp_si (x, -1);
 
69
  /* log1p(x) is undefined for x < -1 */
 
70
  if (MPFR_UNLIKELY(comp <= 0))
 
71
    {
 
72
      if (comp == 0)
 
73
        /* x=0: log1p(-1)=-inf (division by zero) */
 
74
        {
 
75
          MPFR_SET_INF (y);
 
76
          MPFR_SET_NEG (y);
 
77
          MPFR_RET (0);
 
78
        }
 
79
      MPFR_SET_NAN (y);
 
80
      MPFR_RET_NAN;
 
81
    }
 
82
 
 
83
  MPFR_SAVE_EXPO_MARK (expo);
 
84
 
 
85
  /* General case */
 
86
  {
 
87
    /* Declaration of the intermediary variable */
 
88
    mpfr_t t;
 
89
    /* Declaration of the size variable */
 
90
    mp_prec_t Ny = MPFR_PREC(y);             /* target precision */
 
91
    mp_prec_t Nt;                            /* working precision */
 
92
    mp_exp_t err;                            /* error */
 
93
    MPFR_ZIV_DECL (loop);
 
94
 
 
95
    /* compute the precision of intermediary variable */
 
96
    /* the optimal number of bits : see algorithms.tex */
 
97
    Nt = Ny + MPFR_INT_CEIL_LOG2 (Ny) + 6;
 
98
 
 
99
    /* if |x| is smaller than 2^(-e), we will loose about e bits
 
100
       in log(1+x) */
 
101
    if (MPFR_EXP(x) < 0)
 
102
      Nt += -MPFR_EXP(x);
 
103
 
 
104
    /* initialise of intermediary variable */
 
105
    mpfr_init2 (t, Nt);
 
106
 
 
107
    /* First computation of cosh */
 
108
    MPFR_ZIV_INIT (loop, Nt);
 
109
    for (;;)
 
110
      {
 
111
        /* compute log1p */
 
112
        mpfr_add_ui (t, x, 1, GMP_RNDN);      /* 1+x */
 
113
        mpfr_log (t, t, GMP_RNDN);        /* log(1+x)*/
 
114
 
 
115
        /* estimation of the error */
 
116
        /*err=Nt-(__gmpfr_ceil_log2(1+pow(2,1-MPFR_GET_EXP(t))));*/
 
117
        err = Nt - (MAX (1 - MPFR_GET_EXP (t), 0) + 1);
 
118
 
 
119
        if (MPFR_LIKELY (MPFR_CAN_ROUND (t, err, Ny, rnd_mode)))
 
120
          break;
 
121
 
 
122
        /* increase the precision */
 
123
        MPFR_ZIV_NEXT (loop, Nt);
 
124
        mpfr_set_prec (t, Nt);
 
125
      }
 
126
    MPFR_ZIV_FREE (loop);
 
127
    inexact = mpfr_set (y, t, rnd_mode);
 
128
 
 
129
    mpfr_clear (t);
 
130
  }
 
131
 
 
132
  MPFR_SAVE_EXPO_FREE (expo);
 
133
  return mpfr_check_range (y, inexact, rnd_mode);
 
134
}