Problem with floor

Discussion in 'Cadence' started by Pradeep Kumar Chawda, Mar 8, 2006.

  1. Is the below is a known issue? if so any workarounds?

    test1 = 500 * 1.001
    test2 = test1 * 10.0
    (Now value of test2 is 5005.0)
    floor(test2) will give 5004.
     
    Pradeep Kumar Chawda, Mar 8, 2006
    #1
  2. See countless posts before on similar subjects. This is a standard floating
    point rounding problem - which occurs in pretty much all high level languages
    (including C).

    If you do:

    sstatus(fullPrecision t)
    test2 => 5004.999999999999
    printf("%.12f" test2)
    test2-5005.0 => -9.094947e-13

    The problem is caused by the fact that floating point numbers are represented
    with a finite binary mantissa, and so 1.001 is a recurring fraction (should be
    infinitely recurring, but is truncated due to the finite precision). When
    multipled, you end up with rounding errors (small, but still noticeable if you
    look).

    To avoid this either use round(), or use a small tolerance added to the value
    before you floor it.

    Andrew.
     
    Andrew Beckett, Mar 8, 2006
    #2
  3. Pradeep Kumar Chawda

    Jimka Guest

    Hi Pradeep, please try this any any other programming language you can
    get your hands on. You should get
    the same results in Perl, Python, Java, C, C++, Fortran
    or anything i can think of.

    Try it out and let me know what answer you get and with other language
    you are are using.

    -jim
     
    Jimka, Mar 9, 2006
    #3
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.