1. BuzzStarField's Avatar
    I found another bug in the app's astronomical clock. An update has been submitted and awaits approval, but I thought you might be interested in seeing where the mistake is. Anyone who claims that Air is an "easy" development language is either not being honest or does not know what he/she is talking about. Trying to do any non-trivial operation can be a royal pain...

    Here is the a line of AS3 code that works frequently but not always:
    var dint:int = 367*y - int((7*(y + (int(m+9)/12)))/4) + int(275*m/9) + D - 730530

    Here is the corrected code in the soon-to-be available update. Can you spot the differences? Hint: I added four new brackets.
    var dint:int = 367*y - int((7*(y + (int((m+9)/12))))/4) + int((275*m)/9) + D - 730530;

    Here is the equivalent line of code in the java version that I am working on. It works as intended after simply copying it from the reference book:
    int dint = 367*Y - (7*(Y + ((M+9)/12)))/4 + (275*M)/9 + D - 730530
    bbfan1040 and Morten like this.
    05-02-12 12:21 PM
  2. borceg's Avatar
    Ahhh, that math/operator precedence
    05-02-12 12:33 PM
  3. edmeister's Avatar
    Yes I see it has something to do with floating point operations versus integer, and when the "rounding" or truncation is meant to occur (before or after the division by 12).
    05-02-12 12:43 PM
  4. BuzzStarField's Avatar
    Ahhh, that math/operator precedence
    Close but no cigar for you. The brackets in the Java version are all about mathematical precedence. However, the extra "ints" and brackets in the AS3 version are there to force the runtime to handle all input and output values as integers, e.g. dividing 5 by 3 gets you 1 (and not the usual 1.666666).
    05-02-12 12:46 PM
  5. BuzzStarField's Avatar
    Yes I see it has something to do with floating point operations versus integer, and when the "rounding" or truncation is meant to occur (before or after the division by 12).
    You get the prize.

    The formula in question calculates the exact number of full days since Jan 1, 2000 to the present date. It takes leap days into account for every February that has 29 days instead of the usual 28. Leaving non-integral values in the mix messes up the calculation. Leaving only some non-integral values in the equation causes a nasty intermittent error that is really hard to trace.
    05-02-12 12:51 PM
  6. Innovatology's Avatar
    Actually, AS3 is doing the "correct" thing mathmatically. Older compilers try and fail to be smart and "optimize" the operation by rounding (or worse, truncating) fractions depending on the destination variable. And they often get it wrong. Best practice programming is to cast explicitly.
    05-02-12 06:01 PM
  7. BuzzStarField's Avatar
    Actually, AS3 is doing the "correct" thing mathmatically. Older compilers try and fail to be smart and "optimize" the operation by rounding (or worse, truncating) fractions depending on the destination variable. And they often get it wrong. Best practice programming is to cast explicitly.
    You are correct and when using AS3, I always cast first and ask questions later. With Java I didn't need to do that because the language is defined to treat the various types according to strict rules. With ints, the rule is to truncate every result to make it into an int. When you declare a variable as int or write a literal with no decimal part, and then divide one by the other, you are guaranteed to get an int as a result. In AS3, you will get a Number type which you have to cast as an int (if that's what you want.)
    05-02-12 06:45 PM
  8. BuzzStarField's Avatar
    My original post was about an update to correct a bug. To tell the truth, I am a bit concerned. I got an email from RIM saying that the release was approved. That was 8 hours ago and I have not yet been notified by App World. Has anyone else received an indication that the update is available?
    05-02-12 06:56 PM
  9. stuka85's Avatar
    not yet buzz, ill check in 20 mins
    BuzzStarField likes this.
    05-02-12 07:02 PM
  10. bbfan1040's Avatar
    You are all truly amazing! I am so thankful that you can deal with that level of accuracy. Even Da Vinci would be impressed!
    05-02-12 07:09 PM
  11. BuzzStarField's Avatar
    You are all truly amazing! I am so thankful that you can deal with that level of accuracy. Even Da Vinci would be impressed!
    Actually, I stand on the shoulders of giants. Da Vinci and Galileo were long ago making telescopes and mapping the universe and I am just following meekly in their footsteps. My app uses Kepler's laws (as refined by Newton) to calculate the planets' orbits. I am just copying out the equations (and not doing such a great job as it turns out).

    I'm lucky because modern technology makes it possible for me to bring this stuff to all of you. And it does take a certain amount of artistry and tenacity to build an app so I guess I am a positive force in society after all.
    BB_Bmore likes this.
    05-02-12 07:36 PM
  12. app_Developer's Avatar
    I'm curious, did you consider just doing the calculations in native code? Why did you decide not to (if you don't mind sharing your thinking with us BB10/PB newbies)
    05-02-12 07:39 PM
  13. BuzzStarField's Avatar
    I'm curious, did you consider just doing the calculations in native code? Why did you decide not to (if you don't mind sharing your thinking with us BB10/PB newbies)
    Way back last year at launch, there was only one way to write an app - AS3. Later came Webworks. So I was forced to code everything in AS3.

    It only became possible to include native code (c/c++) in an Air app earlier this year concurrent with OS 2.0. You have to write what is called an "ActionScript Native Extension" and map it to your Air app using a special configuration file. It's not exactly efficient but a good case could be made to write an ANE to offload all the number crunching in my app to native code. This will be the first step toward porting the entire app to native over the next unspecified period of time.
    app_Developer likes this.
    05-02-12 07:46 PM
  14. stuka85's Avatar
    Just updated =)
    BuzzStarField likes this.
    05-02-12 07:47 PM
  15. BuzzStarField's Avatar
    not yet buzz, ill check in 20 mins
    Okay, it's finally arrived and the universe is unfolding as it should.
    05-02-12 07:47 PM
  16. FF22's Avatar
    Upgrading now.
    05-02-12 11:34 PM
LINK TO POST COPIED TO CLIPBOARD