473,568 Members | 2,882 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

2 pytania o float'a w C

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
9 1693
"Domel" <Sz*******@pocz ta.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_Keit h) 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
"Keith Thompson" <ks***@mib.or g> wrote in message
news:ln******** ****@nuthaus.mi b.org...
"Domel" <Sz*******@pocz ta.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

On Tue, 5 Oct 2004, Keith Thompson wrote:

"Domel" <Sz*******@pocz ta.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
"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********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Nov 14 '05 #5
Arthur J. O'Dwyer <aj*@nospam.and rew.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)cybers pace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #6
In <41************ ***@yahoo.com> CBFalconer <cb********@yah oo.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
In article <ck**********@c hessie.cirr.com >,
Christopher Benson-Manica <at***@nospam.c yberspace.org> wrote:
Arthur J. O'Dwyer <aj*@nospam.and rew.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
Dave Vandervies <dj******@csclu b.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)cybers pace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #9

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

Dave Vandervies <dj******@csclu b.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
1867
by: Pat | last post by:
Give two double-typed variable X and Y. If (X==Y) is true, then how about the following results: (float(X) > float(Y))? (float(X) < float(Y))? (float(X) >= float(Y))? ( X > float(Y) )? ( X < float(Y) )?
16
2542
by: Gerald Lafreniere | last post by:
{ float F=123.456000; F*=1000; // Actually I used a for loop F*=10 three times. printf("%f\n", F); } This will produce something like 123456.00XXXX, where XXXX are garbage digits. Why is this happening, and how do I get rid of it? (using Dev-C++
6
2293
by: Dave win | last post by:
Hi all: I'm confused with the expression "(float *())". Book says that this is a cast. But, I have no idea of this expr. why could this expr ignore the variable??? Thanx!!!
9
2393
by: Sisyphus | last post by:
Hi, I have some software that does the following (in an attempt to determine whether the double x, can be represented just as accurately by a float): void test_it(double x) { float y = x; if(x == y) { printf("x can be represented precisely by a float\n"); }
11
2253
by: Marc Pelletier | last post by:
Hello, I am having trouble implementing the following callback: CNCSError CECWCompressor::WriteReadLine(UINT32 nNextLine, void **ppInputArray) where ppInputArray is a 3 by x array. The length of x is not known at compile time.
20
3122
by: ehabaziz2001 | last post by:
That program does not yield and respond correctly espcially for the pointers (*f),(*i) in print_divide_meter_into(&meter,&yds,&ft,&ins); /*--------------pnt02own.c------------ ---1 inch = 2.51 cm ---1 inch = 2.54/100 Meter ---1 yard = 3 feet ---1 feet = 12 inch
8
6006
by: vjnr83 | last post by:
Hi, I have a doubt: what is the difference between float **p and float *p? Thanks in advance, Vijay
13
6166
by: Shirsoft | last post by:
I have a 32 bit intel and 64 bit AMD machine. There is a rounding error in the 8th digit. Unfortunately because of the algorithm we use, the errors percolate into higher digits. C++ code is ------------------ b += (float)(mode *val); On 32 bit(intel , vs 2003, C++), some watch variables are
3
10653
by: Arnie | last post by:
Folks, We ran into a pretty significant performance penalty when casting floats. We've identified a code workaround that we wanted to pass along but also was wondering if others had experience with this and if there is a better solution. -jeff
0
7693
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7916
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7660
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6275
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5498
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2101
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1207
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
932
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.