C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

why nan and infs evaluate to true when used as if condition

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C Language (Moderated)
View previous topic :: View next topic  
Author Message
Ganny
Guest





PostPosted: Tue May 15, 2007 9:06 pm    Post subject: why nan and infs evaluate to true when used as if condition Reply with quote



#include <stdio.h>
int main() {
int a = 0;
if (1.0/0.0)
printf("result was true");
else
printf("result was false");
}

I get "result was true" in the compilers i tried - Is this the right
behaviour expected? shouldn't nans and infs treated as false?

thanks!
-ganesh
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
Francis Glassborow
Guest





PostPosted: Thu May 17, 2007 9:11 am    Post subject: Re: why nan and infs evaluate to true when used as if condit Reply with quote



Ganny wrote:
Quote:
#include <stdio.h
int main() {
int a = 0;
if (1.0/0.0)
printf("result was true");
else
printf("result was false");
}

I get "result was true" in the compilers i tried - Is this the right
behaviour expected? shouldn't nans and infs treated as false?

thanks!
-ganesh
Yes, but did the implementations you tested have NaNs and INFs? As it

stands it is undefined behaviour and so the program can generate any
output it likes not just the alternatives you provided.
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
Douglas A. Gwyn
Guest





PostPosted: Thu May 17, 2007 9:11 am    Post subject: Re: why nan and infs evaluate to true when used as if condit Reply with quote



Ganny wrote:
Quote:
if (1.0/0.0)
printf("result was true");
I get "result was true" in the compilers i tried - Is this the right
behaviour expected? shouldn't nans and infs treated as false?

NaNs and Infs aren't necessarily supported on all platforms;
dividing by 0.0 has undefined behavior in general.

Anyway, any not-precisely-zero value is treated as "true" in
C conditional contexts.
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
Jack Klein
Guest





PostPosted: Thu May 17, 2007 9:11 am    Post subject: Re: why nan and infs evaluate to true when used as if condit Reply with quote

On 15 May 2007 16:06:41 GMT, Ganny <sgganesh (AT) gmail (DOT) com> wrote in
comp.lang.c.moderated:

Quote:
#include <stdio.h
int main() {
int a = 0;
if (1.0/0.0)

Since the line above produces undefined behavior, the C standard has
nothing at all to say about what happens afterward.

Quote:
printf("result was true");
else
printf("result was false");
}

I get "result was true" in the compilers i tried - Is this the right
behaviour expected? shouldn't nans and infs treated as false?

An if() test of an arithmetic value is an explicit comparison with 0.

I would expect a floating point infinity to compare unequal with 0,
since it means a value to large in magnitude to be represented.
Mathematically, it is as far from 0 as it is possible to be for a
value of that type.

A NaN cannot compare equal to 0 because a NaN is required not to
compare equal to anything, even itself.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
Guest






PostPosted: Thu May 17, 2007 9:11 am    Post subject: Re: why nan and infs evaluate to true when used as if condit Reply with quote

Ganny <sgganesh (AT) gmail (DOT) com> wrote:
Quote:

shouldn't nans and infs treated as false?

No -- there's an implicit comparison to 0. Neither Inf nor NaN compares
equal to zero, so both are treated as true.

-Larry Jones

I don't need to do a better job. I need better P.R. on the job I DO.
-- Calvin
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
Hans-Bernhard Bröker
Guest





PostPosted: Thu May 17, 2007 9:11 am    Post subject: Re: why nan and infs evaluate to true when used as if condit Reply with quote

Ganny wrote:
Quote:
#include <stdio.h
int main() {
int a = 0;
if (1.0/0.0)
printf("result was true");
else
printf("result was false");
}

I get "result was true" in the compilers i tried - Is this the right
behaviour expected?

It is one of many right behaviours. The code causes undefined
behaviour. Which means there's strictly no way it can behave incorrectly.

Quote:
shouldn't nans and infs treated as false?

