Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: Programming Issue  (Read 1184 times)

Kogan Loloklam

  • Bay Watcher
  • I'm suffering from an acute case of Hominini Terravitae Biologis. Keep your distance!
    • View Profile
Programming Issue
« on: October 10, 2011, 08:28:57 pm »

I'm playing around with haXe and flash in Flash Develop.
I'm trying to learn variables and random generators.
I'm running into an odd situation...
Spoiler: This code (click to show/hide)
Output  "0.07200000000000001" for Food Check Drop, 0.03 for Food Check Percentage, and 0.042 for Food Check base.

Now to say I don't know where it came from is a understatement.  It seems to work normally most of the time. What did I do wrong? Any idea?

The point of doing this is to create a percentage to work with that operates to 6 digits. (xx.xxxx)
This gives me lots of leeway to create a system that is affected by many different small details and check against percentage-based pass/fail checks. While most parts I created of that system tested independently worked fine, This part had such an odd output number that I think something I'm doing is fundamentally wrong.
I ran multiple tests when I discovered this, and it outputted similar results sometimes.

Sometimes it works fine. Here's a few examples of what it spat out when it didn't:
Spoiler (click to show/hide)

So yea, I'm pretty darn confused!
Logged
... if someone dies TOUGH LUCK. YOU SHOULD HAVE PAYED ATTENTION DURING ALL THE DAMNED DODGING DEMONSTRATIONS!

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: Programming Issue
« Reply #1 on: October 10, 2011, 11:10:42 pm »

Umm, could you give some sample output of what you want it to produce?

Rysith

  • Bay Watcher
    • View Profile
Re: Programming Issue
« Reply #2 on: October 11, 2011, 01:17:08 am »

You're almost certainly running into floating-point imprecision issues. See Wikipedia for the technical details.

My advice, if you want absolute precision, would be to do all of your operations in whole numbers representing ten-thousandths of a percent, to give you your 6 digits as xxxxxx (since integers can be represented with infinite precision, and I would hope that Flash represented integer quantities as integers).
Logged
Lanternwebs: a community fort
Try my orc mod!
The OP deserves the violent Dwarven equivalent of the Nobel Peace Prize.

Kogan Loloklam

  • Bay Watcher
  • I'm suffering from an acute case of Hominini Terravitae Biologis. Keep your distance!
    • View Profile
Re: Programming Issue
« Reply #3 on: October 11, 2011, 01:57:07 am »

Umm, could you give some sample output of what you want it to produce?

xx.xxxx
instead of
xx.xxxxxxxxxxxxxxxxxx

...
Reading that, I think your right. Interesting. I never knew binary wasn't able to represent 0.1 accurately. It's fun to learn limitations! That'll be a headache if I ever wish to show the results in a decimal format though, even if I keep all operations in whole numbers. I imagine I'll find some workaround if I ever have to use it though. Still, outputting information is easier than dealing with data that isn't operated on correctly.
Logged
... if someone dies TOUGH LUCK. YOU SHOULD HAVE PAYED ATTENTION DURING ALL THE DAMNED DODGING DEMONSTRATIONS!