[Rd] Suppressing Scientific Notation

David Brahm brahm at alum.mit.edu
Fri May 2 11:42:27 MEST 2003


R gurus,

Every so often(*) someone asks how to suppress scientific notation in
printing, so I thought I'd give it a shot, but I need some help.

The formatting decision is made(**) on line 286 of src/main/format.c :

    if (mF <= *m) { /* IFF it needs less space : "F" (Fixpoint) format */

where mF is the number of characters for "normal" printing and *m is the number
of characters for scientific notation.  If mf <= *m, then parameters are set
that cause "normal" printing.  My idea was to introduce a "penalty" for
scientific notation, which changes line 286 to:

    if (mF <= *m + R_print.scipen) {

R_print.scipen is an integer (defaulting to 0) set with "options":
  R> options(scipen=99)

I tried to copy the code for R_print.digits (as in "options(digits=7)")
wherever I found it, notably in the struct "R_print_par_t" defined in Print.h.
I changed main/options.c and main/print.c, as detailed in the diff output
below.  But I must have done it wrong because my version of R crashes with:
  > Error: bad value
  > Segmentation fault

Can anyone more familiar with options() help?  How do you add a new option
parameter?  Thanks!
-- 
                              -- David Brahm (brahm at alum.mit.edu)


******************************* Footnotes: ***********************************

(*) Thomas Gerds <gerds at fdm.uni-freiburg.de> "[R] printing decimal numbers"
      posted to R-help on 24 Feb 2003.
      > how can i force R to print 0.0001 instead of 1e-04???
    Bob Porter <rjporter at mindspring.com> "[R] scientific notation"
      posted to R-help on 16 Mar 2003
      > Is there a way to force R to forgo use of scientific notation...

(**) In R-1.7.0.  Also on lines 395 and 421 for complex numbers.

*********** diff output for my "scipen" modifications to R-1.7.0 *************

diff -r R-1.7.0/src/include/Print.h R-1.7.0.mod/src/include/Print.h
40a41
>     int scipen;
diff -r R-1.7.0/src/main/format.c R-1.7.0.mod/src/main/format.c
286c286
<     if (mF <= *m) { /* IFF it needs less space : "F" (Fixpoint) format */
---
>     if (mF <= *m + R_print.scipen) { /* IFF less space : "F" (Fixpoint) fmt */
395c395
< 	if (mF <= *mr) { /* IFF it needs less space : "F" (Fixpoint) format */
---
>         if (mF <= *mr + R_print.scipen) { /* IFF less space : "F"(Fixpt) fmt */
421c421
< 	if (mF <= *mi) { /* IFF it needs less space : "F" (Fixpoint) format */
---
>         if (mF <= *mi + R_print.scipen) { /* IFF less space : "F"(Fixpt) fmt */
diff -r R-1.7.0/src/main/options.c R-1.7.0.mod/src/main/options.c
52a53
>  *	"scipen"
136a138,140
> int GetOptionSciPen(SEXP rho) {
>     return asInteger(GetOption(install("scipen"), rho));
> }
235a240,243
>     SET_TAG(v, install("scipen"));
>     SETCAR(v, ScalarInteger(0));
>     v = CDR(v);
> 
374a383,386
> 	    else if (streql(CHAR(namei), "scipen")) {
> 		k = asInteger(argi);
> 		SET_VECTOR_ELT(value, i, SetOption(tag, ScalarInteger(k)));
> 	    }
diff -r R-1.7.0/src/main/print.c R-1.7.0.mod/src/main/print.c
88a89
>     R_print.scipen = GetOptionSciPen(rho);

***************************** End diff output ********************************



More information about the R-devel mailing list