double trouble with numbers

Posted By: Attila

double trouble with numbers - 10/12/07 16:49

I try to get numbers like 0.0001, -0.0004 from a text file.
This is my lite-C Pure code of an action.
I get an ugly error window saying that acknex.exe has to be closed.
Is there something wrong of how i declare a double variable?

action in_node_000()
{

STRING reader;
double check;
var filehandle;

filehandle = file_open_read("mytextfile");

if (filehandle > 0){
file_str_read(filehandle,reader);

file_close(filehandle);

check = str_to_num(reader);

while(check == 0.00001){

my.pan += 1;
wait(1);

}
}

}
Posted By: i_program_games

Re: double trouble with numbers - 10/12/07 21:34

Try
STRING* reader;
as opposed to STRING reader;
Posted By: Attila

Re: double trouble with numbers - 10/13/07 01:22

I tried to debug, and I foung file_open_read returns NULL value.
I checked, I have the file in work folder and numbers in it like this

1
-2
3
4
3
-5

Still I cannot read them in pure c-lite mode.
Posted By: Ottawa

Re: double trouble with numbers - 10/13/07 22:45

The numbers have to be on the same line (I think)
like
1 -2 3 4 3 -5

I use them on the same line and it works.
Posted By: Doug

Re: double trouble with numbers - 10/16/07 23:44

Code:

double check;

...

while(check == 0.00001){
my.pan += 1;
wait(1);
}



This will probably not work. To check it, set "check = 0.00001;" and try the while loop.

I say probably because, depending on the compiler, it might work in this case. But it is always a bad idea to compare floating point values using "==" since the numbers are not always exact.
Posted By: HeelX

Re: double trouble with numbers - 05/04/08 12:20

(Digging this up to add some useful information -- I saw the thread under "who's online")

Doug is right. Floating point numbers are mostly never equal a specific value except you set them onto it. Better use a function, that calculates the difference and proves it against a threshold value.

Algorithm:
 Code:
bool fEqual (double f1, double f2) {
	static const double EPSILON = 0.001;
	return((bool)(fabs(f1-f2) <= EPSILON));
}

© 2023 lite-C Forums