• 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. How to compare 2 float variable/numbers?

Stats

  • Locked Locked
  • Replies 1
  • Subscribers 144
  • Views 2411
  • 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

How to compare 2 float variable/numbers?

raams
raams over 15 years ago

Hi,

Below code reads the  float value from a given txt file and compares it with another float value calculated using equation below.

If loop is not executing for the below mentioned code though the two variables "nspace" and "vspace" are equal 

 

input file has following line:

net tt space= 0.15

code:

procedure(match()

in=infile("~/nets.txt")
when(in
while(gets(line in) && line!="NULL"
linelist=simStringsToList(line)
nspace=atof(nth(3 linelist))

))

a=0.16

b=0.16

x1=0.715

x2=1.025

vspace=abs(abs(x1-x2)-(a/2)-(b/2))

if(vspace==nspace then println("correct") else println("wrong") )

) 

output:

wrong 

can anyone pls let me know the error in the above code?

Thnx

  • Cancel
  • StefanSL
    StefanSL over 15 years ago

    Hi raams,

    as almost expected, your problem seems to be related to the internal representation of floats;

    two floats being displayed as the same number must not necessarily be the same number cause
    they are cut behind the 7th decimal:
    (in the CIW, define x=0.15 and add 0.0001, 0.00001 and so on;
    if you add 0.0000001, you'l  get 0.1500001,
    if you add 0.00000001,the result will be 0.15
    )

    depending on the way they are calculated, nspace and vspace seem to be different
    more or less far behind the decimal-point;

    after rounding the result looks as initially expected:

    nspace=float( round( 100000 * vspace ) ) /100000
    vspace=float( round( 100000 * vspace ) ) /100000
    println(nspace)
    println(vspace)
    if(nspace==vspace then println("correct") else println("wrong") )

    will get a "correct"

    if you are interested in very small differences, this might not be applicable,
    but perhaps it will help you...

    kind regards

    Stefan

     

    • 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