%1.1f on 45.78 is 45.8 ??

  • Thread starter karthikbalaguru
  • Start date
K

karthikbalaguru

Hi,

In the below code , i get the output as
45.8 if i specify the printf format specifier
as %1.1f. But, how ?

int main(void)
{
float b = 45.78;
printf("%1.1f",b);
return 0;
}

Shouldn't it throw an error for trying to print
45.8 in %1.1f format ?
Any ideas ?

Thx in advans,
Karthik Balaguru
 
K

karthikbalaguru

Hi,

In the below code , i get the output as
45.8 if i specify the printf format specifier
as %1.1f. But, how ?

int main(void)
{
    float b = 45.78;
    printf("%1.1f",b);
    return 0;

}

Shouldn't it throw an error for trying to print
45.8 in %1.1f format ?
Any ideas ?

I also have the #include<stdio.h> at the top of the c file.

Karthik Balaguru
 
K

Kojak

Le Sun, 1 Mar 2009 08:20:13 -0800 (PST),
karthikbalaguru a écrit :
In the below code , i get the output as
45.8 if i specify the printf format specifier
as %1.1f. But, how ?

int main(void)
{
float b = 45.78;
printf("%1.1f",b);
return 0;
}

Shouldn't it throw an error for trying to print
45.8 in %1.1f format ?

The answer is: 42

Have you get the document pointed previously?

Again:

<http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1256.pdf>

Get it, read it (and keep it on hand) and all your questions
will find an answer.

Cheers,
 
B

Barry Schwarz

Hi,

In the below code , i get the output as
45.8 if i specify the printf format specifier
as %1.1f. But, how ?

int main(void)
{
float b = 45.78;
printf("%1.1f",b);
return 0;
}

Shouldn't it throw an error for trying to print
45.8 in %1.1f format ?
Any ideas ?

You are guessing. You need to read some reference.

What does the first "1" in the conversion specification mean? Given
the purpose of this part of the specification, does using 1 even make
sense?

What does the ".1" in the conversion specification mean? Does the
answer you see comply with this meaning?

You should be aware that very few floating point values can be
represented exactly (in either a float or a double). Even if you had
coded b=45.8, the value in b would not be 45.8. Given that 45.78 and
45.8 are represented as approximations, why does it surprise you the
printf rounds to satisfy the conversion specification?

Since almost all common decimal values are approximations, why would
you expect an error out of printf? How can it tell the difference
between 45.78000018 that represents 45.78 and 45.78000018 that
represents 45.78000018? And why do you think you are not allowed to
print either value with only one digit after the decimal?
 
K

Keith Thompson

karthikbalaguru said:
In the below code , i get the output as
45.8 if i specify the printf format specifier
as %1.1f. But, how ?

int main(void)
{
float b = 45.78;
printf("%1.1f",b);
return 0;
}

Shouldn't it throw an error for trying to print
45.8 in %1.1f format ?

Can you explain to us (a) why you think printf should throw an error
in this case, and (b) just what you mean by "throw an error"?
 
K

karthikbalaguru

Le Sun, 1 Mar 2009 08:20:13 -0800 (PST),
karthikbalaguru a écrit :




The answer is: 42

Have you get the document pointed previously?

Again:

  <http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1256.pdf>

Get it, read it (and keep it on hand) and all your questions
will find an answer.

Can you pls let me know the section of that document talks about this.
Does it come under point number 10 of section 7.19.6.1 ?

Thx in advans,
Karthik Balaguru
 
K

karthikbalaguru

Can you explain to us (a) why you think printf should throw an error
in this case, and (b) just what you mean by "throw an error"?

I thought that if the format specifier causes the lose
of lot of info, then it should not be supported and the printf
should convey by some forms of error statement.

On looking into the standards , i understand that
the compiler will give the correct value just as it does for
the below code.

int main(void)
{
int a,b,c;
scanf("%3d %3d %3d",&a,&b,&c);
printf("%d %d %d\n",a,b,c);
return 0;
}

If the input is - 123 456 789
The output for above code is -123 456 789


So, the below code also gives the output
as 45.8

int main(void)
{
float b = 45.78;
printf("%1.1f",b);
return 0;
}

This has been tackled by the standards in a different way
by giving the correct output instead of an error.
So, No problem.

Karthik Balaguru
 
K

Kojak

Le Sun, 1 Mar 2009 12:48:28 -0800 (PST),
karthikbalaguru a écrit :
Can you please let me know the section of that document talks about
this. Does it come under point number 10 of section 7.19.6.1 ?

Part 10 doesn't apply here (i.e. for your rounding problem).

See part 8 (p. 278) and 13 (p. 281) in section 7.19.6.1.

The rounding refer to IEEE 754/IEC 60559. Unfortunately, I don't
have pointer to this kind of document. Nevertheless you can refer
to annex F (p. 444).

That said, rounding is done to the "nearest value" as in your code
(45.78 is rounded to 45.8, this is what is expected in the nearest
value rounding mode).

Although this is not related directly to your question, section
5.2.4.2.2 can provide some hints on floats.

Hope I understood your question.

Truly,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,067
Latest member
HunterTere

Latest Threads

Top