The R Project SVN R

Rev

Rev 68947 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 68947 Rev 74317
Line 1... Line 1...
1
/*
1
/*
2
 *  Mathlib : A C Library of Special Functions
2
 *  Mathlib : A C Library of Special Functions
3
 *  Copyright (C) 1998 Ross Ihaka
3
 *  Copyright (C) 1998 Ross Ihaka
4
 *  Copyright (C) 2000-2014 The R Core Team
4
 *  Copyright (C) 2000-2018 The R Core Team
5
 *
5
 *
6
 *  This program is free software; you can redistribute it and/or modify
6
 *  This program is free software; you can redistribute it and/or modify
7
 *  it under the terms of the GNU General Public License as published by
7
 *  it under the terms of the GNU General Public License as published by
8
 *  the Free Software Foundation; either version 2 of the License, or
8
 *  the Free Software Foundation; either version 2 of the License, or
9
 *  (at your option) any later version.
9
 *  (at your option) any later version.
Line 36... Line 36...
36
 */
36
 */
37
 
37
 
38
#include <config.h>
38
#include <config.h>
39
#include "nmath.h"
39
#include "nmath.h"
40
 
40
 
41
 
-
 
42
/*  nearbyint is C99, so all platforms should have it (and AFAIK, all do) */
-
 
43
#ifdef HAVE_NEARBYINT
-
 
44
# define R_rint nearbyint
-
 
45
#elif defined(HAVE_RINT)
-
 
46
# define R_rint rint
-
 
47
#else
-
 
48
# define R_rint private_rint
-
 
49
# include "nmath2.h" // for private_rint
-
 
50
#endif
-
 
51
 
-
 
52
/* Improvements by Martin Maechler, May 1997;
41
/* Improvements by Martin Maechler, May 1997;
53
   further ones, Feb.2000:
42
   further ones, Feb.2000:
54
   Replace  pow(x, (double)i) by  R_pow_di(x, i) {and use  int dig} */
43
   Replace  pow(x, (double)i) by  R_pow_di(x, i) {and use  int dig} */
55
 
44
 
56
#define MAX_DIGITS 22
45
#define MAX_DIGITS 22
Line 101... Line 90...
101
	    e10 = max10e;
90
	    e10 = max10e;
102
	} 
91
	} 
103
	if(e10 > 0) { /* Try always to have pow >= 1
92
	if(e10 > 0) { /* Try always to have pow >= 1
104
			 and so exactly representable */
93
			 and so exactly representable */
105
	    pow10 = R_pow_di(10., e10);
94
	    pow10 = R_pow_di(10., e10);
106
	    return(sgn*(R_rint((x*pow10)*p10)/pow10)/p10);
95
	    return(sgn*(nearbyint((x*pow10)*p10)/pow10)/p10);
107
	} else {
96
	} else {
108
	    pow10 = R_pow_di(10., -e10);
97
	    pow10 = R_pow_di(10., -e10);
109
	    return(sgn*(R_rint((x/pow10))*pow10));
98
	    return(sgn*(nearbyint((x/pow10))*pow10));
110
	}
99
	}
111
    } else { /* -- LARGE or small -- */
100
    } else { /* -- LARGE or small -- */
112
	do_round = max10e - l10	 >= R_pow_di(10., -dig);
101
	do_round = max10e - l10	 >= R_pow_di(10., -dig);
113
	e2 = dig + ((e10>0)? 1 : -1) * MAX_DIGITS;
102
	e2 = dig + ((e10>0)? 1 : -1) * MAX_DIGITS;
114
	p10 = R_pow_di(10., e2);	x *= p10;
103
	p10 = R_pow_di(10., e2);	x *= p10;