473,325 Members | 2,342 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

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

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
Nov 14 '05 #1
11 14513
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

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

iD8DBQFAqPpIagVFX4UWr64RApR5AJ9tUJX+9j50vtrBaUt7zR OkwKbkdACeL+/m
DF4Mo3cFDbqmVAwI4ekW7R8=
=5AwR
-----END PGP SIGNATURE-----
Nov 14 '05 #2
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Third time's a charm??

Dario wrote:
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)

iD8DBQFAqP72agVFX4UWr64RAvXKAKCl8sn9Tpr1kM3D8fsH2z E0gZb56ACdHepF
PE66iwK0pwleh9CkMZBtaoE=
=jVzY
-----END PGP SIGNATURE-----
Nov 14 '05 #3
-----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-----
Nov 14 '05 #4
Lew Pitcher wrote:
Dario wrote:
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


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.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #5
Lew Pitcher wrote:
Dario wrote:
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


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
Nov 14 '05 #6
"Dario (drinking co?ee in the o?ce…)" wrote:
.... 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.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #7
In <L9********************@news20.bellglobal.com> Lew Pitcher <Le*********@td.com> writes:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Could you, please, drop this junk from your posts. It's annoying...
Dario wrote:
The following program

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.

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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #8
Dan Pop wrote:
In <L9********************@news20.bellglobal.com> Lew Pitcher <Le*********@td.com> writes:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Could you, please, drop this junk from your posts. It's annoying...


Seconded.
Dario wrote:
The following program

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;
}
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
Nov 14 '05 #9
In <c8**********@balena.cs.interbusiness.it> =?UTF-8?B?IkRhcmlvIChkcmlua2luZyBjb++sgGVlIGluIHRoZSBv76 yDY2XigKYpIg==?= <da***@despammed.com> writes:
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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #10
"Dario (drinking co?ee in the o?ce…)" wrote:
Dan Pop wrote:
Lew Pitcher <Le*********@td.com> writes:
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.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

Nov 14 '05 #11
In <40***************@yahoo.com> CBFalconer <cb********@yahoo.com> writes:
"Dario (drinking co?ee in the o?ce…)" wrote:
Dan Pop wrote:
Lew Pitcher <Le*********@td.com> writes:
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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
by: Chris Potter | last post by:
Hello everyone. I am taking my first course in C and in one of my assignments i need to print out an array that could have anywhere from 0 to 100 positive integers in it (a negative integer is...
81
by: Matt | last post by:
I have 2 questions: 1. strlen returns an unsigned (size_t) quantity. Why is an unsigned value more approprate than a signed value? Why is unsighned value less appropriate? 2. Would there...
3
by: Leo Nunez | last post by:
Hello! I need copy from structure "A" to "B" that contains "strings" in a one line code. Me problem like this : typedef struct tHeader{ char field1; char field2; char field3;
8
by: vijay | last post by:
Hello, As the subject suggests, I need to print the string in the reverse order. I made the following program: # include<stdio.h> struct llnode { char *info;
43
by: markryde | last post by:
Hello, I saw in some open source projects a use of "!!" in "C" code; for example: in some header file #define event_pending(v) \ (!!(v)->vcpu_info->evtchn_upcall_pending & \...
12
by: Zero | last post by:
Hi everybody, i want to write a small program, which shows me the biggest and smallest number in dependance of the data type. For int the command could be: ...
10
by: fei.liu | last post by:
Consider the following sample code char * ptr = "hello"; char carray = "hello"; int main(void){ } What does the standard have to say about the storage requirement about ptr and carray? Is...
17
by: Chen Shusheng | last post by:
Hi all, In fact, I want to let my memory run out. And see what will happen. My system is windowsXp. Memory is 256M.I think my cdes will apply more memory than I have. Codes are below: ...
9
by: chutsu | last post by:
hi I got a simple program, and I was wondering how do you check if the string in an array = a string. For example if I put "APPLE" in array Array then how can I check it with a if statement. if...
3
by: DrVitoti | last post by:
On that program the compiler says "parse error" on line 8, 10, 12 and 21, it also says "too many arguments" on lines 10, 12 and finally it says "at this port in" on lines 13, 14, 20 . How could I...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.