[Subject Prev][Subject Next][Thread Prev][Thread Next][Subject Index][Thread Index]
Re: int(x) is .ne. to x when x is whole number.
Dear Shridhar
Thanks for your good reply. Sir, I am fully aware that 1/3.0 * 3 is not
equal to one, and my program is not involving such or other trignometric
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.
I thing you help by providing pointer or answer to this.
Bye
h s rai
rurki ( uttaranchal )
*************************************************************************
On Tue, 28 Nov 2000, Shridhar Daithankar wrote:
> Hi
>
> It looks that the routine generating the float is not producing exact numbers.
> It's very possible if you are using some trigometric series type stuff to
> generate it. These are precision errors and usually can be avoided.
>
> 1)How to generate error. e.g do PI/4-atan(tan(PI/4)). See it's not zero... :-))
> 2)How to rectify the error? Define some epsilon like 1e-6, say. and trip it to
> that precision.
>
> x=atof(sprintf(str,"%6.4f",x));
>
> So you got it.
>
> Check out the syntax. I am not sure. It's been long time I touched maths.
>
> HTH
>
> Bye
> Shridhar
>
> P.S. This has nothing to do with linux. Anyway you need to understand how does
> computer computes. That will tell you such problems in advance... :-))
>
> "H.S.Rai" wrote:
>
> > All linux Programmers,
> >
> > I have written a program in which I have to check variable, and
> > in case it is whole number, follow some different path. To check this,
> > float variable is casted with int, and if difference of int(x) and x is
> > zero, it should be a whole number. For posting purpose I simplified
> > program just to highlight the problem. The differnece is zero only for ist
> > two execution of loop, after that it is not zero, which make my flow of
> > program wrong. At the end, output ( line ended with '****' mean unexpected
> > results), and source code of program is appended.
> >
> > Pl help me by pointing out my mistake, and suggest how to implement such a
> > logic. I am using g++ on REDHAT 6.2 (kernel 2.2.14-5.0 ).
> > ______________________________________________________________
> >
> > Result File:
> >
> > Integer Float Differance
> > 0 0 0
> > 0 0.2 0.2
> > 0 0.4 0.4
> > 0 0.6 0.6
> > 0 0.8 0.8
> > 1 1 0
> > 1 1.2 0.2
> > 1 1.4 0.4
> > 1 1.6 0.6
> > 1 1.8 0.8
> > 2 2 2.38419e-07 **** / 3rd coloumn should be 0 /
> > 2.2 0.2
> > 2 2.4 0.4
> > 2 2.6 0.6
> > 2 2.8 0.8
> > 3 3 4.76837e-07 **** / do /
> > 3 3.2 0.200001 **** / 3rd coloumn should be .2/
> > 3 3.4 0.400001 ****
> > 3 3.6 0.600001 ****
> > 3 3.8 0.800001 ****
> > 4 4 4.76837e-07 ****
> > 4 4.2 0.2
> > 4 4.4 0.4
> > 4 4.6 0.6
> > 4 4.8 0.8
> > 4 5 1
>
>
> ---------------------------------------------
> An alpha version of a web based tool to manage
> your subscription with this mailing list is at
> http://lists.linux-india.org/cgi-bin/mj_wwwusr
>