Grasshopper

algorithmic modeling for Rhino

In VB.net: "For i = -5 to 5 Step .4"... The last item in the array should be "5.0" but comes out "0.0" (if we record "i" for each iteration of the loop).  However, "For i = -5 to 5.0000001 Step .4" returns the expected value of "5.0".  Why?

I've uploaded a simple example of this behavior.  Is this normal or a bug?

Views: 231

Attachments:

Replies to This Discussion

Hi Justin,

it's not a bug, it's normal. In the real world (as opposed to in mathematics), it's difficult having an infinite-precision representation of all numbers. So, floating point numbers are approximations of real values. You should take this into account: there might be rounding errors when adding two floating point values.

If you do not want rounding error sum ups, one way is to re-calculate the final value every time and loop over an integer value. Here an example:


For i = 0 To arrCount
'value = start + (increment * steps)
dblA(i) = (-5) + (.4 * i)
Next


I hope this helps,

- Giulio
Attachments:
Giulio: Thank you for the explanation and example. Makes sense. Best, J

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service