The R Project SVN R

Rev

Rev 87925 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 87925 Rev 88880
Line 73... Line 73...
73
*/
73
*/
74
 
74
 
75
attribute_hidden
75
attribute_hidden
76
double R_pretty(double *lo, double *up, int *ndiv, int min_n,
76
double R_pretty(double *lo, double *up, int *ndiv, int min_n,
77
		double shrink_sml,
77
		double shrink_sml,
78
		const double high_u_fact[], // = (h, h5, f_min) below 
78
		const double high_u_fact[], // = (h, h5, f_min) below
79
		int eps_correction, int return_bounds)
79
		int eps_correction, int return_bounds)
80
{
80
{
81
/* From version 0.65 on, we had rounding_eps := 1e-5, before, r..eps = 0
81
/* From version 0.65 on, we had rounding_eps := 1e-5, before, r..eps = 0
82
 * then, 1e-7 was consistent with seq.default() and seq.int() till 2010-02-03,
82
 * then, 1e-7 was consistent with seq.default() and seq.int() till 2010-02-03,
83
 * where it was changed to 1e-10 for seq*(), and in 2017-08-14 for pretty(): */
83
 * where it was changed to 1e-10 for seq*(), and in 2017-08-14 for pretty(): */
Line 108... Line 108...
108
    }
108
    }
109
 
109
 
110
#ifdef DEBUGpr
110
#ifdef DEBUGpr
111
    REprintf("R_pretty(lo=%g,up=%g,ndiv=%d,min_n=%d,shrink=%g,high_u=(%g,%g,%g),eps=%d,bnds=%d)"
111
    REprintf("R_pretty(lo=%g,up=%g,ndiv=%d,min_n=%d,shrink=%g,high_u=(%g,%g,%g),eps=%d,bnds=%d)"
112
	     "\n\t => dx=%g; i_small:%s. ==> first cell=%g\n",
112
	     "\n\t => dx=%g; i_small:%s. ==> first cell=%g\n",
113
	     lo_, up_, *ndiv, min_n, shrink_sml, h, h5, min_f,
113
	     lo_, up_, *ndiv, min_n, shrink_sml, h, h5, f_min,
114
	     eps_correction, return_bounds,
114
	     eps_correction, return_bounds,
115
	     dx, i_small ? "TRUE" : "F", cell);
115
	     dx, i_small ? "TRUE" : "F", cell);
116
#endif
116
#endif
117
 
117
 
118
    /*OLD: cell = FLT_EPSILON+ dx / *ndiv; FLT_EPSILON = 1.192e-07 */
118
    /*OLD: cell = FLT_EPSILON+ dx / *ndiv; FLT_EPSILON = 1.192e-07 */
Line 140... Line 140...
140
 
140
 
141
    double subsmall = f_min*DBL_MIN;
141
    double subsmall = f_min*DBL_MIN;
142
    if(subsmall == 0.) // subnormals underflowing to zero (not yet seen!)
142
    if(subsmall == 0.) // subnormals underflowing to zero (not yet seen!)
143
	subsmall = DBL_MIN;
143
	subsmall = DBL_MIN;
144
    if(cell < subsmall) { // possibly subnormal
144
    if(cell < subsmall) { // possibly subnormal
-
 
145
	if(cell > 0)
145
	warning(_("R_pretty(): very small range 'cell'=%g, corrected to %g"),
146
	  warning(_("R_pretty(): very small range 'cell'=%.3g, increased to %g"),
146
		cell, subsmall);
147
		cell, subsmall);
147
	cell = subsmall;
148
	cell = subsmall;
148
    } else if(cell > DBL_MAX/MAX_F) {
149
    } else if(cell > DBL_MAX/MAX_F) {
149
	warning(_("R_pretty(): very large range 'cell'=%g, corrected to %g"),
150
	warning(_("R_pretty(): very large range 'cell'=%.3g, decreased to %g"),
150
		cell, DBL_MAX/MAX_F);
151
		cell, DBL_MAX/MAX_F);
151
	cell = DBL_MAX/MAX_F;
152
	cell = DBL_MAX/MAX_F;
152
    }
153
    }
153
 
154
 
154
#undef MAX_F
155
#undef MAX_F
Line 179... Line 180...
179
#ifdef DEBUGpr
180
#ifdef DEBUGpr
180
    REprintf("\t => final cell=%g; base=%g unit=%g; (ns,nu) = (%g,%g)\n",
181
    REprintf("\t => final cell=%g; base=%g unit=%g; (ns,nu) = (%g,%g)\n",
181
	     cell, base, unit, ns, nu);
182
	     cell, base, unit, ns, nu);
