My computer got stupid. 0 + 20 = 19.921875!!! Help me!
Asked Answered
R

5

8

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!

Russi answered 30/7, 2010 at 16:35 Comment(1)
It probably has to do something with floating points, but I've never dabbled in actionscript. Cast to int maybe?Dore
H
23

_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.

Helm answered 30/7, 2010 at 17:0 Comment(3)
Very well explained. I have no experience in actionscript and even I went "aaahh yeah that makes sense! i get it!"Monochord
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.Nancinancie
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.Nancinancie
W
1

rounding up it seems

Worse answered 30/7, 2010 at 16:59 Comment(0)
N
0

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?

Nitro answered 30/7, 2010 at 16:39 Comment(0)
E
0

The "floating problem" also occurred in component property box in Flash CS5. After a fresh installation, CS5 seems crazy with component properties, and gives 20.00001 after you type 20.

Updating your software thanks to Adobe Application Manager correct this problem.

Elfredaelfrida answered 18/5, 2011 at 12:40 Comment(0)
N
-1

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.

Nancinancie answered 1/8, 2010 at 10:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.