Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
1995
*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 1995

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

Search the Archive

Re: ? write/save truncated numbers to file

  • To: mathgroup at christensen.cybernetics.net
  • Subject: [mg1966] Re: [mg1953] ? write/save truncated numbers to file
  • From: John Fultz <jfultz>
  • Date: Sun, 27 Aug 1995 23:24:42 -0400

> Hi Mma experts,
> 
> Greetings.  The other day i saw another posting with basically the same
> question, but haven't seen posted answers yet, so please pardon.
> 
> First off, i'm a loyal fan of Mma and WRI, so this is no mean a complain
> (how minor it is), but rather, i'm hoping to hear i've been ignorant ...
> 
> The idea is that i don't need to save full-accuracy numbers (often
> reaching 14-15 decimal places), and instead wanted, say, 2-3 decimal places.
> This would cut down the file size by half or so, and even more important,
> it makes much better comprehension of the data save.  i'm running things that
> gave 100-200+ printed pages each run, and need to run a lot (100+ of runs).  
> Truncated numbers is real helpful, as you can imagine.
> 
> i've tried to use NumberForm[ exp, numberLength ], but Print/Write
> into files just doesn't evaluate the NumberForm function, making the
> printed expressions even harder to read.  So, i've been waiting to hear
> if someone mentioned this problem/solution.
> 
> Thank you for your time and kind help.  i'd appreciate it greatly.
> 
> Best regards,
> - thang nguyen

What follows is a transcript of a session I created with the method you're
probably using, which fails as you stated, and a method that works.

Mathematica 2.2 for SPARC
Copyright 1988-94 Wolfram Research, Inc.
 -- Motif graphics initialized -- 

In[1]:= OpenWrite["foo"]

Out[1]= OutputStream[foo, 3]

In[2]:= NumberForm[N[Pi], {4, 3}] >> "foo"

In[3]:= Close["foo"]

Out[3]= foo

In[4]:= !!foo
NumberForm[3.141592653589793, {4, 3}]

In[4]:= OpenWrite["foo", FormatType->OutputForm]

Out[4]= OutputStream[foo, 4]

In[5]:= NumberForm[N[Pi], {4, 3}] >> "foo"

In[6]:= Close["foo"]

Out[6]= foo

In[7]:= !!foo
3.142

Note the difference was in how we opened the stream.  We set the option
FormatType to OutputForm.  FormatType describes what format will be used
when writing to a file being opened by OpenWrite (FormatType can also
be set for OpenAppend and for standalone streams).  By default:

In[7]:= Options[OpenWrite]

Out[7]= {PageWidth -> 78, PageHeight -> 22, TotalWidth -> Infinity, 
 
>    FormatType -> InputForm, TotalHeight -> Infinity, 
 
>    StringConversion :> $StringConversion}

You might wonder, why on earth did those Wolfram people set FormatType
to InputForm, when clearly I want to write what I'm seeing on the screen
as output (i.e. OutputForm)?  Well, I believe the motivation here was
that if you write an expression to a stream in InputForm, you can always
read it back into Mathematica using the Get (<<) command.  It is guaranteed
to be exactly the same as it was written out (including all digits of
precision preserved, and so on).  So, the default chosen by Wolfram is
to not throw away any information.

John Fultz
Wolfram Research, Inc.


  • Prev by Date: MacTCP connection
  • Next by Date: Find with line returns
  • Previous by thread: ? write/save truncated numbers to file
  • Next by thread: Re: ? write/save truncated numbers to file