NTNUJAVA Virtual Physics Laboratory
Enjoy the fun of physics with simulations!
Backup site http://enjoy.phy.ntnu.edu.tw/ntnujava/

Easy Java Simulations (2001- ) => Questions related to EJS => Topic started by: lookang on July 15, 2009, 03:54:10 pm



Title: a bug i don't understand why it is happening something about errors when 0.2 0.8
Post by: lookang on July 15, 2009, 03:54:10 pm
a bug i don't understand why it is happening something about errors when p = -0.2, -0.8, -1.2, -1.8.

the numbers for q and other calculation become wrong, it should be infinity but it shows a large number.

did i code it wrongly?

thx!


Title: Re: a bug i don't understand why it is happening something about errors when 0.2 0.8
Post by: lookang on July 16, 2009, 08:00:49 am
fixed relationship i have a line of code like this:

if ( (p/f==-1&&p<0&&f>0)||(p/f==1&&p>0&&f>0) ) {

s_mom2 = "When Object distance, u = f, use by spotlight when projecting parallel beam of light";
}


the rest of the times it seems p = -f =  0.4, 0.6 1.0, 1.4,1.6,2.0 works   it works for p/f==-1

but when p = -f = -0.2, -0.8, -1.2, -1.8, it fails.  It is very strange so i being thinking and thinking but i thought i bounce it off this forum!
thx!


Title: Re: a bug i don't understand why it is happening something about errors when 0.2 0.8
Post by: lookang on July 16, 2009, 01:03:38 pm
i tried using the field to enter the value again, and the bug is problem solved.

i wonder if it is due to
1. the value stored in p = 0.20000000000001 instead of 0.2 exactly is occuring
2. the condition to check maybe i change it to be less strict like 

if ( (p/f<-0.95&&p/f>-1.05&&p<0&&f>0)||(p/f<1.05&&p/f>0.95&&p>0&&f>0) ) {

maybe that will solved the bug problem?


Title: Re: a bug i don't understand why it is happening something about errors when 0.2 0.8
Post by: lookang on July 16, 2009, 04:28:16 pm
continue to work at home later


Title: Re: a bug i don't understand why it is happening something about errors when 0.2 0.8
Post by: Fu-Kwun Hwang on July 16, 2009, 06:46:55 pm
I am sorry that I was out of town for a few days.

There is finite number of memory storage so there is no way to represent infinite number of all values.
For "double" defined in java/ejs, it use 8 byte to represent a number.
And the maximum number can be represented is about 10138 , there is no way to stored infinite number of different values.
The maximum value is defined as Double.POSITIVE_INFINITY / Doublle.NEGATIVE_INFINITY

For real world problem, if the distance is 100 or 1000. times larger than focus length, it can almost be treated as infinity (the error is too small to be neglected).



Title: Re: a bug i don't understand why it is happening something about errors when 0.2 0.8
Post by: lookang on July 17, 2009, 03:57:01 pm
i managed to re-code the logic of showing the text scaffolds and picture. One problem solved.

It seems there is a bug i can't work around it.

Only for p = -1.8 , f = 1.8, a strange drawing appears.
The rest of the values seems to work well.

any suggestion how to make the bug disappear?(http://www.phy.ntnu.edu.tw/ntnujava/index.php?action=dlattach;topic=1167.0;attach=1198;image)


Title: Re: a bug i don't understand why it is happening something about errors when 0.2 0.8
Post by: Fu-Kwun Hwang on July 17, 2009, 04:52:07 pm
You have the following equation in "Fixed relation" page:
q=f*p/(f+s*p);

It will produce run time error when  f+s*p=0;

You should have check
if(Math.abs(f+s*p)<1.e-6){// or whatever small value which is enough for your case
 q=
}else q=f*p/(f+s*p);


Title: Re: a bug i don't understand why it is happening something about errors when 0.2 0.8
Post by: lookang on July 17, 2009, 11:17:57 pm
ic, good idea.

i did a little more investigation, i think i know the reason for the "bug".

I have a feeling about the "bug" when the values for p and f are not exactly as they seems.
for example p = 1.8, but the computer instead stored it as 1.80000000000000000000001

so i constructed a code

Code:
if(f>0)dv1=0.5; // trying to solve bug of f = 1.8, p = 1.8, lines are drawn in wrong, should be invisible
 else if(f<0)dv1=-0.5;
 else dv1=0;
//f = ((int)(f*100000+dv))/100000.;
f = (int)(f*10+dv1)/10.;

if(p>0)dv2=0.5; // trying to solve bug of f = 1.8, p = 1.8, lines are drawn in wrong, should be invisible
 else if(p<0)dv2=-0.5;
 else dv2=0;
//f = ((int)(f*100000+dv))/100000.;
p = (int)(p*10+dv2)/10.;

to put on the left (this is important the sequence) of
Code:
q=f*p/(f+s*p);

the idea i think is to force the computer to store p = 1.8 instead of 1.80000000000000000000001
and sub into the equation,  q=f*p/(f+s*p);

the slider bars default may have caused the 1.8000000000001  i believe.

i think it is ok now! thanks for help and ideas :)

i will find out more bugs to overcome, still can do more : ;D