• 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. problem facing with modf function in skill

Stats

  • Locked Locked
  • Replies 3
  • Subscribers 143
  • Views 14510
  • 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

problem facing with modf function in skill

iqbalsahid
iqbalsahid over 7 years ago

Hi,

I am using modf function in my script.

where modf(1.26 0.315) giving 0 but modf(0.945 0.315) giving 0.315.

I want 0 for both cases. I dont know what to do for that.

And also equal(modf(0.945 0.315) 0.315) is giving nil instead of t because modf(0.945 0.315)  is giving 0.315.

I am confuse. Please help.

  • Cancel
Parents
  • Andrew Beckett
    Andrew Beckett over 7 years ago

    This is because of floating point rounding errors. It's nothing to do with SKILL (other languages will have similar problems) - similar questions (maybe not with modf but with other calculations) have come  up on numerous occasions on the forums.

    You should never do an equal type check on floating point numbers because it's highly likely they are not precisely equal. Floating point numbers are represented in IEEE floating point format which uses a binary mantissa and so .1 in decimal ends up being a recurring fraction (similar to how 1/3 is 0.3333 recurring in decimal). Because of this, you easily can end up with a rounding error due to a loss of a bit in the 53rd binary digit of the mantissa.

    To illustrate this:

    modf(0.945 0.315)-0.315
    -5.551115e-17

    So as you can see, it's very slightly less than 0.315 but when displayed with fewer digits of precision, it appears to be 0.315. If I generate the 0.945 slightly differently:

    modf(0.315*3 0.315)
    5.551115e-17

    Even now you see it's not quite 0 - slightly above. Another example:

    0.315*3-0.945
    1.110223e-16

    What  you have to do is do equality checks using a range of values - sometimes things like:

    abs(answer - expectedAnswer) < someTolerance

    For your modf, if the numbers have three digits of precision, maybe you'd be better multiplying by 1000, rounding, and then using mod() instead (i.e. integer modulus)/

    Regards,

    Andrew.

    • Cancel
    • Vote Up +1 Vote Down
    • Cancel
  • iqbalsahid
    iqbalsahid over 7 years ago in reply to Andrew Beckett

    Thanks alot. I did not thought like that. That will help me.

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
Reply
  • iqbalsahid
    iqbalsahid over 7 years ago in reply to Andrew Beckett

    Thanks alot. I did not thought like that. That will help me.

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
Children
No Data

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