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: 1,221 ✭✭✭✭✭
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
https://cad.onshape.com/documents/c0429467b6e5c445825d5e38/w/4ac0768c5fbd908e4dd309f6/e/d53b7f0578bcaaa9835d8461
FeatureScript 559;<br>import(path : "onshape/std/geometry.fs", version : "559.0");<br><br>annotation { "Feature Type Name" : "My Feature" }<br>export const myFeature = defineFeature(function(context is Context, id is Id, definition is map)<br>    precondition<br>    {<br>        annotation { "Name" : "My Angle" }<br>        isAngle(definition.myAngle, ANGLE_360_BOUNDS);<br><br>    }<br>    {<br>        var list = [];<br>        for (var phi = 0 * degree; phi < definition.myAngle; phi += 1 * degree)<br>        {<br>            list = append(list, phi);<br>        }<br><br>        list = append(list, list[0]);<br>        debug(context, size(list));<br>    });

Comments

  • ilya_baranilya_baran Onshape Employees, Developers, HDM Posts: 1,173
    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 \ VP, Architecture and FeatureScript \ Onshape Inc
  • konstantin_shiriazdanovkonstantin_shiriazdanov Member Posts: 1,221 ✭✭✭✭✭
    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: 1,173
    It's only adding one more iteration: the result of adding 1 degree 127 times is very slightly less than 127 * degree.
    Ilya Baran \ VP, Architecture and FeatureScript \ Onshape Inc
  • MBartlett21MBartlett21 Member, OS Professional, Developers Posts: 2,034 EDU
    @konstantin_shiriazdanov
    Would it fix it if you divided by degree before doing the for loop?
    mb - draftsman - also FS author: View FeatureScripts
    IR for AS/NZS 1100
  • konstantin_shiriazdanovkonstantin_shiriazdanov Member Posts: 1,221 ✭✭✭✭✭
    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.