2 pytania o float'a w C

D

Domel

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
 
K

Keith Thompson

Domel said:
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>.
 
M

Mabden

Keith Thompson said:
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.
 
A

Arthur J. O'Dwyer

Domel said:
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
 
C

CBFalconer

Arthur J. O'Dwyer said:
.... 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.
 
C

Christopher Benson-Manica

Arthur J. O'Dwyer said:
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 :)
 
D

Dan Pop

In said:
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
 
D

Dave Vandervies

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
 
C

Christopher Benson-Manica

Dave Vandervies said:
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.
 
A

Arthur J. O'Dwyer

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,008
Messages
2,570,271
Members
46,874
Latest member
CyberGateway

Latest Threads

Top