Floating point rouding in SKILL

Discussion in 'Cadence' started by Aby, May 14, 2005.

  1. Aby

    Aby Guest

    Hi,
    I am observing some wierd results in SKILL when I try to perform
    division.

    A - float
    B - float

    When I do:
    A/B
    I get:
    2.0
    But I do:
    int(A/B)
    I get:
    1

    And the even more strange thing is, When I call the function which does
    this over and over, few times it gives correct result. Has someone
    faced this before ?
     
    Aby, May 14, 2005
    #1
  2. This is a VFAQ (very frequently asked question).

    SKILL behaves no differently than any other application which uses the
    machine's floating point hardware.

    Rather than try to explain it, I'll just link to David Goldberg's
    excellent paper on the subject, "What Every Computer Scientist Should
    Know About Floating-Point Arithmetic." It's very rigorous, and will
    require some patient study, but if you work through the theorems you'll
    never be bitten by this again.

    http://docs.sun.com/source/806-3568/ncg_goldberg.html
     
    David Cuthbert, May 15, 2005
    #2
  3. Aby

    Aby Guest

    Thanks for the reply. I have had a course on computer arithmetic before
    but I don't recollect seeing such inconsistencies. Here is what I am
    printing in a loop using the fprintf( "%f" varName):

    Iteration #1
    coreWidth = 3.000000
    netSpace = 1.500000
    ratio (i.e. coreWidth/netSpace) = 2.000000
    int(ratio) = 2 ----> Correct

    Iteration #1
    coreWidth = 3.000000
    netSpace = 1.500000
    ratio (i.e. coreWidth/netSpace) = 2.000000
    int(ratio) = 1 ----> Incorrect

    How can I get two different results for the exact same inputs in
    different iterations?

    Thanks
     
    Aby, May 15, 2005
    #3
  4. Aby

    S. Badel Guest

    How can I get two different results for the exact same inputs in
    It might be that the inputs are not exactly the same.
    They are identical to some precision - we see only six digits
    after the point, but they might be different in fact.

    ie. it might be that the result in your second example is
    1.9999999998929, which rounds to 2.000000 when printed with this
    precision, but it's smaller that 2 and hence yields the result 1.0
    to the int() function.

    it's allways tricky to work with floating-point numbers - i've been hurt
    before. you should try this:
    precision = 1e-6
    x = coreWidth / precison
    y= netSpace / precision
    ration = x/y
    result = precision * int(ratio)

    try changing the precison and see.

    stéphane
     
    S. Badel, May 15, 2005
    #4
  5. If you had read the paper, it would explain this.

    Furthermore, %f performs rounding in both C and SKILL (which relies on
    the underlying C library). Try it again with %.20g instead.
     
    David Cuthbert, May 15, 2005
    #5
  6. If all you want is to round the floating point number to an integer, then
    you can use round() instead of int(), or you could add 0.5 to the number
    before calling int() (subtract 0.5 if the number is negative). It's hard
    to say what the solution is because I don't know what you want the math to
    work out to in other cases such as 3.0/2.0. Even if you want to truncate
    the number you probably want to use floor() instead of int().

    Frank
     
    Frank E. Gennari, May 16, 2005
    #6
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.