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

« back to all changes in this revision

Viewing changes to mpfr/isqrt.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
/* __gmpfr_isqrt && __gmpfr_cuberoot -- Integer square root and cube root
 
2
 
 
3
Copyright 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
#include "mpfr-impl.h"
 
23
 
 
24
/* returns floor(sqrt(n)) */
 
25
unsigned long
 
26
__gmpfr_isqrt (unsigned long n)
 
27
{
 
28
  unsigned long s;
 
29
 
 
30
  s = 1;
 
31
  do {
 
32
    s = (s + n / s) / 2;
 
33
  } while (!(s*s <= n && n <= s*(s+2)));
 
34
  return s;
 
35
}
 
36
 
 
37
/* returns floor(n^(1/3)) */
 
38
unsigned long
 
39
__gmpfr_cuberoot (unsigned long n)
 
40
{
 
41
  double s, is;
 
42
 
 
43
  s = 1.0;
 
44
  do {
 
45
    s = (2*s*s*s + (double) n) / (3*s*s);
 
46
    is = (double) ((int) s);
 
47
  } while (!(is*is*is <= (double) n && (double) n < (is+1)*(is+1)*(is+1)));
 
48
  return (unsigned long) is;
 
49
}
 
50