Welcome to the Onshape forum! Ask questions and join in the discussions about everything Onshape.

First time visiting? Here are some places to start:

  1. Looking for a certain topic? Check out the categories filter or use Search (upper right).
  2. Need support? Ask a question to our Community Support category.
  3. Please submit support tickets for bugs but you can request improvements in the Product Feedback category.
  4. Be respectful, on topic and if you see a problem, Flag it.

If you would like to contact our Community Manager personally, feel free to send a private message or an email.

Strange behavior of FOR loop for real iterator.

konstantin_shiriazdanovkonstantin_shiriazdanov Member Posts: 897 ✭✭✭✭✭
edited April 2017 in FeatureScript
in this simple test feature i reproduced the sutuatuin when number of iterations
of my loop is +2 bigger then one would expect for test values bigger then 127 degrees
FeatureScript 559;
import(path : "onshape/std/geometry.fs", version : "559.0");

annotation { "Feature Type Name" : "My Feature" }
export const myFeature = defineFeature(function(context is Context, id is Id, definition is map)
annotation { "Name" : "My Angle" }
isAngle(definition.myAngle, ANGLE_360_BOUNDS);

var list = [];
for (var phi = 0 * degree; phi < definition.myAngle; phi += 1 * degree)
list = append(list, phi);

list = append(list, list[0]);
debug(context, size(list));


  • ilya_baranilya_baran Onshape Employees, Developers, HDM Posts: 919
    I didn't check, but I expect you're running into floating point tolerance issues -- keep in mind that addition is not an exact operation and neither is multiplication by degree (since our internal units are radians).  To fix this, I suggest you either add or subtract TOLERANCE.zeroAngle * radian to definition.myAngle (depending on whether you want inclusion or exclusion).
    Ilya Baran \ Director of FeatureScript \ Onshape Inc
  • konstantin_shiriazdanovkonstantin_shiriazdanov Member Posts: 897 ✭✭✭✭✭
    thanks @ilya_baran, i suspected it was because real iterator, but hardly belived that it can add two more iterations after only 127
  • ilya_baranilya_baran Onshape Employees, Developers, HDM Posts: 919
    It's only adding one more iteration: the result of adding 1 degree 127 times is very slightly less than 127 * degree.
    Ilya Baran \ Director of FeatureScript \ Onshape Inc
  • MBartlett21MBartlett21 Member Posts: 1,550 EDU
    Would it fix it if you divided by degree before doing the for loop?
    MB - I make FeatureScripts: view FS (My FS's have "Official" beside them)
  • konstantin_shiriazdanovkonstantin_shiriazdanov Member Posts: 897 ✭✭✭✭✭
    Would it fix it if you divided by degree before doing the for loop?
    @mbartlett21 possibly if i had unitless integer iterator and multiplied it by degree inside the loop it would work without taking tolerances into account. don't think dividing by real number would help for it
  • john_f_carrjohn_f_carr Onshape Employees Posts: 74
    The FeatureScript range() function may help you.  It divides a range into equal steps and ensures the last value is equal to the upper limit.
Sign In or Register to comment.