What's new

Banging my head on this one: why doesn't 24.0 equal 24.0 ?

albie0803

Alibre Super User
Create a 24 dia cylinder > 20 long and run this script, selecting an end and the shaft.

why is line 67 not true when i[0] = 24.0 ?

Please, what am I missing? :eek:o_O

Python:
Win = Windows()
Units.Current = UnitTypes.Millimeters
P = CurrentPart()
P.Regenerate()

Clip = [
# dia,width,ID
[10.0,1.1,9.6],
[11.0,1.1,10.5],
[12.0,1.1,11.5],
[13.0,1.1,12.4],
[14.0,1.1,13.4],
[15.0,1.1,14.3],
[16.0,1.1,15.2],
[17.0,1.1,16.2],
[18.0,1.3,17],
[19.0,1.3,18],
[20.0,1.3,19],
[21.0,1.3,20],
[22.0,1.3,21],
[23.0,1.3,22],
[24.0,1.3,22.9],
[25.0,1.3,23.9],
[26.0,1.3,24.9],
[27.0,1.3,25.6],
[28.0,1.6,26.6],
[29.0,1.6,27.6],
[30.0,1.6,28.6],
[31.0,1.6,26.6],
[33.0,1.6,31.3],
[34.0,1.6,32.3],
[35.0,1.6,33.0],
[36.0,1.85,34],
[38.0,1.85,36],
[40.0,1.85,37.5],
[42.0,1.85,39.5],
[45.0,1.85,42.5],
[46.0,1.85,43.5],
[47.0,1.85,44.5],
[48.0,1.85,45.5],
[50.0,2.15,47],
]

loop = 1
while loop == 1:

  Options = []
  Options.append(["Choose Shaft End", WindowsInputTypes.Face, None]) #[0]
  Options.append(['Choose Shaft Diameter Face', WindowsInputTypes.Face, None]) #[1]
  Options.append(["Dist from Edge", WindowsInputTypes.Real, 10]) #[2]
#  Options.append([None, WindowsInputTypes.Image, 'HKey.png', 150])

  Values = Win.OptionsDialog("Circlip Groove Generator", Options, 110)
  if Values == None:
    sys.exit()
  else:
    loop = 0

end = Win.GetInputValue(0)
sha = Win.GetInputValue(1)
dis = Win.GetInputValue(2)
shaE = sha.GetEdges()
dia = shaE[0].Diameter
print 'dia = ', dia

for i in Clip:
  if dia == i[0]:
    WD = i[1]
    ID = i[2]
  if dia != i[0]:
    print dia, i[0]
print 'WD = ',WD, 'ID = ',ID
    
S = P.AddSketch('Circlip', P.AddPlane('CirclipPlane', end, -dis))
S.AddCircle(0,0,dia+1,False)
S.AddCircle(0,0,ID,False)
P.AddExtrudeCut('Circlip %' % (dia), S, WD , True)
 

NateLiquidGravity

Alibre Super User
I'm unable to test at the moment but something comes to mind:
Could be a floating point number error. Instead of testing for equals switch to checking the difference is within tolerance.
if abs(dia - i[0]) < 0.00001:
 

simonb65

Alibre Super User
Could be a floating point number error. Instead of testing for equals switch to checking the difference is within tolerance.
Just to reinforce Nate's comment. You should never do just an equals operator on a floating point number! The way floats are stored (represented) by a 32 bit (64 bits for doubles) has a natural side effect that a precise number is not actually stored ... but a close approximation! Therefore when doing calculations, etc slight errors will creep in. The equals operator tests the stored 32 bits for an exact binary match, which will rarely be true!

Here is a good in depth background read, for anyone who is interested in the detail, on the subject ... What Every Computer Scientist Should Know About Floating-Point Arithmetic

Nate's solution is the accepted industry wide solution, but it does have performance issues in certain applications that require repeated comparison testing (and I suspect the geometry/constrain engine in Alibre is a very good candidate for that).
 

albie0803

Alibre Super User
Yes, I remembered all the reasons when I read Nate's reply. I don't program often enough for it to easily stick in my mind.

and anyway,
cover.jpg

24 is 24!
 
Top