niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Programming » C
floating point problem
Post new topic   Reply to topic Page 2 of 3 [34 Posts] View previous topic :: View next topic
Goto page:  Previous  1, 2, 3 Next
Author Message
Ancient_Hacker
*nix forums beginner


Joined: 31 May 2006
Posts: 42

PostPosted: Thu Jul 20, 2006 3:33 pm    Post subject: Re: floating point problem Reply with quote

Richard Heathfield wrote:

Quote:
Um, that is totally and utterly irrelevant to my point, which is that with
32 bits you only have 2^32 bit patterns so you can only represent 2^32
different values,

One might suspect most programmers are aware it's impossible to
represent an infinite number of distinct states in a computer.

This poor sod was just wondering why 0.7 didnt seem to equal 0.7, and
probably wanting to learn why and how to avoid this in the future.

So that comment was obvious, inaccurate and generally unhelpful toward
the goal of answering the posers question.

Is the goal of this ng to polish our egos in cyberspace or to maybe
actually help answer the questions posed? Smile
Back to top
neutron*star
*nix forums Guru


Joined: 21 Feb 2005
Posts: 2039

PostPosted: Thu Jul 20, 2006 5:06 pm    Post subject: Re: floating point problem Reply with quote

Ancient_Hacker said:

Quote:

Richard Heathfield wrote:

Um, that is totally and utterly irrelevant to my point, which is that
with 32 bits you only have 2^32 bit patterns so you can only represent
2^32 different values,

One might suspect most programmers are aware it's impossible to
represent an infinite number of distinct states in a computer.

One might, but one doesn't need to read the IEEE 754 spec to understand why.

Quote:
This poor sod was just wondering why 0.7 didnt seem to equal 0.7, and
probably wanting to learn why and how to avoid this in the future.

So that comment was obvious, inaccurate and generally unhelpful toward
the goal of answering the posers question.

If you check up, you'll find that I wasn't answering the OP's question. I
was making a followup comment to a followup answer.

Quote:
Is the goal of this ng to polish our egos in cyberspace or to maybe
actually help answer the questions posed? Smile

The goal of this newsgroup is the discussion of C programming. To answer
questions is *not* in fact the aim.

The questions asked here are rarely terribly interesting, but they often
lead to very interesting spin-off discussions about the less obvious
aspects of C and computer science. That is why some people bother to answer
them. Others like to answer them because they're nice guys who like to help
out. Sometimes the motivation is a mix of the two, and no doubt there are
other reasons why people here answer questions, but it is not the primary
purpose of the group.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Back to top
Tak-Shing Chan
*nix forums beginner


Joined: 10 Jul 2006
Posts: 19

PostPosted: Thu Jul 20, 2006 8:38 pm    Post subject: Re: floating point problem Reply with quote

On Thu, 20 Jul 2006, Richard Heathfield wrote:

Quote:
The questions asked here are rarely terribly interesting, but they often
lead to very interesting spin-off discussions about the less obvious
aspects of C and computer science. That is why some people bother to answer
them. Others like to answer them because they're nice guys who like to help
out. Sometimes the motivation is a mix of the two, and no doubt there are
other reasons why people here answer questions, but it is not the primary
purpose of the group.

In the above quote, Richard said ``C and computer science''
instead of just ``C''---and then he ended with ``other reasons...
not the primary purpose of the group''. This seems to suggest
that computer science is one of the primary purposes here!

Tak-Shing
Back to top
Joe Wright
*nix forums Guru


Joined: 22 Feb 2005
Posts: 468

PostPosted: Thu Jul 20, 2006 8:45 pm    Post subject: Re: floating point problem Reply with quote

dis_is_eagle@yahoo.com wrote:
Quote:
hi....i have encountered strange problem regarding floating point
comparison...the problem is...

main()
{
float a=0.7;
if(0.7 > a)
printf("hi");
else
printf("hello");
}

i think the answer should have hello...but strangely it is hi....but
for a=0.8 the answer is hello...again for a=0.9 the answer is hi....the
program is run on a linux system....if anybody explains this problem it
will be very helpful...thanx in advance...bye..

regards,

eric

Do this instead..


#include <stdio.h>
int main(void) {
float f = 0.7;
double d = 0.7;
printf("%.16e\n", f);
printf("%.16e\n", d);
return 0;
}

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Back to top
dcorbit@connx.com
*nix forums addict


Joined: 13 Jun 2006
Posts: 92

