printf("%.2f\n", 90.625)

G

Guest

The following program

int main(){printf("%.2f\n", 90.625);}

what it must print out?

I try it on several compiler/operating system
and I obtaing different results.

On some compilers it writes 90.62,
but with other compilers it writes 90.63.

- Dario
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Dario (drinking co
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFAqPpIagVFX4UWr64RApR5AJ9tUJX+9j50vtrBaUt7zROkwKbkdACeL+/m
DF4Mo3cFDbqmVAwI4ekW7R8=
=5AwR
-----END PGP SIGNATURE-----
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

OK, that was wierd. Thunderbird definitely messed that one up.

I'll try again...

Dario (drinking co
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFAqP6iagVFX4UWr64RAollAKCugSLdkGM+0jzi5fOVUy+ihKV3lQCgiKMH
WYbGHjocD3vDI9Eu1XiLI6w=
=0uhC
-----END PGP SIGNATURE-----
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Third time's a charm??
The following program

int main(){printf("%.2f\n", 90.625);}

what it must print out?

See the C faq (http:/www.faqs.org/) question 14.1



- --
Lew Pitcher
IT Consultant, Enterprise Application Architecture,
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed are my own, not my employers')
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFAqP72agVFX4UWr64RAvXKAKCl8sn9Tpr1kM3D8fsH2zE0gZb56ACdHepF
PE66iwK0pwleh9CkMZBtaoE=
=jVzY
-----END PGP SIGNATURE-----
 
C

CBFalconer

Lew said:
See the C faq (http:/www.faqs.org/) question 14.1

The binary value is: 1011010.101 with . representing the binary
point. The fractional portion represents 5/8. The whole thing
only needs 10 significant bits. So the OPs question is about how
his printf interpreter does its precision rounding, which is a
matter for the particular implementation. This is almost
certainly outside the FP implementation.

I don't know just where this dog buried its bone.
 
G

Guest

Lew said:
See the C faq (http:/www.faqs.org/) question 14.1

Question 14.1 is not related to my question.
The number 90.625 is exactly represented with a double value.

My question was:
Why printf("%.2f\n", 90.625) prints 90.62 or 90.63
depending on compiler ?

Using GCC it writes 90.62.
Using Microsoft compiler it writes 90.63.

What is the correct result?

- Dario
 
C

CBFalconer

Dario (drinking co?ee in the o?ce…) said:
.... snip ...

My question was:
Why printf("%.2f\n", 90.625) prints 90.62 or 90.63
depending on compiler ?

Using GCC it writes 90.62.
Using Microsoft compiler it writes 90.63.

What is the correct result?

I already answered this. However, my guess is that Microsoft
lazily does rounding up, while what ever library you are using for
gcc rounds to even.
 
D

Dan Pop

In said:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Could you, please, drop this junk from your posts. It's annoying...

Let's see what the standard says:

The value is rounded to the appropriate number of digits.

But it doesn't provide any rounding algorithm. So, you can realistically
expect both 90.62 and 90.63, depending on the implementor's preference.

OTOH, calling printf with no prototype declaration in scope is undefined
behaviour in C89, so the program in question could print *anything*.
See the C faq (http:/www.faqs.org/) question 14.1

Bogus advice: 0.625 = 1/2 + 1/8 and all "small" integers are exactly
represented in (binary) floating point.

Dan
 
G

Guest

Dan said:
Could you, please, drop this junk from your posts. It's annoying...
Seconded.


Let's see what the standard says:

The value is rounded to the appropriate number of digits.

But it doesn't provide any rounding algorithm. So, you can realistically
expect both 90.62 and 90.63, depending on the implementor's preference.

Just to clarify:
I can expect both 90.56 and 90.57 even from the following program?

#include <stdio.h>
int main(int argc, char*argv[])
{
printf("%.2f\n", 90.5625);
return 0;
}
OTOH, calling printf with no prototype declaration in scope is undefined
behaviour in C89, so the program in question could print *anything*.

Obviously!
In my original post I over-simplified the example
just to demonstrate my problem.

- Dario
 
D

Dan Pop

In said:
Just to clarify:
I can expect both 90.56 and 90.57 even from the following program?

#include <stdio.h>
int main(int argc, char*argv[])
{
printf("%.2f\n", 90.5625);
return 0;
}

I think so, but QoI (quality of implementation) considerations would
effectively rule out 90.57.

Dan
 
C

CBFalconer

Dario (drinking co?ee in the o?ce…) said:
Dan said:
Let's see what the standard says:

The value is rounded to the appropriate number of digits.

But it doesn't provide any rounding algorithm. So, you can
realistically expect both 90.62 and 90.63, depending on the
implementor's preference.

Just to clarify:
I can expect both 90.56 and 90.57 even from the following program?

#include <stdio.h>
int main(int argc, char*argv[])
{
printf("%.2f\n", 90.5625);
return 0;
}

No. 0.0025 is obviously less than 0.005, so should round down.
 
D

Dan Pop

In said:
Dario (drinking co?ee in the o?ce…) said:
Dan said:
Dario wrote:

int main(){printf("%.2f\n", 90.625);}

what it must print out?

Let's see what the standard says:

The value is rounded to the appropriate number of digits.

But it doesn't provide any rounding algorithm. So, you can
realistically expect both 90.62 and 90.63, depending on the
implementor's preference.

Just to clarify:
I can expect both 90.56 and 90.57 even from the following program?

#include <stdio.h>
int main(int argc, char*argv[])
{
printf("%.2f\n", 90.5625);
return 0;
}

No. 0.0025 is obviously less than 0.005, so should round down.

"Should" is a bit too strong. Rounding to the nearest integer is not the
only rounding algorithm invented or used. Furthermore, the comp.std.c
people say that the rounding direction is under program control (in C99).

Dan
 

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
474,142
Messages
2,570,819
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top