• Skip to main content
  • Skip to search
  • Skip to footer
Cadence Home
  • This search text may be transcribed, used, stored, or accessed by our third-party service providers per our Cookie Policy and Privacy Policy.

  1. Community Forums
  2. Custom IC SKILL
  3. Simple counting with number 0.07 ?

Stats

  • Locked Locked
  • Replies 5
  • Subscribers 143
  • Views 7279
  • Members are here 0
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Simple counting with number 0.07 ?

Bertas
Bertas over 2 years ago

Hi guys, I have one question, maybe I do something wrong or I don't wake up properly today :) but I can't find an answer why this happen.

I will make it simple .... its just counting in CIW

for number 0.07

x=(0.07/(2*0.005))  => 7.0 

y=truncate(x) => 7 (integer number)

z=x-y =>  I would expect 0 but system give me 8.881784e-16

When I put any other number like like 0.09 or 0.08  .... 2.0 atc always get expected result 0

Why is that ?

Robert

  • Cancel
  • Andrew Beckett
    Andrew Beckett over 2 years ago

    Robert,

    This problem has come up countless times on these forums, and on the internet in general - for any programming language that uses IEEE floating point representation (so C, Pascal, Python, SKILL, etc, etc). For example:

    • Round-off error (Wikipedia)
    • Why you should never compare two floating point numbers with equals (==) in SKILL?
    • Numerical dynamic range (precision) in SKILL (this one was written by me)

    Andrew

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • p94todorov
    p94todorov over 2 years ago

    Hello Bertas,

    this is due to the machine epsilon and the representation of each number in the computer. In this case, your 7.0 is not exactly that much, but there is some rounding accuracy related to the number of bits used to represent the number. You can confirm that by printing more significant digits for both of your examples:

    Hence you can see where this 8.881784e-16 comes from. It's considered a good practice to avoid the equal operator (==) when comparing expressions on both sides, since such unpredicted results might result in spending some time debugging something that looks weird at first sight. I would say that the more stable and general way to approach number comparisons is:

    num1 = a + b - ... - p

    num2 = m - n + ... +q

    epsilon = 1e-6 ;(or whatever accuracy works for you, usually related to the design grid when drawing somethng)

    if(abs(num1-num2) < epsilon then ...

    Also, you will notice that if you put the expression evaluation outside the if operator your code will behave more optimal in terms of speed.

     

    Hope this explains the situation.

    Regards,

    Petar

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • p94todorov
    p94todorov over 2 years ago in reply to p94todorov

    Well, Andrew has already replied while I was typing the above, so sorry for the duplication.

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • Bertas
    Bertas over 2 years ago

    Thank you guys for the explanation. I got the point. It's clear to me now. Thumbsup

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • skillUser
    skillUser over 2 years ago in reply to Bertas

    One more thing, SKILL now has the nearlyEqual function:

    x=(0.07/(2*0.005))
    7.0
    x == 7.0  ;; BAD IDEA, AS NOTED ABOVE
    => nil
    nearlyEqual(x 7)
    => t
    
    • Cancel
    • Vote Up 0 Vote Down
    • Cancel

Community Guidelines

The Cadence Design Communities support Cadence users and technologists interacting to exchange ideas, news, technical information, and best practices to solve problems and get the most from Cadence technology. The community is open to everyone, and to provide the most value, we require participants to follow our Community Guidelines that facilitate a quality exchange of ideas and information. By accessing, contributing, using or downloading any materials from the site, you agree to be bound by the full Community Guidelines.

© 2025 Cadence Design Systems, Inc. All Rights Reserved.

  • Terms of Use
  • Privacy
  • Cookie Policy
  • US Trademarks
  • Do Not Sell or Share My Personal Information