PostPosted: Thu Jul 20, 2006 8:46 pm    Post subject: Re: floating point problem Reply with quote

"Tak-Shing Chan" <t.chan@gold.ac.uk> wrote in message
news:Pine.GSO.4.61.0607202116000.4798@scorpio.gold.ac.uk...
Quote:
On Thu, 20 Jul 2006, Richard Heathfield wrote:

The questions asked here are rarely terribly interesting, but they often
lead to very interesting spin-off discussions about the less obvious
aspects of C and computer science. That is why some people bother to
answer
them. Others like to answer them because they're nice guys who like to
help
out. Sometimes the motivation is a mix of the two, and no doubt there are
other reasons why people here answer questions, but it is not the primary
purpose of the group.

In the above quote, Richard said ``C and computer science''
instead of just ``C''---and then he ended with ``other reasons...
not the primary purpose of the group''. This seems to suggest
that computer science is one of the primary purposes here!

If it were nothing but C, then the group could be replaced by a pointer to
the ANSI/ISO C standard.

The things that really get posted here are usually mixtures of C and C
related things and things unrelated to C. The amount of tangentiality that
is allowed is probably a bit less than other groups, but that keeps the
group focused.

I recently went off on a tangent about algorithms. Now, we write algorithms
in C (and we might also say that we use C to write algorithms). We can
stray further and further from the core ideas until we are talking about
rainbows and aluminum (which is munimula, when spelled backwards).
Eventually, we must arrive at something which no longer holds any interest
for the vast majority of denizens of c.l.c.

But of course, this post is topical (along with both Richard's post and your
response) because it is a discussion of topicality, which is always
topical -- at least according to long established USENET tradition.

> Tak-Shing
Back to top
Tak-Shing Chan
*nix forums beginner


Joined: 10 Jul 2006
Posts: 19

PostPosted: Thu Jul 20, 2006 9:37 pm    Post subject: Re: floating point problem Reply with quote

On Thu, 20 Jul 2006, Dann Corbit wrote:

Quote:
"Tak-Shing Chan" <t.chan@gold.ac.uk> wrote in message
news:Pine.GSO.4.61.0607202116000.4798@scorpio.gold.ac.uk...
On Thu, 20 Jul 2006, Richard Heathfield wrote:

The questions asked here are rarely terribly interesting, but they often
lead to very interesting spin-off discussions about the less obvious
aspects of C and computer science. That is why some people bother to
answer
them. Others like to answer them because they're nice guys who like to
help
out. Sometimes the motivation is a mix of the two, and no doubt there are
other reasons why people here answer questions, but it is not the primary
purpose of the group.

In the above quote, Richard said ``C and computer science''
instead of just ``C''---and then he ended with ``other reasons...
not the primary purpose of the group''. This seems to suggest
that computer science is one of the primary purposes here!

If it were nothing but C, then the group could be replaced by a pointer to
the ANSI/ISO C standard.

The things that really get posted here are usually mixtures of C and C
related things and things unrelated to C. The amount of tangentiality that
is allowed is probably a bit less than other groups, but that keeps the
group focused.

I recently went off on a tangent about algorithms. Now, we write algorithms
in C (and we might also say that we use C to write algorithms). We can
stray further and further from the core ideas until we are talking about
rainbows and aluminum (which is munimula, when spelled backwards).
Eventually, we must arrive at something which no longer holds any interest
for the vast majority of denizens of c.l.c.

But of course, this post is topical (along with both Richard's post and your
response) because it is a discussion of topicality, which is always
topical -- at least according to long established USENET tradition.

``Primary'' and ``tangentiality'' are antonyms. As far as I
know, computer science is not the primary purpose of this group.

Tak-Shing
Back to top
dcorbit@connx.com
*nix forums addict


Joined: 13 Jun 2006
Posts: 92

PostPosted: Thu Jul 20, 2006 9:42 pm    Post subject: Re: floating point problem Reply with quote

"Tak-Shing Chan" <t.chan@gold.ac.uk> wrote in message
[snip]
Quote:
``Primary'' and ``tangentiality'' are antonyms. As far as I
know, computer science is not the primary purpose of this group.

No, but a subset of computer science is the primary purpose of the group.

> Tak-Shing
Back to top
Joe Wright
*nix forums Guru


Joined: 22 Feb 2005
Posts: 468

PostPosted: Thu Jul 20, 2006 9:43 pm    Post subject: Re: floating point problem Reply with quote

