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

int(x) is .ne. to x when x is whole number.



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
__________________________________________________________
Source Code:
________________________________________________________

#include <iostream.h>
int z;
float x;
void main()
{
	float start = 0, termi = 5,step = .2;
	cout <<"Integer Float   Differance" << endl;
	for ( x = start; x <= termi; x += step )
	{
		z = int(x);
		cout << z <<'\t'<< x<< '\t' << x - float(z) <<endl;
	}
}

__________________________________________________________________



H. S. Rai
Roorkee - 247 667( Uttaranchal )