Home > Community > Forums > Custom IC SKILL > SKILL arithmetic comparisons

Email

* Required Fields

Recipients email * (separate multiple addresses with commas)

Your name *

Your email *

Message *

Contact Us

* Required Fields
First Name *

Last Name *

Email *

Company / Institution *

Comments: *

 SKILL arithmetic comparisons 

Last post Wed, Sep 30 2009 7:47 AM by sri125. 3 replies.
Started by sri125 29 Sep 2009 02:17 PM. Topic has 3 replies and 1326 views
Page 1 of 1 (4 items)
Sort Posts:
  • Tue, Sep 29 2009 2:17 PM

    • sri125
    • Not Ranked
    • Joined on Mon, Sep 28 2009
    • Posts 4
    • Points 35
    SKILL arithmetic comparisons Reply

     SKILL is notorious for the way it stores numbers in memory. e.g variable w, which

    should be 0.4 gets stored as 0.3999999999999 or something like that.

     

    So when you use it in 'if-else' loop like:

    if( w == 0.4 then 

        block good;

    else

       block bad;

     )

     

    You will end up in block bad unless you do something like

     

    abs( w - 0.4 ) < 0.000001 

    or something like above.

     

    Is there an elegant way to handle this in SKILL ?

     

    thanks

     

     

     

    • Post Points: 20
  • Tue, Sep 29 2009 2:52 PM

    • skillUser
    • Top 10 Contributor
    • Joined on Fri, Sep 19 2008
    • Austin, TX
    • Posts 2,535
    • Points 14,940
    Re: SKILL arithmetic comparisons Reply

    There is nothing "notorious" or special with the way that SKILL stores floating point numbers compared to other programming languages.  A floating point number is typically stored in an IEEE (754) format which is comprised of a sign, an exponent and a mantissa (or significand).  Most decimal numbers do not have an exact representation in the binary floating-point storage format used in single-precision and double-precision data types.  So in order to be able to test for equality in a reliable fashion, the best thing is to subtract one number from the other and test the remainder against a small number, exactly as you have shown.  The small number could be a fraction of the input number so that the test works equally well for large values and smaller values.

    Sorry, that's just how things are for floating point number comparison, I'm not sure if there is any better way?

    Regards,

    Lawrence.

    • Post Points: 20
  • Tue, Sep 29 2009 3:59 PM

    Re: SKILL arithmetic comparisons Reply
    As Lawrence says, you get exactly the same kind of rounding errors in C and all languages using IEEE floating point numbers. In fact whenever this comes up with someone saying this is a bug in SKILL, I show a C program with the same behaviour.

    The trick is to write a function to do floating point "equality" checks. If you know the magnitude of the numbers, you can use a fixed size "epsilon" as in your example.

    Or you can do something like:

    procedure(myCompare(a b @key (reltol 1e-6) (abstol 1e-12))
    let((tol)
    tol=reltol*max(abs(a) abs(b))+abstol
    abs(a-b) < tol
    )
    )

    This has both a relative and absolute tolerance in the comparison.

    Regards,

    Andrew
    • Post Points: 20
  • Wed, Sep 30 2009 7:47 AM

    • sri125
    • Not Ranked
    • Joined on Mon, Sep 28 2009
    • Posts 4
    • Points 35
    Re: SKILL arithmetic comparisons Reply

     Thanks Lawresce and Andrew.

    • Post Points: 5
Page 1 of 1 (4 items)
Sort Posts:
Started by sri125 at 29 Sep 2009 02:17 PM. Topic has 3 replies.