summaryrefslogtreecommitdiff
path: root/zeta.c
blob: 23702f2c595ef9a58cc06af56af0efa091dd231c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/* mpfr_zeta -- Riemann Zeta function at a floating-point number

Copyright 1999, 2000, 2001, 2002 Free Software Foundation.

This file is part of the MPFR Library.

The MPFR Library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or (at your
option) any later version.

The MPFR Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
License for more details.

You should have received a copy of the GNU Lesser General Public License
along with the MPFR Library; see the file COPYING.LIB.  If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA. */

#include <stdio.h>
#include <math.h>
#include "gmp.h"
#include "mpfr.h"
#include "gmp-impl.h"
#include "longlong.h"

int
#if __STDC__
mpfr_zeta (mpfr_ptr result, mpfr_srcptr op, mp_rnd_t rnd_mode)
#else
mpfr_zeta (result, op, rnd_mode)
     mpfr_ptr result;
     mpfr_srcptr op;
     mp_rnd_t rnd_mode;
#endif
{
  mpfr_t s,s2,x,y,u,b,v,nn,z,z2; 
  int i, n, succes;

  /* to do: check whether op is NaN or infinity,
     and clear NaN/Inf flags of result */

  /* first version */
  if (mpfr_get_d1 (op) != 2.0 || rnd_mode != GMP_RNDN
      || MPFR_PREC(result) != 53) {
    fprintf(stderr, "not yet implemented\n"); exit(1);
  }

  mpfr_set_default_prec(67);

  mpfr_init(x); mpfr_init(y); mpfr_init(s); mpfr_init(s2);
  mpfr_init(u); mpfr_init(b); mpfr_init(v); mpfr_init(nn);
  mpfr_init(z); mpfr_init(z2);

  mpfr_set_ui(u,1,GMP_RNDN);
  mpfr_set_ui(s,0,GMP_RNDN);

  /* s=Somme des 1/i^2 (i=100...2) */
  n=100;
  for (i=n; i>1; i--)
    {
      mpfr_div_ui(y,u,i*i,GMP_RNDN);
      mpfr_add(s,s,y,GMP_RNDN);
    }

  /* Application d'Euler-Maclaurin, jusqu'au terme 1/n^7 - n=100) */
  /*   mpfr_set_ui(nn,n,GMP_RNDN); */
  mpfr_div_ui(z,u,n,GMP_RNDN);
  mpfr_mul(z2,z,z,GMP_RNDN);
  mpfr_div_2ui(v,z2,1,GMP_RNDN);

  mpfr_set(s2,z,GMP_RNDN);
  mpfr_sub(s2,s2,v,GMP_RNDN);

  mpfr_mul(z,z,z2,GMP_RNDN);
  mpfr_div_ui(v,z,6,GMP_RNDN);
  mpfr_add(s2,s2,v,GMP_RNDN);

  mpfr_mul(z,z,z2,GMP_RNDN);
  mpfr_div_ui(v,z,30,GMP_RNDN);
  mpfr_sub(s2,s2,v,GMP_RNDN);

  mpfr_mul(z,z,z2,GMP_RNDN);
  mpfr_div_ui(v,z,42,GMP_RNDN);
  mpfr_add(s2,s2,v,GMP_RNDN);

  mpfr_add(s,s,s2,GMP_RNDN);
  mpfr_add(s,s,u,GMP_RNDN);

  /*Peut-on arrondir ? La reponse est oui*/
  succes=mpfr_can_round(s, 57, GMP_RNDN,GMP_RNDN, 53);

  if (succes) mpfr_set(result,s,GMP_RNDN);
  else {
    fprintf(stderr, "can't round in mpfr_zeta\n"); exit(1);
  }

  mpfr_clear(x); mpfr_clear(y); mpfr_clear(s); mpfr_clear(s2);
  mpfr_clear(u); mpfr_clear(b); mpfr_clear(v); mpfr_clear(nn);
  mpfr_clear(z); mpfr_clear(z2);

  return 1; /* result is inexact */
}