Author |
Message |
Henrique Seganfred #1 / 21
|
 strange behaviour.....
whats going on with the following code: #include <stdio.h> void main(void){ float i; i=0.1; for(i;i<1000;i++){ printf("%f", i); } Quote: }
(it will output something like: 0.1, 1.1, 1.1, 1.19999!!! ) note 1:after many for's, a significant difference will be noticed....this happend with me when I was doing a program to solve integrals through computational methods... note 2: putting the variable in my compiler IDE watch window the data seems to be ok...only when outputting it to the screen using the printf() function the modified data is shown...unless I ask to printf to use only 2 or three decimal places for float (%.2f) thats about it.. :-) Henrique Seganfredo --
|
Sat, 16 Feb 2002 03:00:00 GMT |
|
 |
wtmitch.. #2 / 21
|
 strange behaviour.....
Quote: > whats going on with the following code: <snip> > (it will output something like: 0.1, 1.1, 1.1, 1.19999!!! ) >From the comp.lang.c FAQ:
14.1: When I set a float variable to, say, 3.1, why is printf() printing it as 3.0999999? A: Most computers use base 2 for floating-point numbers as well as for integers. In base 2, 1/1010 (that is, 1/10 decimal) is an infinitely-repeating fraction: its binary representation is 0.0001100110011... . Depending on how carefully your compiler's binary/decimal conversion routines (such as those used by printf) have been written, you may see discrepancies when numbers (especially low-precision floats) not exactly representable in base 2 are assigned or read in and then printed (i.e. converted from base 10 to base 2 and back again). See also question 14.6. Sent via Deja.com http://www.deja.com/ Share what you know. Learn what you don't. --
|
Sun, 17 Feb 2002 03:00:00 GMT |
|
 |
Francis Glassboro #3 / 21
|
 strange behaviour.....
Quote: >#include <stdio.h> >void main(void){ > float i; > i=0.1; > for(i;i<1000;i++){
If this is really what you wrote your compiler is broken because there is no increment operator defined for floats. Quote: > printf("%f", i); > } >}
Francis Glassborow Journal Editor, Association of C & C++ Users 64 Southfield Rd Oxford OX4 1PA +44(0)1865 246490 All opinions are mine and do not represent those of any organisation --
|
Sun, 17 Feb 2002 03:00:00 GMT |
|
 |
Jens Schweikhard #4 / 21
|
 strange behaviour.....
#>#include <stdio.h> #>void main(void){ #> float i; #> i=0.1; #> for(i;i<1000;i++){ # If this is really what you wrote your compiler is broken because there # is no increment operator defined for floats. Hmm. ISO 9899 6.3.2.4 says postfix ++ wants a modifiable scalar type. I'd say a float like the above is a perfect example. Hey, ++ even works for pointers, try it one day ;-) :-) :-O Maybe you mixed up ++ with % ? Regards, Jens -- Jens Schweikhardt http://www.schweikhardt.net/ SIGSIG -- signature too long (core dumped) --
|
Sun, 17 Feb 2002 03:00:00 GMT |
|
 |
kgol #5 / 21
|
 strange behaviour.....
Quote:
> > float i; > > i=0.1; > > for(i;i<1000;i++){ > If this is really what you wrote your compiler is broken because there > is no increment operator defined for floats.
Are you sure? According to H&S p-200 "the operands [of ++ and --] may be of any scalar type" and p-110 Table 5-1 shows float as a scalar type. FWIW, the AIX C compiler handles this with no error. --
--
|
Sun, 17 Feb 2002 03:00:00 GMT |
|
 |
Dave Hans #6 / 21
|
 strange behaviour.....
On Wed, 01 Sep 1999 06:15:02 GMT, Francis Glassborow Quote:
>>#include <stdio.h> >>void main(void){ >> float i; >> i=0.1; >> for(i;i<1000;i++){ >If this is really what you wrote your compiler is broken because there >is no increment operator defined for floats.
I thought ++ and -- worked for all scalar types, not just integral ones? Regards, -=Dave Just my (10-010) cents I can barely speak for myself, so I certainly can't speak for B-Tree. Change is inevitable. Progress is not. --
|
Sun, 17 Feb 2002 03:00:00 GMT |
|
 |
Adam Sprag #7 / 21
|
 strange behaviour.....
Quote:
> If this is really what you wrote your compiler is broken because there > is no increment operator defined for floats.
*is amazed* Never knew that before. Had a look in the FAQ there's nothing about it there, and according to K&R2, the only restriction is that the operator must be applied to an lvalue (variable). Quote: >From p.46 "The increment and decrement operators can only be applied to
variables; an expression like (i+j)++ is illegal." I'd have thought that they'd have said 'integer variables' there if inc/dec on floats was illegal. A similar implication is made on p.203, where the only listed requirement for operators ++ and -- is that they are applied to an lvalue. I realise that K&R is not the standard, and that Appendix A in the back is merely a description of the standard, but I'd have thought that that's quite an omission. Almost worth an entry on the 'errata' page. I also realise that the code a compiler needs to generate to increment a float would not nearly be as efficient on many architectures as that to increment an integer, due to the (universal?) integer increment instruction being more prevalent than the (non-existent?) float increment instruction, but making the language increment and decrement operators meaningless when applied to floats does seem a little - unsymmetric. Truly surprised, Adam Spragg. -- Apparently [...] police in many lands are now complaining that local arrestees are insisting on having their Miranda rights read to them, just like perps in American TV cop shows. When it's explained to them that they are in a different country, where those rights do not exist, they become outraged. Starsky and Hutch reruns, dubbed into diverse languages, may turn out, in the long run, to be a greater force for human rights than the [United States] Declaration of Independence. -- Neal Stephenson (Cryptonomicon - http://www.io.com/~mccoy/beginning_print.html) ---------------- The opinions expressed in this email are mine alone, and do not neccesarily represent those of my employer, my parents, or the people who wrote the email software I use. --
|
Sun, 17 Feb 2002 03:00:00 GMT |
|
 |
Kevin D. Quit #8 / 21
|
 strange behaviour.....
On Tue, 31 Aug 1999 20:11:56 GMT, "Henrique Seganfredo" Quote:
>(it will output something like: 0.1, 1.1, 1.1, 1.19999!!! )
Let me suggest you read ftp://ftp.quitt.net/outgoing/goldberg1.pdf to learn about floating point numbers. -- #include <standard.disclaimer> _ Kevin D Quitt USA 91351-4454 96.37% of all statistics are made up Per the FCA, this email address may not be added to any commercial mail list --
|
Sun, 17 Feb 2002 03:00:00 GMT |
|
 |
Martin Ambuh #9 / 21
|
 strange behaviour.....
Quote:
> whats going on with the following code: > #include <stdio.h> > void main(void){
^^^^ How could you do this? As a good usenet citizen, you have lurked in the newsgroup and checked the FAQ before posting. You KNOW this is wrong. Quote: > float i; > i=0.1; > for(i;i<1000;i++){ > printf("%f", i); > } > } > (it will output something like: 0.1, 1.1, 1.1, 1.19999!!! )
5 is not a power of 2. As a good usenet citizen, you have already checked the backtaffic on the newsgroup with a tool like dejanews, so you already know the answer. --
__________________________________________________________ Fight spam now! Get your free anti-spam service: http://www.brightmail.com --
|
Sun, 17 Feb 2002 03:00:00 GMT |
|
 |
