Grasshopper

algorithmic modeling for Rhino

A Paradoxical problem with the SIMPLEST conditional statement !!

I was trying to filter a list of data via a simple equational statement, but I faced the problem that the statement doesn't work properly with some datas, and can't find the reason . .  so any idea of why it occurs would give me a great hand with it !

Views: 617

Attachments:

Replies to This Discussion

Hi Mehrad,

might the numbers be approximated somehow ?
I mean for example 4.80000000001 instead of 4.8 ?

Cheers
Emilio
Thanx Emilio

most of all i was confused by the "why" of this occurrence, but i haven't noticed this subtle point . . .
I too have noticed a lot of inconsistencies with the equality operator. I often round my inputs to a fixed number of decimal places or use the "similarity" operator component to work around it.
thanx Andrew

I've replaced the "=" with "≈" in the function's Expression section and it worked . . .do you know how much the "≈" tolerates around the specified number? for example if we describe " x ≈ 4.8 " as " x= 4.8 ± a ", do you know what is the min and max for "a"?

and second, i had tried the "round" function, but i faced problem with it too! for example:

if the input is a series as {0.0, 0.5, 1, 1.5, 2, 2.5, ...}
the output for Round(x, 0.5) is : {0, 0, 1, 2, 2, 2, 3, 4, 4, 4, 5, 6, 6, 6, ... }

and for Round(x, 2) the output is : {0.0, 0.5, 1, 1.5, 2, 2.5, ... }

i can't understand the logic that lies behind this function, i think
for Round(x, 0.5) the output must be {0.0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, ... }
and for Round(x, 2) it must be {0.0, 0.0, 2, 2, 2, 2, 4, 4, 4, 4, ... }

so, is there any problem with it, or I misendestood the logic ?
In the expression Round(x, [d]) the optional input d is the number of decimal places for the rounding operation so your results are as expected...

d can only be an integer so your expression value of 0.5 is being interpreted as 0. For your expression value of 2 the results are also correct since your input values aren't more than two decimal places long.
Equality shouldn't be used on floating point numbers, unless you can be absolutely certain that they can indeed be identical. Whenever the floating point number is the result of a mathematical operation (addition, multiplication, division and -especially- subtraction) then you should expect there to be binary noise in the least significant digits.

Equality can be used on Integers without problems, but for floating points you should compare the difference to a threshold value. For example:

If (Math.Abs(A - B) < 1e-16) Then

Where 1e-16 is a value you need to pick. If you are dealing with very big numbers, you'll need a much larger equality threshold.

--
David Rutten
david@mcneel.com
Turku, Finland

RSS

About

Translate

Search

Photos

  • Add Photos
  • View All

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service