PDA

View Full Version : value comparison problem!

 chib03-23-2012, 02:30 PMI've got this little problem: when I execute this chunk of code, i = 1.0 n = 10.0 step = 0.6 while true do ( format "% > % : %\n" i n (i > n) if i > n then exit i += step ) I get this result: 1.0 > 10.0 : false 1.6 > 10.0 : false 2.2 > 10.0 : false 2.8 > 10.0 : false 3.4 > 10.0 : false 4.0 > 10.0 : false 4.6 > 10.0 : false 5.2 > 10.0 : false 5.8 > 10.0 : false 6.4 > 10.0 : false 7.0 > 10.0 : false 7.6 > 10.0 : false 8.2 > 10.0 : false 8.8 > 10.0 : false 9.4 > 10.0 : false 10.0 > 10.0 : true why?? ^^ 10.0 > 10.0 is false when the step is set to 0.5, which sounds more normal... has anyone faced this problem before? note: the problem is the same with the for i to n by step syntax. (For the record, I'd like to store data like a pointcache modifier would do, by specifying a start and a step value, but the number of samples recorded is a bit random because of this problem...) thanks, Paul.
Pjanssen
03-23-2012, 02:48 PM
This happens because of floating point precision. The 10.0 value is not exactly 10.0, but something very close to it. This can be a bit tricky when comparing values.

Have a look at this function in the maxscript help:
Float Close_Enough Method
close_enough <float> <float> <int>

Returns true if the two <float> values are approximately equal, where increasing values of <int> increase the range defined as appoximately equal. Available in 3ds Max 2008 and higher. Previously available in the [AVG] Avguard Extensions.

A good value of <int> in most cases is 10.

chib
03-23-2012, 03:17 PM
Useful function in maxscript I guess! But it still feels a little bit strange that an operation that gives exactly 10 (mathematically speaking), returns a float number that doesn't :surprised

Thanks,
Paul.

chib
03-23-2012, 03:25 PM
Just for the record, the problem doesn't occur in python...

denisT
03-23-2012, 03:27 PM
that's absolutely crazy. i haven't seen this before. i don't have any clue why it works this way. How can I trust max after that? The worse thing I don't see any workaround.
if it starts from 8.8 it doesn't work rigth, but when it starts from 9.4 it's correct:

(
i = 8.8
k = 10.0
step = 0.6

while (i <= k) do
(
format "% <= % = %\n" i k (i <= k)
i = i + step
)
)
(
i = 9.4
k = 10.0
step = 0.6

while (i <= k) do
(
format "% <= % = %\n" i k (i <= k)
i = i + step
)
)

denisT
03-23-2012, 03:35 PM
Just for the record, the problem doesn't occur in python...

do you think so? python has another problems ... round is an example: http://docs.python.org/tutorial/floatingpoint.html

CGTalk Moderation
03-23-2012, 03:35 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.