problem with precision when reading from file
Anna Sidera
sidera.anna@ucy.ac.cy
Sun Sep 19 17:27:00 GMT 2010
----- Original Message -----
From: Tim Prince <n8tm@aol.com>
Date: Thursday, September 16, 2010 4:18 pm
Subject: Re: problem with precision when reading from file
> On 9/16/2010 5:38 AM, Anna Sidera wrote:
> >
> > ----- Original Message -----
> > From: Tim Prince<n8tm@aol.com>
> > Date: Wednesday, September 15, 2010 6:07 pm
> > Subject: Re: problem with precision when reading from file
> >
> >> On 9/15/2010 7:37 AM, Anna Sidera wrote:
> >>> Hello,
> >>>
> >>> I wrote the following program:
> >>>
> >>>
> >>>
> >>> #include<stdio.h>
> >>> #include<math.h>
> >>> #include<stdlib.h>
> >>> #include<time.h>
> >>> #include<string.h>
> >>>
> >>> int main()
> >>> {
> >>>
> >>> FILE *fp;
> >>> fp = fopen("test.c", "r");
> >>> if (fp == NULL){
> >>> printf("Error opening input file\n");
> >>> exit(1);
> >>> }
> >>>
> >>> float var1, var2, var3;
> >>>
> >>> fscanf(fp,"%f",&var1);
> >>> fscanf(fp,"%f",&var2);
> >>> fscanf(fp,"%f",&var3);
> >>>
> >>> if (var1!=0.01) printf("var1 = %g\n",var1);
> >>> if (var2!=5) printf("var2 = %g\n",var2);
> >>> if (var3!=0.6) printf("var3 = %g\n",var3);
> >>>
> >>>
> >>>
> >>> Where test.c contains the following:
> >>>
> >>> 0.01
> >>> 5
> >>> 0.6
> >>>
> >>>
> >>>
> >>> When I run the program I get the following output:
> >>>
> >>> var1 = 0.01
> >>> var3 = 0.6
> >>>
> >>>
> >>>
> >>> I expected to get no output. Can you explain what is the problem?
> >>>
> >>> Thanks,
> >>> Anna
> >> Did you mean to try
> >>
> >> if (var1!=0.01f) printf("var1 = %g\n",var1);
> >> if (var3!=0.6f) printf("var3 = %g\n",var3);
> >>
> >> or possibly to convert var1 and var3 back to short char strings
> >> and compare those?
> >>
> >> 0.01 and 0.6 are constants of type double and will have typically
> >> 29 more bits precision in their
> >> conversion from decimal to binary representation than your
> >> conversions to type float.
> >>
> >>
> >>
> >>
> >> --
> >> Tim Prince
> >>
> >>
> >
> > I wrote a program that uses some input parameters. The program
> should read the input parameters from a file. There are also some
> input parameters that are arguments to main. Is there a way to
> read the input parameters so that for example var1*var2 gives a
> value equal to 0.05? What is the best way to read the input
> parameters?>
> > If var1 and var3 are float and 0.01 and 0.6 are long then why
> you mention short char?
> >
> > Anna
> I meant use sprintf() or such to make a string of char to see if
> it
> comes out to the desired value. Such a string conversion might be
> a
> possibility for your product test. It should be evident that you
> can't
> make an inexact float value come out identical to a double value
> resulting from a higher precision calculation, but maybe you
> haven't
> explained what you want to do. My initial impression was that you
> might
> have forgotten the distinction between float and double constants.
>
> --
> Tim Prince
>
>
Thank you for your help.
Tim, what I was trying to do is the following. I wrote a simulation program that reads some input parameters and gives some output parameters. I ran it for a set of input parameters and got the output. Then I changed some of the values of the input paramters and ran it again. I thought that I could guess the output for the second set of input parameters given the output for the first set of input parameters, but I was wrong. Then I found out about this precision problem.
Anna
More information about the Gcc-help
mailing list