Increase precision

Anything that doesn't fit elsewhere.
Post Reply
ChrisR

Increase precision

Post by ChrisR »

I am running a simulation and export to a file:

Code: Select all

objref rect, recv
rect = new Vector()
recv = new Vector()
recv.record(&soma.v(0.5))
rect.record(&t)

run()
objref savdata
savdata = new File()
savdata.wopen("result.dat")

objref tempmatrix
tempmatrix = new Matrix()
tempmatrix.resize(recv.size(),2)
tempmatrix.setcol(0, rect)
tempmatrix.setcol(1, recv)
tempmatrix.fprint(savdata, " %g")
savdata.close() 
But after exceeding 10000 msec the time in the datafile result.dat becomes indistinct:

10000.1 -62.6805
10000.1 -62.6806
10000.1 -62.6808
10000.1 -62.681

How can I increase the precision of the exported data?

Thanks a lot!
Chris
ted
Site Admin
Posts: 6394
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: Increase precision

Post by ted »

Use f conversion instead of g, and specify the field width and number of significant digits explicitly. Example:
printf("%10.3f\n", PI)
prints

Code: Select all

     3.142
i.e. numeric value is printed with three decimal place precision in a field 10 spaces wide. The decimal point counts as a one of the 10 spaces, and so does the - sign if the number is < 0.

Read more about printf and conversion strings here:
http://www.neuron.yale.edu/neuron/stati ... tml#printf
For details of printf syntax and usage examples, see documentation of printf in any book on C or C++ programming. One of my favorites is C: A Reference Manual, by Harbison and Steele
http://www.careferencemanual.com/, now in its 5th edition. But remember that NEURON's printf etc. are "Only a subset of the C standard library functions."
Post Reply