-
How do I fix this math error
Please test this code. I get totally incorrect output.
crx ends holding the value -4.44089209850063e-16.
tmpVar ends holding the value 100
The output is 100!!!!! crx is NOT added to tmpVar2.
Code:
var vax = -2.25;
var vay = 11.875;
var vaz = -0.187499999999829;
var vbx = 0.186139126359066;
var vby =-0.982400944672849;
var vbz = 0.0155115938632414;
var crx=(vby*vaz - vbz*vay);
var cry=(vbz*vax - vbx*vaz);
var crz=(vbx*vay - vby*vax);
var tmpVar = 100;
var tmpVar2 =tmpVar - crx;
trace(crx);
trace(tmpVar2);
What is happening here??
shipstern
-
I am not sure if this will help, but it just may:
Original Code:
Code:
var crx=(vby*vaz - vbz*vay);
var cry=(vbz*vax - vbx*vaz);
var crz=(vbx*vay - vby*vax);
The difference in the 2nd is the added parenthesis ("(" and ")").
Modded Code:
Code:
var crx=(vby*vaz) - (vbz*vay);
var cry=(vbz*vax) - (vbx*vaz);
var crz=(vbx*vay) - (vby*vax);
I am not sure if in the first line you would want it to subtract the product of vby and vaz from vbz then multiply that answer by vay. Try that code. Maybe it works.
Last edited by Daboarder; 12-26-2005 at 05:21 PM.
-
Daboarder
No it makes no difference.
The error is still there. Flash will not perform any math operations on crx once it contains a high precision number.
shipstern
-
Unfortunately, there is no error here... the problem is accuracy.
Originally Posted by Merriam-Websters
Main Entry: ac·cu·ra·cy
Pronunciation: 'a-ky&-r&-sE, 'a-k(&-)r&-
Function: noun
Inflected Form(s): plural -cies
1 : freedom from mistake or error : CORRECTNESS
2 a : conformity to truth or to a standard or model : EXACTNESS
b : degree of conformity of a measure to a standard or a true value -- compare PRECISION 2a
Note 2b. The problem with computers is they prefer to be exact, but can't always be. So, the IEEE came up with a way to represent non-exact numbers, we call them float values. Back in the good ol' days, we had secondary processors to handle these nasty little values for us, and, to get these processors to work, we had to define how to represent these numbers... this is where accuracy becomes a problem.
Computers do a remarkable job dealing with large, whole numbers, small whole numbers, large floats and small... but when you start mixing them, you get issues.
Code:
trace( crx * 20 );
trace( crx * 50 );
trace( .011 - crx );
gives us
Code:
-4.44089209850063e-16
100
-8.88178419700125e-15
-2.22044604925031e-14
0.0110000000000004
As you can see, crx is still working fine... just that when you combine crx with such a large number as 100 (heck, even 1 is a large number to a computer), -4.44*10^-16 becomes a very, very insignificant number. To fit inside of the storage space in memory for a float, after the math is completed, the number has to be set to a specific number of decimal places.
1e2 (that's 100) + 4.44e-16 = 1.00000000000000000444 or, 1e-20... that's just too many decimal places for Flash to handle... so, it rounds. Unfortunately, during your rounding, the 4.44e-16 is lost. Flash isn't ignoring crx, it's just so insignificant, that it's getting lost.
If you can read this, you're in the right place.
-
Solved !!!
wombatLove
Thank you heaps. To be honest I now realize that I did not fully understand how the exponent affected the floating point value.
Your explanation sent me to do some study. Being self taught I sometimes amaze even myself at the wholes (such basic things) in my understanding.
It's so hard to find them and I am always gratefull when I have the chance to benifit from such well presented assistance.
Daboarder thank you also ... I guess I was the only one in the dark here!!
shipstern
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|