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

## Email

Recipients email * (separate multiple addresses with commas)

Message *

 Send yourself a copy

## Subscribe

Intro copy of the newsletter section here, some intro copy of the newsletter. Instruction of how to subscribe to this newsletter.

First Name *

Last Name *

Email *

Company / Institution *

 Send Yourself A Copy

# 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 1386 views
• #### Tue, Sep 29 2009 2:17 PM

• sri125
• Joined on Mon, Sep 28 2009
• Posts 4
• Points 35
SKILL arithmetic comparisons
 SKILL is notorious for the way it stores numbers in memory. e.g variable w, whichshould 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
• Joined on Fri, Sep 19 2008
• Austin, TX
• Posts 2,587
• Points 15,915
Re: SKILL arithmetic comparisons
 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
 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))+abstolabs(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
• Joined on Mon, Sep 28 2009
• Posts 4
• Points 35
Re: SKILL arithmetic comparisons
 Thanks Lawresce and Andrew.
• Post Points: 5