Thomas Gagn #10 / 21
|
 strange behaviour.....
The increment operator (I thought) only incremented integers and not floats. What would (should) it mean to increment a float? Should it increase by 1? 0.1? 0.01? I would expect the answer should be 1.0, though I'm not positive. For it to be anything else would lead to certain confusion. -- Open Source middleware available at http://pweb.netcom.com/~tgagne --
|
Sun, 17 Feb 2002 03:00:00 GMT |
|
 |
Alex_K.. #11 / 21
|
 strange behaviour.....
Quote: > whats going on with the following code: > #include <stdio.h> > void main(void){ > float i; > i=0.1; > for(i;i<1000;i++){ > printf("%f", i); > } > } > (it will output something like: 0.1, 1.1, 1.1, 1.19999!!! ) > note 1:after many for's, a significant difference will be > noticed....this happend with me when I was doing > a program to solve integrals through computational methods... > note 2: putting the variable in my compiler IDE watch window the data seems > to be ok...only when outputting it to the screen using the printf() function > the modified data is shown...unless I ask to printf to use only 2 or three > decimal places for float (%.2f) > thats about it.. :-)
Nothing strange at all; remember that most real numbers can't be represented in binary _exactly_. Floating point representation is nearly always an approximation. In your specific case the problem is aggravated because you are using float and not more precise double. -- Regards, Alex Krol Disclaimer: I'm not speaking for Scitex Corporation Ltd Sent via Deja.com http://www.deja.com/ Share what you know. Learn what you don't. --
|
Sun, 17 Feb 2002 03:00:00 GMT |
|
 |
Martin Ambuh #12 / 21
|
 strange behaviour.....
Quote:
> > If this is really what you wrote your compiler is broken because there > > is no increment operator defined for floats. > *is amazed* > Never knew that before.
It's good that you didn't know this untruth. --
__________________________________________________________ Fight spam now! Get your free anti-spam service: http://www.brightmail.com --
|
Sat, 23 Feb 2002 03:00:00 GMT |
|
 |
Francis Glassboro #13 / 21
|
 strange behaviour.....
Quote: >Hmm. ISO 9899 6.3.2.4 says postfix ++ wants a modifiable scalar type. >I'd say a float like the above is a perfect example. Hey, ++ even works >for pointers, try it one day ;-) :-) :-O
Thanks. Curiously that was a hole in my thinking. I would never even dream of using increment and decrement operators with floating point types because, from my perspective, they serve no useful purpose. Integral and pointer types are a different issue (and in both these cases the operators often map to a machine code instruction). Actually this highlights a common problem, if we _know_ something is _useless_ we never consider it until our ignorance hits us. Francis Glassborow Journal Editor, Association of C & C++ Users 64 Southfield Rd Oxford OX4 1PA +44(0)1865 246490 All opinions are mine and do not represent those of any organisation --
|
Sat, 23 Feb 2002 03:00:00 GMT |
|
 |
Francis Glassboro #14 / 21
|
 strange behaviour.....
Quote: >> (it will output something like: 0.1, 1.1, 1.1, 1.19999!!! )
Reference my earlier ignorance. The above claim is what compounded my ignorance. According to my belated reading of 9899:1990 the output should be: 0.1, 1.1, 2.1, 3.1, etc. Or am I still wrong? Quote: True, but it does not seem to relate to the sequence he claimed was produced. Quote: >As a good usenet citizen, you have already checked the backtaffic on the >newsgroup with a tool like dejanews, so you already know the answer.
Francis Glassborow Journal Editor, Association of C & C++ Users 64 Southfield Rd Oxford OX4 1PA +44(0)1865 246490 All opinions are mine and do not represent those of any organisation --
|
Sat, 23 Feb 2002 03:00:00 GMT |
|
|