dis_is_eagle@yahoo.com wrote:
Quote:
hi....i have encountered strange problem regarding floating point
comparison...the problem is...

main()
{
float a=0.7;
if(0.7 > a)
printf("hi");
else
printf("hello");
}

i think the answer should have hello...but strangely it is hi....but
for a=0.8 the answer is hello...again for a=0.9 the answer is hi....the
program is run on a linux system....if anybody explains this problem it
will be very helpful...thanx in advance...bye..

regards,

eric

float a = 0.7 has a precision of 24 bits. 0.7 is a double with precision

(here) of 53 bits. As it turns out the double 0.7 is larger than the
float 0.7 by a bit. :-)

The float converted to double:
6.9999998807907104e-01

The double:
6.9999999999999996e-01

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Back to top
Mark McIntyre
*nix forums Guru


Joined: 16 Feb 2005
Posts: 1564

PostPosted: Thu Jul 20, 2006 9:49 pm    Post subject: Re: floating point problem Reply with quote

On 20 Jul 2006 08:33:57 -0700, in comp.lang.c , "Ancient_Hacker"
<grg2@comcast.net> wrote:

Quote:

Richard Heathfield wrote:

Um, that is totally and utterly irrelevant to my point, which is that with
32 bits you only have 2^32 bit patterns so you can only represent 2^32
different values,

One might suspect most programmers are aware it's impossible to
represent an infinite number of distinct states in a computer.

One might, but one would be disappointed. An alarmingly large number
of computer programmers seem to think that computers are exact.

--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Back to top
neutron*star
*nix forums Guru


Joined: 21 Feb 2005
Posts: 2039

PostPosted: Thu Jul 20, 2006 9:56 pm    Post subject: Re: floating point problem Reply with quote

Tak-Shing Chan said:

Quote:
On Thu, 20 Jul 2006, Richard Heathfield wrote:

The questions asked here are rarely terribly interesting, but they often
lead to very interesting spin-off discussions about the less obvious
aspects of C and computer science. That is why some people bother to
answer them. Others like to answer them because they're nice guys who
like to help out. Sometimes the motivation is a mix of the two, and no
doubt there are other reasons why people here answer questions, but it is
not the primary purpose of the group.

In the above quote, Richard said ``C and computer science''
instead of just ``C''---and then he ended with ``other reasons...
not the primary purpose of the group''. This seems to suggest
that computer science is one of the primary purposes here!

Bear in mind that my view is not normative! I am merely stating my opinion,
which is that C discussions are the primary purpose of the group.

Computer science discussions are not strictly topical, but are a
nice-to-have when they flow naturally from a topical discussion.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Back to top
Tak-Shing Chan
*nix forums beginner


Joined: 10 Jul 2006
Posts: 19

PostPosted: Thu Jul 20, 2006 10:06 pm    Post subject: Re: floating point problem Reply with quote

On Thu, 20 Jul 2006, Dann Corbit wrote:

Quote:
"Tak-Shing Chan" <t.chan@gold.ac.uk> wrote in message
[snip]
``Primary'' and ``tangentiality'' are antonyms. As far as I
know, computer science is not the primary purpose of this group.

No, but a subset of computer science is the primary purpose of the group.

The original context is ``C and computer science''. It is
quite clear to me that this is not referring to a subset. More
to the point, if the subset in question is ``general issues of
the C programming language, as defined by the ANSI/ISO language
standard'' (Billy Chambless), then the phrase ``C and computer
science'' becomes redundant.

Tak-Shing
Back to top
Tak-Shing Chan
*nix forums beginner


Joined: 10 Jul 2006
Posts: 19

PostPosted: Thu Jul 20, 2006 10:12 pm    Post subject: Re: floating point problem Reply with quote

On Thu, 20 Jul 2006, Richard Heathfield wrote:

Quote:
Computer science discussions are not strictly topical, but are a
nice-to-have when they flow naturally from a topical discussion.

In other words original posters aren't allowed to post
off-topic while the question-answerers (regulars) can as long as
it's deemed ``natural''. Interesting view. :-)

Tak-Shing
Back to top
neutron*star
*nix forums Guru


Joined: 21 Feb 2005
Posts: 2039

PostPosted: Thu Jul 20, 2006 10:25 pm    Post subject: Re: floating point problem Reply with quote

Tak-Shing Chan said:

Quote:
On Thu, 20 Jul 2006, Richard Heathfield wrote:

Computer science discussions are not strictly topical, but are a
nice-to-have when they flow naturally from a topical discussion.

In other words original posters aren't allowed to post
off-topic

That depends on what you mean by "allowed". OPs /do/ post off-topic
material, after all.

Quote:
while the question-answerers (regulars) can as long as
it's deemed ``natural''.

Nobody is doing any deeming, and anybody /can/ post what they like - it is
not a question of whether they /can/, but whether they /should/.

Kaz once talked about solid, topical articles being like pennies in the
bank, and off-topic articles being dollar withdrawals. That's a pretty good
analogy. And of course the trick is to stay in credit. (Warning: that's
just an analogy, and is not to be taken literally - nobody is doing the
accounting, as far as I am aware.)

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Back to top
Tak-Shing Chan
*nix forums beginner


Joined: 10 Jul 2006
Posts: 19

PostPosted: Thu Jul 20, 2006 10:45 pm    Post subject: Re: floating point problem Reply with quote

On Thu, 20 Jul 2006, Richard Heathfield wrote:

Quote:
Kaz once talked about solid, topical articles being like pennies in the
bank, and off-topic articles being dollar withdrawals. That's a pretty good
analogy. And of course the trick is to stay in credit. (Warning: that's
just an analogy, and is not to be taken literally - nobody is doing the
accounting, as far as I am aware.)

I do not think that this is a good analogy. IMHO the level
of expertise and/or effort required to answer the question should
be taken into account. I have in mind the mini-essays from Chris
Torek which in the real world should worth 20 times more than the
average on-topic posts (in terms of the post-to-pennies ratio).

Tak-Shing
Back to top
dcorbit@connx.com
*nix forums addict


Joined: 13 Jun 2006
Posts: 92

PostPosted: Thu Jul 20, 2006 11:13 pm    Post subject: Re: floating point problem Reply with quote

"Tak-Shing Chan" <t.chan@gold.ac.uk> wrote in message
news:Pine.GSO.4.61.0607202337001.18953@scorpio.gold.ac.uk...
Quote:
On Thu, 20 Jul 2006, Richard Heathfield wrote:

Kaz once talked about solid, topical articles being like pennies in the
bank, and off-topic articles being dollar withdrawals. That's a pretty
good
analogy. And of course the trick is to stay in credit. (Warning: that's
just an analogy, and is not to be taken literally - nobody is doing the
accounting, as far as I am aware.)

I do not think that this is a good analogy. IMHO the level
of expertise and/or effort required to answer the question should
be taken into account. I have in mind the mini-essays from Chris
Torek which in the real world should worth 20 times more than the
average on-topic posts (in terms of the post-to-pennies ratio).

Which is to say that some deposits are a dollar or ten dollars or more.

So there are individual posters (Tanmoy Bhattacharya, Dan Pop, Chris Torek,
Peter Seebach, etc.) who have a huge balance on hand (c.l.c billionaires?).

Other posters are way, way, way overdrawn and have never managed a positive
balance.

Different posters see this forum in different ways. But I think a sensible
goal is to add value to the forum, which fits our analogy nicely. After
reading any chain of messages, I should walk away with some incremental
improvement in my understanding.

(Or at least a refresher of what I already knew).

So we should strive to have threads that impart focused knowledge to the
readers that is clear, unambiguous, and enlightening [not to mention
correct].

IMO-YMMV.

> Tak-Shing
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 2 of 3 [34 Posts] Goto page:  Previous  1, 2, 3 Next
View previous topic :: View next topic
The time now is Wed Dec 03, 2008 8:41 pm | All times are GMT
navigation Forum index » Programming » C
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts Unknown in header problem -SOLVED- Light Speed Postfix 0 Thu Jul 03, 2008 10:40 am
No new posts problem with sending mail nuxia Postfix 0 Mon Apr 21, 2008 3:58 am
No new posts Postfix 2.3.8 Virtual problem Blotto Postfix 0 Fri Apr 04, 2008 6:11 am
No new posts Postfix sending problem for local domain remote email monkey_magix Postfix 0 Mon Sep 10, 2007 10:17 am
No new posts bounce problem murkis Postfix 0 Sun Oct 08, 2006 3:45 pm

Loans | Loan | Credit Card Consolidation | Society 2007 | Credit Card
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.3389s ][ Queries: 16 (0.1568s) ][ GZIP on - Debug on ]