What makes you think that? Only zero is defined to be treated as false?
Why should NaN or Inf be considered zero?
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
jg
Guest





PostPosted: Thu May 17, 2007 9:11 am    Post subject: Re: why nan and infs evaluate to true when used as if condit Reply with quote

On May 15, 9:06 am, Ganny <sggan...@gmail.com> wrote:
Quote:
#include <stdio.h
int main() {
int a = 0;
if (1.0/0.0)
printf("result was true");
else
printf("result was false");

}

I get "result was true" in the compilers i tried - Is this the right
behaviour expected? shouldn't nans and infs treated as false?

I think you condition is to compare NaN with zero. Since the

representation of NaN isn't zero, your condition is true.

I don't see any relation between false/true and Nan.
Why would you think NaN and Infs should be false ?

JG
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
Michael Tiomkin
Guest





PostPosted: Thu May 17, 2007 9:11 am    Post subject: Re: why nan and infs evaluate to true when used as if condit Reply with quote

On May 15, 6:06 pm, Ganny <sggan...@gmail.com> wrote:
Quote:
#include <stdio.h
int main() {
int a = 0;
if (1.0/0.0)
printf("result was true");
else
printf("result was false");

}

I get "result was true" in the compilers i tried - Is this the right
behaviour expected? shouldn't nans and infs treated as false?

First, as far as I remember, only NaNs should return false after
any comparison,
and second, this happens only with IEEE compatible floating point
implementation.

1.0/0.0 will usually give you an infinity value, with results of
comparisons different
from those of NaNs, e.g. inf!=0 is true.
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
John Dallman
Guest





PostPosted: Thu May 17, 2007 9:11 am    Post subject: Re: why nan and infs evaluate to true when used as if condit Reply with quote

In article <clcm-20070515-0006 (AT) plethora (DOT) net>, sgganesh (AT) gmail (DOT) com (Ganny)
wrote:

Quote:
#include <stdio.h
int main() {
int a = 0;
if (1.0/0.0)
printf("result was true");
else
printf("result was false");
}

I get "result was true" in the compilers i tried - Is this the right
behaviour expected? shouldn't nans and infs treated as false?

Relying on this is a bad idea to start with, but since the general
definition of "what is true" in C is something rather like "not equal to
zero", it seems hard to say your compilers are wrong. Whatever
infinities and NaNs are, they aren't equal to zero.

--
John Dallman, jgd (AT) cix (DOT) co.uk, HTML mail is treated as probable spam.
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
Keith Thompson
Guest





PostPosted: Thu May 17, 2007 9:11 am    Post subject: Re: why nan and infs evaluate to true when used as if condit Reply with quote

Ganny <sgganesh (AT) gmail (DOT) com> writes:
Quote:
#include <stdio.h
int main() {
int a = 0;
if (1.0/0.0)
printf("result was true");
else
printf("result was false");
}

I get "result was true" in the compilers i tried - Is this the right
behaviour expected? shouldn't nans and infs treated as false?

Anything that compares equal to zero is false; anything else is true.

In any case, your program invokes undefined behavior. The C standard
doesn't require 1.0/0.0 to yield Infinity or NaN. It could yield some
real value, or terminate your program, or make demons fly out of your
nose.

--
Keith Thompson (The_Other_Keith) kst-u (AT) mib (DOT) org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
WillerZ
Guest





PostPosted: Thu May 17, 2007 9:11 am    Post subject: Re: why nan and infs evaluate to true when used as if condit Reply with quote

Ganny wrote:
Quote:
#include <stdio.h
int main() {
int a = 0;
if (1.0/0.0)
printf("result was true");
else
printf("result was false");
}

I get "result was true" in the compilers i tried - Is this the right
behaviour expected? shouldn't nans and infs treated as false?

if (exp)
statement1
else
statement2

is defined to execute statement1 iff exp is not equal to zero.

Is inf or nan equal to zero? No.
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.