Go Back   kirupaForum > Flash > ActionScript 3.0

Reply
 
Thread Tools Display Modes
Old 11-21-2009, 12:26 AM   #1
audiohominis
flash eating virus
Afrostyle Double-You Tee Eff! (Question Mark) Is this a known bug?

It's certainly new to me?
PHP Code:
trace(1*.1);//0.1
trace(2*.1);//0.2
trace(3*.1);//0.30000000000000004
trace(4*.1);//0.4
trace(5*.1);//0.5
trace(6*.1);//0.6000000000000001
trace(7*.1);//0.4
trace(8*.1);//0.5
trace(9*.1);//0.6000000000000001 
Double-You Tee Eff indeed.

F.P.10
audiohominis is offline   Reply With Quote

Sponsored Links (Guests Only) - Register | Need Help?
 

Old 11-21-2009, 12:39 AM   #2
Shaedo
Registered User
 
Shaedo's Avatar
Location Australia, Sydney

Posts 572
I get
ActionScript Code:
0.1
0.2
0.30000000000000004
0.4
0.5
0.6000000000000001
0.7000000000000001
0.8
0.9
but I see your point (I think)
EDIT and this
trace(1*.1*10);//1
trace(2*.1*10);//2
trace(3*.1*10);//3.0000000000000004
trace(4*.1*10);//4
trace(5*.1*10);//5
trace(6*.1*10);//6.000000000000001
trace(7*.1*10);//7.000000000000001
trace(8*.1*10);//8
trace(9*.1*10);//9
Shaedo is offline   Reply With Quote
Old 11-21-2009, 01:05 AM   #3
Krilnon
≈ ≠ =
 
Krilnon's Avatar
Location Rochester, NY

Posts 7,183
It's not a bug, it's just what you get when using floating point numbers: http://en.wikipedia.org/wiki/Floatin...uracy_problems
Krilnon is online now   Reply With Quote
Old 11-21-2009, 01:09 AM   #4
Scythe
Mwa ha ha
 
Scythe's Avatar
Location USA

Posts 338
Getting .4 when you multiply 7 by .1 would be a bug, but I assume that's a typo on the OP's part.

Observation: none of the four posters in this thread have signatures.

Last edited by Scythe; 11-21-2009 at 01:11 AM..
Scythe is offline   Reply With Quote
Old 11-21-2009, 01:48 AM   #5
BeerOclock
Registered User
When you're checking to see if one Number equals another Number, its common practice to use a range, instead of a strict equals.

Using the results you found, the following would print "WTF"

ActionScript Code:
var n1:Number = 0.6;
var n2:Number = 6 * 0.1;

if (n1 == n2) {
   trace ("floating point numbers play nicely");
} else {
   trace ("WTF");
}


A common solution is to do something like this:

ActionScript Code:
const EPSILLON:Number = 0.000001;

var n1:Number = 0.6;
var n2:Number = 6 * 0.1;

if ( Math.abs(n1-n2) > EPSILLON) {
   trace ("floating point numbers play nicely");
} else {
   trace ("WTF");
}


In this case, the first statement is printed. Youre basically saying "If n1 is equal to, or reaaaaaally close to n2..."
BeerOclock is offline   Reply With Quote
Old 11-21-2009, 01:50 AM   #6
Scythe
Mwa ha ha
 
Scythe's Avatar
Location USA

Posts 338
I believe you mean
Code:
if ( Math.abs(n1-n2) < EPSILLON)
and not
Code:
if ( Math.abs(n1-n2) > EPSILLON)
Scythe is offline   Reply With Quote
Old 11-21-2009, 01:59 AM   #7
BeerOclock
Registered User

I believe you're right.
BeerOclock is offline   Reply With Quote
Old 11-22-2009, 02:55 AM   #8
audiohominis
flash eating virus
Thank you all gentlemen.

I have a follow-up question then.
I learned that division by 10, in contrast, does not produce funny numbers. Then again, my understanding is that division is not as efficient as multiplication, so which way would you go?
Thanks.

Quote:
Originally Posted by Scythe View Post
Observation: none of the four posters in this thread have signatures
Though one of them has a particularly delicious handle, you feel me?
audiohominis is offline   Reply With Quote
Old 11-22-2009, 05:58 AM   #9
sekasi
[ Fiscal conservatist ]
 
sekasi's Avatar
Location Melbourne + Stockholm

Posts 1,822
Division does not make this problem go away, it just manifests in different ways. As above posters have already mentioned this is a problem in all Floating point systems. Since they aren't 'real' numbers, this is the downside.

Most of the time you can ease the pain of the results by some overhead. If there are any particular scenarios where this is breaking your applications, feel free to post the relevant code and I'm sure you'll get a good answer.

__________________
* [ Flash portfolio ] http://www.erikhallander.com/
* [ Flash blog ] http://www.erikhallander.com/blog/
* [ Work @ ] http://www.visualjazz.com.au
sekasi is offline   Reply With Quote
Reply

Tags
bug, impresision


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 06:28 PM.

SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple. flash components
Creative web apps. Make your own free flash banners and photo slideshows.
Check out the great, high-quality flash extensions. Buy or sell stock flash, video, audio and fonts for as little as 50 cents at FlashDen.

Flash Transition Effects

Flash Effect Tutorials

Digicrafts Components
Flash effects. Art without coding. Upload, publish, deliver. Secure hosting for your professional or academic video, presentations & more. Screencast.com
Streamsolutions Content Delivery Networks Flipping Book - page flip flash component.
Flash-Gallery.com - Get your flash photo gallery (flash component or swf gallery Learn how to advertise on kirupa.com
 

cdn
content delivery network (cdn)

Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd. Copyright 2010 - kirupa.com Copyright 2010 - kirupa.com