Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

[081fbe]: src / jrmath / fprec.c Maximize Restore History

Download this file

fprec.c    123 lines (112 with data), 3.5 kB

  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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
* Mathlib : A C Library of Special Functions
* Copyright (C) 1998 Ross Ihaka
* Copyright (C) 2000, 2001, 2005-2006 The R Development Core Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, a copy is available at
* http://www.r-project.org/Licenses/
*
* SYNOPSIS
*
* #include <Rmath.h>
* double fprec(double x, double digits);
*
* DESCRIPTION
*
* Returns the value of x rounded to "digits" significant
* decimal digits.
*
* NOTES
*
* This routine is a translation into C of a Fortran subroutine
* by W. Fullerton of Los Alamos Scientific Laboratory.
* Some modifications have been made so that the routines
* conform to the IEEE 754 standard.
*/
#include <config.h>
#include "nmath.h"
#ifndef HAVE_RINT
#define USE_BUILTIN_RINT
#endif
#ifdef USE_BUILTIN_RINT
#define R_rint private_rint
extern double private_rint(double x);
#else
#define R_rint rint
#endif
/* Improvements by Martin Maechler, May 1997;
further ones, Feb.2000:
Replace pow(x, (double)i) by R_pow_di(x, i) {and use int dig} */
#define MAX_DIGITS 22
/* was till R 0.99: DBL_DIG := digits of precision of a double, usually 15 */
/* FIXME: Hmm, have quite a host of these:
1) ./fround.c uses much more (sensibly!) ``instead''
2) ../main/coerce.c & ../main/deparse.c have DBL_DIG directly
3) ../main/options.c has #define MAX_DIGITS 22 for options(digits)
Really should decide on a (config.h dependent?) global MAX_DIGITS.
--MM--
*/
double fprec(double x, double digits)
{
double l10, pow10, sgn, p10, P10;
int e10, e2, do_round, dig;
/* Max.expon. of 10 (=308.2547) */
const static int max10e = DBL_MAX_EXP * M_LOG10_2;
#ifdef IEEE_754
if (ISNAN(x) || ISNAN(digits))
return x + digits;
if (!R_FINITE(x)) return x;
if (!R_FINITE(digits)) {
if(digits > 0) return x;
else return 0;
}
#endif
if(x == 0) return x;
dig = (int)floor(digits+0.5);
if (dig > MAX_DIGITS) {
return x;
} else if (dig < 1)
dig = 1;
sgn = 1.0;
if(x < 0.0) {
sgn = -sgn;
x = -x;
}
l10 = log10(x);
e10 = (int)(dig-1-floor(l10));
if(fabs(l10) < max10e - 2) {
p10 = 1.0;
if(e10 > max10e) { /* numbers less than 10^(dig-1) * 1e-308 */
p10 = R_pow_di(10., e10-max10e);
e10 = max10e;
}
if(e10 > 0) { /* Try always to have pow >= 1
and so exactly representable */
pow10 = R_pow_di(10., e10);
return(sgn*(R_rint((x*pow10)*p10)/pow10)/p10);
} else {
pow10 = R_pow_di(10., -e10);
return(sgn*(R_rint((x/pow10))*pow10));
}
} else { /* -- LARGE or small -- */
do_round = max10e - l10 >= R_pow_di(10., -dig);
e2 = dig + ((e10>0)? 1 : -1) * MAX_DIGITS;
p10 = R_pow_di(10., e2); x *= p10;
P10 = R_pow_di(10., e10-e2); x *= P10;
/*-- p10 * P10 = 10 ^ e10 */
if(do_round) x += 0.5;
x = floor(x) / p10;
return(sgn*x/P10);
}
}