printf %g

Hi.

I’m having a problem with precision on the printf("%g", …).

If I print out frequency to a file using,

{
float frequency;

frequency = (float)1.0 / 3.0;

fprint(filePtr, “%g\n”, frequency);
}

The value in the file for frequency is 0.333333

When I read the frequency back, the value is off by a little, which affect
the program values.

If I change the frequency in the file to 0.33333333, then the program gets
the correct value and works okay.

If I enter the time interval of 3 seconds and then compute frequency from
this, it always works.

Is there a way to change the precision for printf("%g", …)? I need the
nice formatting from the %g, so I don’t want to go to the %f.

Any way of working around this?

I have tried floats and doubles and I get the same type of problem.

TIA

Augie

Augie Henriques <augiehenriques@hotmail.com> wrote:

Hi.

I’m having a problem with precision on the printf("%g", …).

If I print out frequency to a file using,

{
float frequency;

frequency = (float)1.0 / 3.0;

fprint(filePtr, “%g\n”, frequency);
}

The value in the file for frequency is 0.333333

When I read the frequency back, the value is off by a little, which affect
the program values.

If I change the frequency in the file to 0.33333333, then the program gets
the correct value and works okay.

If I enter the time interval of 3 seconds and then compute frequency from
this, it always works.

Is there a way to change the precision for printf("%g", …)? I need the
nice formatting from the %g, so I don’t want to go to the %f.

Hello,

I don’t know if this is what you asking for but to get an output you want
you can change your fprintf statement to …"%.8g\n"…

Best Regards,

Marcin

Any way of working around this?

I have tried floats and doubles and I get the same type of problem.

TIA

Augie