Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
1996
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 1996

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: "FortranForm" for numbers?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg4640] Re: "FortranForm" for numbers?
  • From: rubin at msu.edu (Paul A. Rubin)
  • Date: Thu, 22 Aug 1996 03:54:57 -0400
  • Organization: Michigan State University
  • Sender: owner-wri-mathgroup at wolfram.com

In article <4u9ihd$moj at dragonfly.wolfram.com>,
   siegman at ee.stanford.edu (AES) wrote:
->
->Wanted: 
->
->1)  A simple way to put an option at the start of a notebook so that all
->output numbers (in Print, Write, TableForm, etc) will come out in
->something similar to Fortran format, e.g.
->
->     4.56789E-7
->
->(I don't care much about the exact number of digits, or exactly where the
->decimal point ends up, and the exponent part can be skipped if not
->needed.  Note that in mma FortranForm[1/7] comes out as 1/7 and not
->0.1429).

I was with you until you said "simple."  :-)

Try defining the following functions:

Clear[ oForm, fForm ]
fForm[ x_ ] :=
  StringJoin[ 
    RealDigits[ x ] /. 
      {a_List, b_} :> ToString /@
      Flatten[ {First[ a ], ".", Rest[ a ], "E", b - 1} ]
  ]
oForm[ x_Integer ] := 
  fForm[ N[ x, Length[ IntegerDigits[ x ] ] ] ]
oForm[ x_Rational ] := fForm[ N[ x ] ]
oForm[ x_?NumberQ ] := fForm[ x ]
oForm[ x_ ] := x
SetAttributes[ oForm, Listable ]

The function oForm will convert integer, rational and real quantities to 
Fortran-like notation, giving integers their full complement of significant 
digits (no loss of accuracy), converting rationals to machine numbers with 
default precision, and leaving the precision of other reals alone.  It 
should leave anything else unmolested.  If you don't want to convert 
integers, change the first part of the oForm definition to 
oForm[ x_Integer ] := x.

Now, somewhere at the top of your notebook (after defining fForm and 
oForm), add the line

$PrePrint = oForm;

which will cause all output to be passed through oForm ...

.. except output in a Print statement or in any kind of wrapper (such as 
TableForm).  You could extend the definition of oForm to have it commute 
with each individual wrapper (e.g. TableForm), but it might be less hassle 
just to type it in, as in

Print[ oForm[ Range[ 3 ] ] ]

or 

TableForm[ oForm[ Range[ 3 ] ] ].

->
->2)  I'd also like to understand why TableForm sometimes comes out with
->blank lines between each line of output and sometimes not, on the same
->table (and it's not due to word wrapping).

Just to annoy me - you're what the military would call "collateral damage." 
 :-)

Then again, it might be a rounding error in computing the number of elapsed 
lines or somesuch.

->  And how to put multiple
->"sometable // TableForm ;" statements inside a Do loop and have them
->Print.
->

Do[ ...; Print[ TableForm[ sometable ] ]; Print[ TableForm[ someothertable 
] ]; ..., {iterator} ] should work.


Paul

**************************************************************************
* Paul A. Rubin                                  Phone: (517) 432-3509   *
* Department of Management                       Fax:   (517) 432-1111   *
* Eli Broad Graduate School of Management        Net:   RUBIN at MSU.EDU    *
* Michigan State University                                              *
* East Lansing, MI  48824-1122  (USA)                                    *
**************************************************************************
Mathematicians are like Frenchmen:  whenever you say something to them,
they translate it into their own language, and at once it is something
entirely different.                                    J. W. v. GOETHE

==== [MESSAGE SEPARATOR] ====


  • Prev by Date: --->What is the problem with 'ParametricPlot'?
  • Next by Date: Help: Professional vd student versions of Mma 2.2.3
  • Previous by thread: "FortranForm" for numbers?
  • Next by thread: Windows fonts (v2.2.2 & 2.2.3)