473,801 Members | 2,240 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 14553
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

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

iD8DBQFAqPpIagV FX4UWr64RApR5AJ 9tUJX+9j50vtrBa Ut7zROkwKbkdACe L+/m
DF4Mo3cFDbqmVAw I4ekW7R8=
=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)

iD8DBQFAqP72agV FX4UWr64RAvXKAK Cl8sn9Tpr1kM3D8 fsH2zE0gZb56ACd HepF
PE66iwK0pwleh9C kMZBtaoE=
=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)

iD8DBQFAqP6iagV FX4UWr64RAollAK CugSLdkGM+0jzi5 fOVUy+ihKV3lQCg iKMH
WYbGHjocD3vDI9E u1XiLI6w=
=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********@yah oo.com) (cb********@wor ldnet.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********@yah oo.com) (cb********@wor ldnet.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**********@b alena.cs.interb usiness.it> =?UTF-8?B?IkRhcmlvICh kcmlua2luZyBjb+ +sgGVlIGluIHRoZ SBv76yDY2XigKYp Ig==?= <da***@despamme d.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

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

Similar topics

26
2529
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 used to stop input), and i have to print 8 integers per line. The code that i have works, and does exactly that, but i feel a little uneasy because i am using two breaks. I would like to hear any comments about this approach, and what i might do to...
81
7360
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 be any advantage in having strcat and strcpy return a pointer to the "end" of the destination string rather than returning a
3
3000
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
7600
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
2763
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 & \ !(v)->vcpu_info->evtchn_upcall_mask) whereas evtchn_upcall_pending is of type unsigned char (and also evtchn_upcall_mask is of type unsigned char).
12
43475
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: printf("\n%20s\t%7u\t%13i\t%13i","signed int",sizeof(signed int),INT_MIN,INT_MAX);
10
5361
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 it a fair statement that char *ptr will take 4 more bytes (on 32bit platform) in DATA segment? I have found
17
2428
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: #include <stdlib.h> #include<stdio.h> #define MAX 1000000000
9
2861
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 (Array == 'APPLE'){ do something; } or do I need to use a different method to check?
3
2999
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 solve it? I know it may be noob mistakes, but this is my first program ;-) main(){ int Operando_1; int Operando_2; int Suma; int Diferencia; int Producto; void clrscr()
0
9558
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10277
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10055
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7594
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6833
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5489
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3785
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2963
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.