what's with gcc?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2004
Posts: 7,596
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 711
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: what's with gcc?

 
1
  #11
May 14th, 2006
>I merely suggested that it could be a possibility.
It's never a possibility until you've exhausted all other options.

>So using %lf with printf is undefined behaviour?
In C89 it's undefined. In C99 the length modifier is quietly ignored because the committee realized this confusion was a very real problem.

>If %lu is okay why should %lf be not.
Because l is meant for integers, not floating-point.

>It sounds so natural.
Just because you see symmetry doesn't mean there is symmetry. What does %lf mean? Double? No, that's what %f means because varargs arguments use pre-C89 promotion rules. Long double? No, that's what %Lf is for. Why a capital L for long double? Because the committee realized that some moron would use %lf thinking it meant double, pass a double, and cause undefined behavior when printf treated it as a long double.

>The authors who wrote books on C and said %lf is a valid printf format specifier should be shot dead
Yes, but not for misleading you. They should be shot dead for trying to teach a language that they still haven't learned properly.

>Dave Sinkula, Narue, Ancient Dragon, Rashakifol and everyone else
>possesing a much better knowledge on C should be shot dead since they
>did not point this one out to me before
It's not our job to delint your programs. If it bothers you so much that we don't take the time to point out every little problem then go shell out $500 for PCLint and do it yourself.

>I believe I have used %lf with printf many a times before in this forum.
This is a problem that can easily be missed unless you're looking for it. Despite your obvious expectations, we're not capable of finding every little bug at a glance. I spend a lot of time looking closely at my code, referring to the standard, and using powerful tools like lint to catch as many problems as I can. I don't usually expend the same amount of effort on every piece of code posted to these forums by other people.

> With all that said my gcc still acts weird even if I use %f instead of %lf with printf
I use MinGW's 3.4.2 version of gcc, and I can't reproduce your problem. My educated guess would be that you really are hitting a floating-point rounding issue and it has nothing to do with gcc. What OS and processor do you use? Also, can you post a dozen or so iterations of the execution with the printf uncommented?
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: what's with gcc?

 
0
  #12
May 14th, 2006
Just for you, I downloaded and installed Mingw.

Guess what - nothing unusual happening here either, despite your broken code.

Anyway, I've cleaned it up a bit (stop it looping forever for example) and shown an example session - I suggest you do the same for a problem case as Narue suggests.

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. int main(void)
  5. {
  6. double x0,x1,a;
  7. int i;
  8.  
  9. x1 = 1;
  10. x0 = 0;
  11. printf("Find square root of: ");
  12. fflush(stdout);
  13. scanf("%lf",&a);
  14. for(i = 0; i < 100 && x0 != x1; ++i)
  15. {
  16. x0 = x1;
  17. x1 = .5 * (x1 + a / x1);
  18. #ifdef INCLUDE_PRINTF
  19. printf("Iteration %d - converging to %f - prev value %f\n",i,x1,x0);
  20. #endif
  21. }
  22.  
  23. printf("Finished in %d iterations\n",i);
  24. return 0;
  25. }
  26.  
  27.  
  28. $ gcc -v
  29. Reading specs from c:/mingw/bin/../lib/gcc/mingw32/3.4.2/specs
  30. Configured with: ../gcc/configure --with-gcc --with-gnu-ld --with-gnu-as --host=mingw32 --target=mingw32 --prefix=/mingw --enable-threads --disable-nls --enable-languages=c,c++,f77,ada,objc,java --disable-win32-registry --disable-shared --enable-sjlj-exceptions --enable-libgcj --disable-java-awt --without-x --enable-java-gc=boehm --disable-libgcj-debug --enable-interpreter --enable-hash-synchronization --enable-libstdcxx-debug
  31. Thread model: win32
  32. gcc version 3.4.2 (mingw-special)
  33. $ gcc -W -Wall -ansi -pedantic -O2 -DINCLUDE_PRINTF prog.c
  34. $ ./a.exe
  35. Find square root of: 5.7
  36. Iteration 0 - converging to 3.350000 - prev value 1.000000
  37. Iteration 1 - converging to 2.525746 - prev value 3.350000
  38. Iteration 2 - converging to 2.391253 - prev value 2.525746
  39. Iteration 3 - converging to 2.387470 - prev value 2.391253
  40. Iteration 4 - converging to 2.387467 - prev value 2.387470
  41. Iteration 5 - converging to 2.387467 - prev value 2.387467
  42. Iteration 6 - converging to 2.387467 - prev value 2.387467
  43. Finished in 7 iterations

