[Subject Prev][Subject Next][Thread Prev][Thread Next][Subject Index][Thread Index]

*To*: linux-india-programmers@xxxxxxxxxxxxxxxxxxxxx*Subject*: Re: int(x) is .ne. to x when x is whole number.*From*: Babu Kalakrishnan <kalakrishnan@xxxxxxxx>*Date*: Sat, 2 Dec 2000 17:31:04 +0530*In-reply-to*: <Pine.LNX.4.04.10011291414570.31641-100000@mail>; from hsraidce@xxxxxxxxxxxxxxxxxxxx on Wed, Nov 29, 2000 at 02:20:04PM +0530*Mail-followup-to*: Babu Kalakrishnan <kalakrishnan@xxxxxxxx>, linux-india-programmers@xxxxxxxxxxxxxxxxxxxxx*Organization*: Sankya Systems & Objects (P) Ltd., Bangalore, India*References*: <3A237E04.DA0083B8@xxxxxxxxxx> <Pine.LNX.4.04.10011291414570.31641-100000@mail>*User-agent*: Mutt/1.2i

Dear Rai, On Wed, Nov 29, 2000 at 02:20:04PM +0530, H.S.Rai typed: > calculations. It is simply adding 0.2 to intial number 0.0 a number of > times. I want to know root couse of problem as I assume such behaviour > shhould not be there. > One can never guarantee that any real number can be accurately rpresented by the "float" or "double" datatypes. Quoting from "info libc" : Floating-point numbers can represent a finite subset of the real numbers. While this subset is large enough for most purposes, it is important to remember that the only reals that can be represented exactly are rational numbers that have a terminating binary expansion shorter than the width of the mantissa. Even simple fractions such as 1/5 can only be approximated by floating point. In your specific case, the number 0.2 is not exactly representable by the "float" data type. Because the operation you're doing is a repeated addition, the error in representation of 0.2 gets accumulated over each operation (as you can observe from the results you'd quoted). In fact if you had used a multiply operator instead of the repeated addition (i.e "N*0.2" instead of adding 0.2 N times), the error would have been lower and non-cumulative. (But of course still with no guarantee that the comparison would return a success :):) ) Kala

- Prev by Subject:
**if your editor is joe..** - Next by Subject:
**Intel 64 bit architechture** - Previous by thread:
**if your editor is joe..** - Next by thread:
**Intel 64 bit architechture** - Index(es):