• 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. round vs pcRound (rounding floating numbers)

Stats

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

round vs pcRound (rounding floating numbers)

Adrian Nistor
Adrian Nistor over 6 years ago

Hi All,

I am aware of the problem with floating point numbers, but I have 2 questions based on the next example:

x = (26.4-14.85) / 2.0
5.775

round(x  / 0.01) * 0.01
5.77

pcRound(x  / 0.01) * 0.01
5.78

Question 1.) Can someone explain me the pcRound parameters ? (the documentation didn't help me to understand it properly)
pcRound( n_num [ f_precision ]  [ x_tolerance ] )

Question 2.) What should I generally use: pcRound() or round()  ?

Thanks,
BR,
Adrian

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

    Hi Adrian,

    Having looked at the description and the code for pcRound, I really cannot see the point of pcRound - it seems an unnecessarily complicated way of not quite rounding to the nearest integer. It was intended for use in PCells - I've never used it though. I can't actually see the benefit of the controls that you have for the additional arguments. I found the original CCR where it was enhanced to provide control, and another which was an attempt to explain what it did - it's as clear as mud though.

    So I'd stick to using round - at least it's obvious what it is doing.

    I would have thought that if rounding to the nearest integer, you wouldn't be that bothered whether the number is 3.499 or 3.49999 or 3.500001 or whatever - rounding errors occurring half way between the numbers would mean that you are close to either direction and it probably doesn't matter - it just depends on the application. If it depends on the application, you want to have precise control over how rounding works for your application rather than depending on some mysterious function.

    Regards,

    Andrew.

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
Reply
  • Andrew Beckett
    Andrew Beckett over 6 years ago

    Hi Adrian,

    Having looked at the description and the code for pcRound, I really cannot see the point of pcRound - it seems an unnecessarily complicated way of not quite rounding to the nearest integer. It was intended for use in PCells - I've never used it though. I can't actually see the benefit of the controls that you have for the additional arguments. I found the original CCR where it was enhanced to provide control, and another which was an attempt to explain what it did - it's as clear as mud though.

    So I'd stick to using round - at least it's obvious what it is doing.

    I would have thought that if rounding to the nearest integer, you wouldn't be that bothered whether the number is 3.499 or 3.49999 or 3.500001 or whatever - rounding errors occurring half way between the numbers would mean that you are close to either direction and it probably doesn't matter - it just depends on the application. If it depends on the application, you want to have precise control over how rounding works for your application rather than depending on some mysterious function.

    Regards,

    Andrew.

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
Children
  • Adrian Nistor
    Adrian Nistor over 6 years ago in reply to Andrew Beckett

    Hi Andrew, thanks for confirming my thoughts regarding pcRound Slight smile  
    I understand what you say regarding the rounding.
    My problem is that, because of this, some PCells are not behaving in the same way for some of the parameters.

    Trying to enlarge the first testcase to demonstrate my problem, I have the following situations:

    procedure( myCalc(x)
         let( (a)
             a = ( 26.40 - x ) / 2.0
             round(a/0.01) * 0.01 ;;return
         )
    )  
    for(i 1481 1490 printf("%f\n" myCalc(i/100.0)) )

    5.790000
    5.790000
    5.780000
    5.780000
    5.770000
    5.770000
    5.770000
    5.760000
    5.750000
    5.750000

    Because of the rounding issue, I get some unexpected & repeated numbers in my sequence .... e.g.: I was expecting a 5.76. not 5.77 on the 7th position in my list.

    Unfortunately ("unfortunately" because "pcRound" looks so strange) pcRound doesn't give me these kind of issues...

    Another solution for me is to round 2 times, to get rid of the additional digits of the floting number ....  but this is ugly:
    a=  round( a/0.001) * 0.001
    round(a/0.01) * 0.01

    If you have in mind any other more reliable solution, please let me know.
    Thanks,
    BR,
    Adrian

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • Andrew Beckett
    Andrew Beckett over 6 years ago in reply to Adrian Nistor

    Hi Adrian,

    Here's what I'd do:

    The small offset moves you above the rounding error which ends you at n.49999999999

    procedure( myCalc(x)
         let( (a)
             a = ( 26.40 - x ) / 2.0
    	 ;printf("debug: %.16g\n" a/0.01)
    	 ;printf("pc: %g\n" pcRound(a/0.01)*0.01)
    	 ; add a small offset to account for rounding errors
             round(a/0.01+0.001) * 0.01 ;;return
         )
    )  
    for(i 1481 1490 printf("%f\n" myCalc(i/100.0)) )

    Regards,

    Andrew

    • 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