My next guess is you have some crusty old pentium with an uncorrected FDIV bug. Other than that, I can't reproduce the problem so unless you post more details, there's not a lot else to do.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 353
Reputation: Asif_NSU is on a distinguished road 
Solved Threads: 3
Asif_NSU's Avatar
Asif_NSU Asif_NSU is offline Offline
Posting Whiz

Re: what's with gcc?

 
1
  #13
May 15th, 2006
Thanks everyone for being so patient with me, especially Salem who actually downloaded MinGW gcc. I really appreciate that!

Here's the screen capture of what I am getting. First I compile the program with the printf statement commented. In this case the loop never ends. I had to Ctrl + C to kill it.

In the next case I uncomment the printf statement and the program works as expected.

The issue is only with MinGW gcc version 3.4.2. I have tested the code with lcc-win32, visual C++ 6.0 and Borland C++ 5.5.

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. int main(void)
  5. {
  6. double x0, x1, a;
  7. int i;
  8. x1 = 1;
  9. x0 = 0;
  10. printf("\nFind square root of: ");
  11. scanf("%lf",&a);
  12.  
  13. for(i = 0; x0 != x1; ++i)
  14. {
  15. x0 = x1;
  16. x1 = 0.5 * (x1 + a / x1);
  17. printf("\nIteration %i: x1 = %.15f x0 = %.15f", i, x0, x1);
  18. }
  19.  
  20. printf("\nAnswer: %.15f\n",x1);
  21. return 0;
  22. }

Unzip and open the gcc.html file with your browser. You will see it yourselves.
Attached Files
File Type: zip gcc.zip (736.4 KB, 6 views)
"He who mixes with people and endures the harm they do is better than he who does not mix and endures." (Tirmidhi)
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 353
Reputation: Asif_NSU is on a distinguished road 
Solved Threads: 3
Asif_NSU's Avatar
Asif_NSU Asif_NSU is offline Offline
Posting Whiz

Re: what's with gcc?

 
1
  #14
May 15th, 2006
Just because you see symmetry doesn't mean there is symmetry. What does %lf mean? Double? No, that's what %f means because varargs arguments use pre-C89 promotion rules. Long double? No, that's what %Lf is for. Why a capital L for long double? Because the committee realized that some moron would use %lf thinking it meant double, pass a double, and cause undefined behavior when printf treated it as a long double.
%lf is valid for scanf(), so it is natural for anyone to think it is also valid for printf(). Things like this is easy to miss unless someone explicitly mentions it.

Now I want to know how much does this C and C++ standard costs? Or can you direct me to a modern book on C(not C++), cos I am thinking of getting a book that really teaches these subtle points. K&R, I guess, has become quite outdated.
"He who mixes with people and endures the harm they do is better than he who does not mix and endures." (Tirmidhi)
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,596
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 711
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: what's with gcc?

 
0
  #15
May 15th, 2006
>%lf is valid for scanf()
Yes, because scanf expects a pointer, and pointers must be appropriately typed. printf doesn't require a pointer, and default promotions apply.

>Things like this is easy to miss unless someone explicitly mentions it.
I agree, but C is a subtle language. Why do you think it takes so many years to become proficient with it?

>Now I want to know how much does this C and C++ standard costs?
$18 for the PDF version at www.ansi.org, or about $60 for the Wiley hardcopy, or about $200 for the official ANSI hardcopy.

>Or can you direct me to a modern book on C
A good reference that makes few mistakes is C: A Reference Manual (5th Edition) by Harbison and Steele. It's a nice alternative to the standard if you can't take hundreds of pages of legalese. But there are a few minor points (that should only come up in a debate of pedantry) where C: A Reference Manual is not entirely accurate.

>K&R, I guess, has become quite outdated.
Not really. K&R is still my most referenced book on C. I'm on my 3rd copy because the other two fell apart from overuse, and the binding is excellent.

