By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,503 Members | 1,185 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,503 IT Pros & Developers. It's quick & easy.

2 pytania o float'a w C

P: n/a
1 Jak zaokrąglić float'a tak żeby jeżeli część dziesiętna była większa lub
równa od 0.5 to był on zaokrąglany w górę, jeżeli mniejsza to w dół
np 1.6 po zaokr 2.0
1.2 po zaokr 1

prosiłbym o ile to możliwe o jakieś sposoby bez dołączania bibliotek, przy
użyciu jakichś operacji

2. dla czego jeżeli wykonuję poniższą operację

delta = delta -1.0 to po kilku iteracjach wyskakują mi jakieś dziewiątki??
12.100000

11.100000

10.100000

9.099997

8.099997

7.099997

6.099997

Dzięki Marcin

Nov 14 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
"Domel" <Sz*******@poczta.fm> writes:
[...]
2. dla czego jeżeli wykonuję poniższą operację

delta = delta -1.0 to po kilku iteracjach wyskakują mi jakieś dziewiątki??
12.100000

11.100000

10.100000

9.099997

8.099997

7.099997

6.099997


This newsgroup uses English by default; you're not likely to find many
people here who can read Polish. (There may be a Polish C newsgroup;
you'll have to check you local server.)

But at least I can read the numbers, which lead me to suspect that
you're having a problem with floating-point rounding errors. See
section 14 of the C FAQ, at <http://www.eskimo.com/~scs/C-faq/top.html>.

--
Keith Thompson (The_Other_Keith) ks***@mib.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.
Nov 14 '05 #2

P: n/a
"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
"Domel" <Sz*******@poczta.fm> writes:
[...]
2. dla czego jeżeli wykonuję poniższą operację

delta = delta -1.0 to po kilku iteracjach wyskakują mi jakieś dziewiątki?? 12.100000

11.100000

10.100000

9.099997

8.099997

7.099997

6.099997


This newsgroup uses English by default; you're not likely to find many
people here who can read Polish. (There may be a Polish C newsgroup;
you'll have to check you local server.)

But at least I can read the numbers, which lead me to suspect that
you're having a problem with floating-point rounding errors. See
section 14 of the C FAQ, at

<http://www.eskimo.com/~scs/C-faq/top.html>.

To get around this problem, you can use integers and a decimal point
value. For instance, instead of trying to store 1.27, store 127 and 2
decimal places. Perform all arithmetic on the integers (or longs, or
whatever) and use the number of decimal places when you print it out for
the user.

--
Mabden
Nov 14 '05 #3

P: n/a

On Tue, 5 Oct 2004, Keith Thompson wrote:

"Domel" <Sz*******@poczta.fm> writes:
2. dla czego jeżeli wykonuję poniższą operację
[...] This newsgroup uses English by default; you're not likely to find many
people here who can read Polish. (There may be a Polish C newsgroup;
you'll have to check you local server.)
It's pl.comp.lang.c, as far as I know. (Maybe the OP just posted to
the wrong group.)
But at least I can read the numbers, which lead me to suspect that
you're having a problem with floating-point rounding errors. See
section 14 of the C FAQ, at <http://www.eskimo.com/~scs/C-faq/top.html>.


Yes. And I'd be willing to bet that the OP's first question is answered
by the snippet

int round(double g)
{
return (int)(g + 0.5);
}

HTH,
-Arthur
Nov 14 '05 #4

P: n/a
"Arthur J. O'Dwyer" wrote:
.... snip ...
Yes. And I'd be willing to bet that the OP's first question is
answered by the snippet

int round(double g)
{
return (int)(g + 0.5);
} From N869:


7.12.9.6 The round functions

Synopsis

[#1]
#include <math.h>
double round(double x);
float roundf(float x);
long double roundl(long double x);

Description

[#2] The round functions round their argument to the nearest
integer value in floating-point format, rounding halfway
cases away from zero, regardless of the current rounding
direction.

Returns

[#3] The round functions return the rounded integer value.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #5

P: n/a
Arthur J. O'Dwyer <aj*@nospam.andrew.cmu.edu> spoke thus:
This message is in MIME format. The first part should be readable text,
while the remaining parts are likely unreadable without MIME-aware tools.


It seems that something is again awry with your news setup :)

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #6

P: n/a
In <41***************@yahoo.com> CBFalconer <cb********@yahoo.com> writes:
"Arthur J. O'Dwyer" wrote:

... snip ...

Yes. And I'd be willing to bet that the OP's first question is
answered by the snippet

int round(double g)
{
return (int)(g + 0.5);
}

From N869:


7.12.9.6 The round functions


In this case, the blame is entirely on C99. The round identifier was in
the user name space in C89 and it is likely that there is plenty of
existing code that already uses it. C99 had no business to make such a
gross intrusion in the C89 user name space.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Currently looking for a job in the European Union
Nov 14 '05 #7

P: n/a
In article <ck**********@chessie.cirr.com>,
Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote:
Arthur J. O'Dwyer <aj*@nospam.andrew.cmu.edu> spoke thus:
This message is in MIME format. The first part should be readable text,
while the remaining parts are likely unreadable without MIME-aware tools.


It seems that something is again awry with your news setup :)


From the content description of the only part of the "multipart" message:
}Content-Type: TEXT/PLAIN; charset=iso-8859-1; format=flowed
}Content-Transfer-Encoding: 8BIT

Other than incorrectly reporting a one-part post as multipart, it seems
to be doing The Right Thing with the non-ASCII characters he was quoting.
dave

--
Dave Vandervies dj******@csclub.uwaterloo.ca
I'm not a programmer by trade, but my stupid mistakes are getting more
complicated!
--Phil Mendelsohn in comp.lang.c
Nov 14 '05 #8

P: n/a
Dave Vandervies <dj******@csclub.uwaterloo.ca> spoke thus:
Other than incorrectly reporting a one-part post as multipart, it seems
to be doing The Right Thing with the non-ASCII characters he was quoting.


Well, the main thing is that Arthur normally doesn't post MIME-encoded
messages; this happened once in the past, where something fubar'ed his
news setup and everything was MIME until he fixed it.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #9

P: n/a

On Wed, 6 Oct 2004, Christopher Benson-Manica wrote:

Dave Vandervies <dj******@csclub.uwaterloo.ca> spoke thus:
Other than incorrectly reporting a one-part post as multipart, it seems
to be doing The Right Thing with the non-ASCII characters he was quoting.


Well, the main thing is that Arthur normally doesn't post MIME-encoded
messages; this happened once in the past, where something fubar'ed his
news setup and everything was MIME until he fixed it.


Yeah. I do think the non-ASCII characters had something to do with
it this time, though. I've sent a message to the people who are supposed
to know what's up with the Andrew system's software, just in case it's the
weird MIME thing coming back. But if this message doesn't show up as
MIME, then I guess it was just a one-time weirdness.

(For the record, I can't see any difference in the displayed posts in
this client, which is why I didn't notice until Chris brought it up last
time or this time. I can, however, choose to look at the actual message
headers to find out whether it's still doing it. So please don't reply to
this post /only/ to say "yes, it's MIME" or "no, it's fixed"---I'll be
able to see that myself. And if it's not fixed, all I can do is wait for
the systems people to tell me what the deal is.)

Thanks for the heads-up,
-Arthur
Nov 14 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.