views:

256

answers:

4

You know it's bugger-all when your computer can't get a sum right!

I have not the slightest idea why this is happening:

_root.attachMovie("clippy","aClip",_root.getNextHighestDepth());

trace("alpha 1 = "+aClip._alpha);
aClip._alpha = 0;
trace("alpha 2 = "+aClip._alpha);
aClip._alpha += 20;
trace("alpha 3 = "+aClip._alpha);
aClip._alpha = 20;
trace("alpha 4 = "+aClip._alpha);

Output is:

alpha 1 = 100
alpha 2 = 0
alpha 3 = 19.921875
alpha 4 = 19.921875

19.921875 should be 20! :(

I'm going to cry. Does my processor have cancer? Alzheimer? Who said computers don't make mistakes?

P.S. I also used aClip._alpha=Math.round(aClip._alpha) but the trace was the same!

A: 

If I remember right, flash stores alpha internally with some weird crazy value, something like 0..240 (I don't think it's 255). Could it be that it's taking 20 to be 20%, and after rounding, 19.92 is the nearest value once it's converted back to a percentage?

izb
+1  A: 

rounding up it seems

Johnny Quest
+16  A: 

_alpha values are stored as a byte (I should say 8 bits - an integer value from 0 to 255)

When you set an _alpha value, you use a percentage. When you retrieve it, it yields an exact decimal representation of the percentage.

aClip._alpha = 20;

20% of 256 is 51.2, since it's stored as an integer, it will get floored to 51.

Then,

51 / 256 * 100 is how Flash gives it back to you, which is 19.921875.

Bertrand Marron
Very well explained. I have no experience in actionscript and even I went "aaahh yeah that makes sense! i get it!"
rownage
Thanks. Now I understand.
navand
Good explanation. But nevertheless, IMO an account of somewhat bad design on Macromedias/Adobes part, because 1) it disregards important parts of the getter/setter principle - i mean you set a variable to 20, but get 19.!@%$^ right after and 2) will break straightforward, logical and readable code such as `if(_alpha == 20)` because you can never know for sure what `_alpha` will equal to even with complete control over it. Of course `int(_alpha)` would help, but then again simply patches holes on top of this little mess. I understand the reasons for not abstracting this futher though - speed.
amn
They did the right thing with ActionScript 3 though - `alpha` now is hard-constrained to a range from `0` to `1` and will get you exactly what you put into it, regardless. Excluding the IEEE floating point quirks of course, which are part of the trade here.
amn
A: 

Regardless of the validity of other answers, there are known and well-documented artefacts of the floating point implementation that among others, Adobe Flash Player uses. The following code, for instance, will produce a slightly incorrect (if there is such thing in mathematics) output of 1.0010000000000001:

trace(0.1009 + 0.9001);

All this, like I said, is part of using a IEEE floating point specification implementation.

amn