182
#endif
183
#endif
183
    if(eps_correction && (eps_correction > 1 || !i_small)) {
184
    if(eps_correction && (eps_correction > 1 || !i_small)) {
-
 
185
#define E_ DBL_EPSILON
184
	// FIXME?: assumes 0 <= lo <= up  (what if lo <= up < 0 ?)
186
	const double D_max = DBL_MAX*(1. - ldexp(E_, -1));
-
 
187
	/* move *lo  to the left, assuming <subnorm>*(1-E_) does not underflow to 0 : */
185
	if(lo_ != 0.) *lo *= (1- DBL_EPSILON); else *lo = -DBL_MIN;
188
	if(lo_ < 0.) *lo *= (1+E_); else if(lo_ > 0) *lo *= (1-E_);  else *lo = -fmin2(unit, DBL_MIN);
-
 
189
	/* and  *up  to the right : */
186
	if(up_ != 0.) *up *= (1+ DBL_EPSILON); else *up = +DBL_MIN;
190
	if(up_ < 0.) *up *= (1-E_); else if(up_ > 0.) {
-
 
191
				     if(up_ < D_max) *up *= (1+E_);} else *up = +fmin2(unit, DBL_MIN);
-
 
192
#undef E_
-
 
193
#ifdef DEBUGpr
-
 
194
	REprintf("  eps_correction (assuming lo=%g <= %g=up): new *lo=%g, *up=%g\n",
-
 
195
		 lo_, up_, *lo, *up);
-
 
196
#endif
187
    }
197
    }
188
 
198
 
189
#ifdef DEBUGpr
199
#ifdef DEBUGpr
190
    if(ns*unit > *lo + rounding_eps*unit)
200
    if(ns*unit > *lo + rounding_eps*unit)
191
	REprintf("\t ns= %.0f -- while(ns*unit > lo + r_eps * u) ns--;\n", ns);
201
	REprintf("\t ns= %.0f -- while(ns*unit > lo + r_eps * u) ns--;\n", ns);
192
#endif
202
#endif
193
    while(ns*unit > *lo + rounding_eps*unit) ns--;
203
    while(ns*unit > *lo + rounding_eps*unit) ns--;
194
 
204
 
195
#ifdef DEBUGpr
205
#ifdef DEBUGpr
196
    if(!R_FINITE(ns*unit))
206
    if(!R_FINITE(ns*unit))
197
	REprintf("\t infinite (ns=%.0f)*(unit=%g)  ==> ns++\n", ns, unit);
207
	REprintf("\t while(!finite((ns=%.0f)*(unit=%g))) ns++\n", ns, unit);
198
#endif
208
#endif
199
    while(!R_FINITE(ns*unit)) ns++;
209
    while(!R_FINITE(ns*unit)) ns++;
200
 
210
 
201
 
211
 
202
#ifdef DEBUGpr
212
#ifdef DEBUGpr
Line 205... Line 215...
205
#endif
215
#endif
206
    while(nu*unit < *up - rounding_eps*unit) nu++;
216
    while(nu*unit < *up - rounding_eps*unit) nu++;
207
 
217
 
208
#ifdef DEBUGpr
218
#ifdef DEBUGpr
209
    if(!R_FINITE(nu*unit))
219
    if(!R_FINITE(nu*unit))
210
	REprintf("\t infinite (nu=%.0f)*(unit=%g)  ==> nu--\n", nu, unit);
220
	REprintf("\t while(!finite((nu=%.0f)*(unit=%g)) nu--\n", nu, unit);
211
#endif
221
#endif
212
    while(!R_FINITE(nu*unit)) nu--;
222
    while(!R_FINITE(nu*unit)) nu--;
213
 
223
 
214
    int k = (int)(0.5 + nu - ns);
224
    int k = (int)(0.5 + nu - ns);
215
#ifdef DEBUGpr
225
#ifdef DEBUGpr
Line 244... Line 254...
244
	if(nu * unit > *up) {REprintf("up=%g too small, set to nu*unit\n", *up); *up = nu * unit;}
254
	if(nu * unit > *up) {REprintf("up=%g too small, set to nu*unit\n", *up); *up = nu * unit;}
245
#else
255
#else
246
	if(ns * unit < *lo) *lo = ns * unit;
256
	if(ns * unit < *lo) *lo = ns * unit;
247
	if(nu * unit > *up) *up = nu * unit;
257
	if(nu * unit > *up) *up = nu * unit;
248
#endif
258
#endif
249
    } else { // used in graphics GEpretty(), hence grid::grid.pretty()
259
    } else { // used in graphics GEPretty(), hence grid::grid.pretty()
250
	*lo = ns;
260
	*lo = ns;
251
	*up = nu;
261
	*up = nu;
252
    }
262
    }
253
#ifdef DEBUGpr
263
#ifdef DEBUGpr
254
    REprintf("\t ns=%5.0g ==> lo=%g\n", ns, *lo);
264
    REprintf("\t ns=%5.0g ==> lo=%g\n", ns, *lo);