>You will see it yourselves.
My guess remains the same. First fix your floating-point comparison so that it uses a fudge factor. If the problem remains, post the code you used. Since we can't reproduce your problem, the only way we can help is to offer suggestions and make sure that your code is 100% correct for troubleshooting.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 353
Reputation: Asif_NSU is on a distinguished road 
Solved Threads: 3
Asif_NSU's Avatar
Asif_NSU Asif_NSU is offline Offline
Posting Whiz

Re: what's with gcc?

 
1
  #16
May 15th, 2006
First fix your floating-point comparison so that it uses a fudge factor.
First you explain to me how come uncommenting a printf() statement makes the loop end. This simply doesn't make any sense.

Within a few iterations the new value of x1 differs so small from the prev value of x1 that it is well beyond the precision of 15 decimal places. x1 is then unchanged. So a match between x0 and x1 should be true(since I do a x0 = x1). The loop should exit. It works with all the compilers. Even you people could not reproduce the problem.

Not really. K&R is still my most referenced book on C
Oh yeah it is. K&R IS outdated. K&R was written around C89 standard. It doesn't teach you the features available in C99 . You can still use it as a reference because most of the compilers still haven't implemented all of C99. So that you can tell people,"Hey not all compilers have implemented C99 fully, so don't use this or that; your code has undefined behaviour according to C89, your code is broken according to C89 etc. etc." In my view that book is outdated.
"He who mixes with people and endures the harm they do is better than he who does not mix and endures." (Tirmidhi)
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,596
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 711
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: what's with gcc?

 
0
  #17
May 15th, 2006
>First you explain to me how come uncommenting a printf() statement makes the loop end.
No, fix your damn code first. Then we'll talk about weird things happening. If you want help, stop being a prick about the help you're getting. If you want to be a smartass, we'll be happy to offer you a cold shoulder.

>You can still use it as a reference because most of the compilers still haven't implemented all of C99.
When you're intimately familiar with both C89 and C99, then you're welcome to debate the validity of K&R's content. Until then, take my word for it.

>In my view that book is outdated.
In my view you lack the foundation to make that judgement.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 353
Reputation: Asif_NSU is on a distinguished road 
Solved Threads: 3
Asif_NSU's Avatar
Asif_NSU Asif_NSU is offline Offline
Posting Whiz

Re: what's with gcc?

 
1
  #18
May 16th, 2006
No, fix your damn code first.
If you run the loop for say 50 times that's enough most of the time to get the desired result. So instead of using x1 != x0, I would use the following code
  1. for(i = 0; i < 50; ++i)
  2. x = .5 * (x + a / x);

But the number of iteration that is required depends very much on the value of a, for some it would require 5 iterations and for some it would take 19. So why should I run the code 50 times for each value of a. That's not very elegant IMHO.

The second method that came into my mind was to use fabs(a - x1 * x1) > 0.00000000000001 to make the loop continue until it reaches 15 decimal precision. Then again for the most of the values of a it goes into a infinite loop. I guess that's quite expected and I should use something like this: fabs(fabs(a - x1 * x1) - 0.00000000000001) )> (FUDGE_FACTOR). I am just not quite sure what to use for the fudge factor so that I do get the precision upto 15 decimal places.
Originally I was solving the follwing problem
x_0 = 1, x_{i + 1} = \frac{1}{2}(x_i + \frac{a}{x_i})

x_i \rightarrow \sqrt{a} as  i \rightarrow \infty

I thought x0 != x1 would be the best way to simulate i \rightarrow \infty.
"He who mixes with people and endures the harm they do is better than he who does not mix and endures." (Tirmidhi)
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: what's with gcc?

 
0
  #19
May 16th, 2006
Can't you find a better way of conveying your results other than some bloated shockwave flash file, which by the way I've no intention of running while I'm at work.

Does the enhanced exit condition in my example code loop forever on your machine as well?
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 353
Reputation: Asif_NSU is on a distinguished road 
Solved Threads: 3
Asif_NSU's Avatar
Asif_NSU Asif_NSU is offline Offline
Posting Whiz

Re: what's with gcc?

 
1
  #20
May 16th, 2006
Does the enhanced exit condition in my example code loop forever on your machine as well?
This is what happens when I use your code. The effect is same as mine. This time it's in GIF format. Hope that's okay with you.
Attached Thumbnails
Salem_gcc.gif  
"He who mixes with people and endures the harm they do is better than he who does not mix and endures." (Tirmidhi)
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC