[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
## Re: Problem with precision

**From**: |
Martijn |

**Subject**: |
Re: Problem with precision |

**Date**: |
Thu, 16 Jun 2011 17:15:59 +0200 |

Hi Fritz,
Try formatted output:
f = fopen("myfile.txt", "w");
fprintf(f, "%.3f\n", mydata);
fclose(f);
The string "%.3f\n" is the formatting string, saying that every number
is to be written als fixed point with 3 decimal, and is to be followed
by a newline.
To write in two columns with a tab as separation or with a fixed column
width use: "%.3f\t%.3f\n" or "%8.3f%12.3f\n".
Beware that to write a file with 2 columns the data matrix needs to have
two rows. So if:
mydata = [x1, y1; x2, y2; ...; xn, yn]
then use
fprintf(f, "%.3f\t%.3f\n", mydata');
Notice the conjugation! This is a quirk that octave has to be compatible
with Matlab.
good luck,
Martijn
On Thu, 2011-06-16 at 16:34 +0200, Fritz Fischer wrote:
>* Hello!*
>* *
>* I have a problem with Octave's precision and hope that you can help*
>* me.*
>* *
>* My function writes data to a file, usually using a normal precision.*
>* But some*
>* values are written with a very high precision and in the exponential*
>* format:*
>* *
>* 44.875*
>* 40.262*
>* 35.327*
>* 30.005*
>* 20.981*
>* 6.9638e-28*
>* 0*
>* 0*
>* *
>* Can someone tell me how to fix this? I tried format short and format*
>* long*
>* but the output is the same at both settings.*
>* *
>* Since the files are used as input for another program that does not*
>* understand the exponential format I need to change this.*
>* *
>* Kind regards,*
>* Walter*
>* *
>* *
>* *
>* *
>* *
>* _______________________________________________*
>* Help-octave mailing list*
>* address@hidden*
>* https://mailman.cae.wisc.edu/listinfo/help-octave*