[Closed] error comparing distances
I have three points A, B and C
distance Betwen A-B and A-C is the same, but when y compare the distances…
dist1 = distance A B
dist2 = distance A C
if dist1 > dist2 then print "dist1 is bigger"
if dist1 < dist2 then print "dist1 is smaller"
if dist1 == dist2 then print "dist1 is equal to dist2"
the execution always prints “dist1 is bigger”. Please help, i cant understand the reason
“dist2=53.4176”
“dist1=53.4176”
this are the values of the distances.
Chances are the values are NOT the same somewhere deep down in the decimal precision.
You can see that by saying (in Max 9 and higher):
formattedPrint dist1 format:“19.18g”
formattedPrint dist2 format:“19.18g”
If you want to make sure the values are rounded off before comparing, you can use
dist1 = (int (dist11000))/1000.0
dist2 = (int (dist11000))/1000.0
This should remove any decimal places after the 3rd and I would expect dist1==dist2 to return true after that.
thanks Bobo for your reply, that was the problem. now i can continue with my script.
any idea of what’s the problem in my other threads??
thanks again.
Bobo isn’t there also somesort of “closeenough A B” function I remember seeing?
Ahhh here it is:
close_enough[font=‘Courier New’] <float> <float> <int>[/font]
NEW in [color=red]3ds Max 2008: [[/color]AVG[color=red]] [/color]Returns true if the two <float> values are approximately equal, where increasing values of <int> increase the range defined as appoximately equal.
[left][left]A good value of <int> in most cases is 10.[/left][/left]
[left][left] [/left][/left]
[left][left] [/left][/left]
[left][left]You could use that instead of ‘==’[/left][/left]
Good point, in 2008 and 2009, that’s the way to go. In previous versions, my other suggestion would be the more universal appoach (for backwards compatibility).
what is the underlying math for ‘close_enough’ anyway? The description is somewhat… vague.
Last I knew “abs (dist1 – dist2) <= threshold” was the way to go, and would seem faster than the casting to integer and back? Am I missing a particular caveat?