Home > Community > Forums > Custom IC SKILL > skill assoc : How does assoc command of skill work with anumerical value ?

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 assoc : How does assoc command of skill work with anumerical value ? 

Last post Mon, May 13 2013 5:38 AM by Giora Karni. 2 replies.
Started by Giora Karni 11 May 2013 10:29 AM. Topic has 2 replies and 561 views
Page 1 of 1 (3 items)
Sort Posts:
  • Sat, May 11 2013 10:29 AM

    • Giora Karni
    • Not Ranked
    • Joined on Tue, Jul 27 2010
    • Haifa, Israel
    • Posts 4
    • Points 65
    skill assoc : How does assoc command of skill work with anumerical value ? Reply
    How does assoc command of skill work ?
     
    After looking closely into several numerical examples, I got the following findings:
     a = '( ( 20n   xx xx) ( 40n yy yy) (50n zz zz) ) assoc( 2e-08 a)   returns:    (2e-08 xx xx) assoc( 4e-08 a)   returns:    (4e-08 yy yy) assoc( 5e-08 a)   returns:    nil  ß       ?????

    but if I try:
    a = '( ( 20e-09   xx xx) ( 40e-09 yy yy) (50e-09 zz zz) ) assoc( 2e-08 a)   returns:    (2e-08 xx xx) assoc( 4e-08 a)   returns:    (4e-08 yy yy)

    assoc( 5e-08 a)   returns:    (5e-08 zz zz)  
     
    I suspect that there is a numerical bug here. It looks like 5e-08 ≠ 50n . I noticed that for 20n,40n,80n,160n (power of 2) the problem doesn't appear.

    virtuoso version ICADV12.1-64b 03/15/2013 18:29 (sjfnl166)

    Giora Karni
    • Post Points: 20
  • Sat, May 11 2013 3:30 PM

    Re: skill assoc : How does assoc command of skill work with anumerical value ? Reply

    Hi Giora,

    It's not a numerical bug.  It appears to be a rounding error, within the bounds of what might be expected.

    assoc simply uses equal() (which is the same as the == operator) to compare the values, and one should never use equal on floating point numbers because there is a possibility of rounding errors (there have been posts related to this before on this forum). Note that this is not something peculiar to SKILL, but in many languages (including C).

    It appears that there has been a subtle change in ICADV12.1 (and IC616) in how the suffix notation is converted to a floating point number.

    If you do: 50n-5e-8 you'll see there is a small residual error -  6.617444900424221e-24

    If I do:

    > printf("%.20g\n" 50n)
    5.0000000000000004355e-08

    > printf("%.20g\n" 5e-8)
    4.9999999999999997737e-08

    And If I do 

    > printf("%.20g\n" 50*1e-9)
    5.0000000000000004355e-08

    In general, as I said, you should not use equal (or assoc) to compare floating point numbers, because a small rounding error (often just due to a single bit difference in the mantissa) can mean they are no longer precisely equal.

    You could use something like:

    procedure(myAssoc(val lst @optional (epsilon 1e-15))
      car(exists(entry lst abs(car(entry)-val)<epsilon))
    )

    Then you can use myAssoc instead of assoc in the above examples (this is using an absolute error criteria, but you could write it to have a relative error criteria if you preferred).

    Kind Regards,

    Andrew.

    • Post Points: 20
  • Mon, May 13 2013 5:38 AM

    • Giora Karni
    • Not Ranked
    • Joined on Tue, Jul 27 2010
    • Haifa, Israel
    • Posts 4
    • Points 65
    Re: skill assoc : How does assoc command of skill work with anumerical value ? Reply

    Hi Andrew,

    Thank you for a full explanation and solution.

    with regards,

    Giora

    • Post Points: 5
Page 1 of 1 (3 items)
Sort Posts:
Started by Giora Karni at 11 May 2013 10:29 AM. Topic has 2 replies.