Author Topic: a bug i don't understand why it is happening something about errors when 0.2 0.8  (Read 9937 times)

lookang

  • Hero Member
  • *****
  • Posts: 1772
  • http://weelookang.blogspot.com
    • lookang web
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!

lookang

  • Hero Member
  • *****
  • Posts: 1772
  • http://weelookang.blogspot.com
    • lookang web
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!
« Last Edit: July 16, 2009, 10:44:49 am by lookang »

lookang

  • Hero Member
  • *****
  • Posts: 1772
  • http://weelookang.blogspot.com
    • lookang web
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?

lookang

  • Hero Member
  • *****
  • Posts: 1772
  • http://weelookang.blogspot.com
    • lookang web
continue to work at home later

Fu-Kwun Hwang

  • Administrator
  • Hero Member
  • *****
  • Posts: 3062
    • Enjoy the fun of physics with simulations
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).


lookang

  • Hero Member
  • *****
  • Posts: 1772
  • http://weelookang.blogspot.com
    • lookang web
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?

Full screen applet or Problem viewing java?Add http://www.phy.ntnu.edu.tw/ to exception site list
Press the Alt key and the left mouse button to drag the applet off the browser and onto the desktop. This work is licensed under a Creative Commons Attribution 2.5 Taiwan License
  • Please feel free to post your ideas about how to use the simulation for better teaching and learning.
  • Post questions to be asked to help students to think, to explore.
  • Upload worksheets as attached files to share with more users.
Let's work together. We can help more users understand physics conceptually and enjoy the fun of learning physics!
« Last Edit: July 17, 2009, 05:02:43 pm by lookang »

Fu-Kwun Hwang

  • Administrator
  • Hero Member
  • *****
  • Posts: 3062
    • Enjoy the fun of physics with simulations
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);

lookang

  • Hero Member
  • *****
  • Posts: 1772
  • http://weelookang.blogspot.com
    • lookang web
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

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
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
« Last Edit: July 18, 2009, 12:31:42 am by lookang »