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.

Round to closest list value?

EvanReeseEvanReese Member, Mentor Posts: 2,136 ✭✭✭✭✭
edited May 2020 in Using Onshape
Does anyone know of a way that I can take a number and round it to the nearest value in a predetermined list (maybe an array)? For example:

if my list is [1,4,8,11,12]
1.9 rounds to 1
9.3 rounds to 8
9.5 rounds to 11
11.6 rounds to 12

There's got to be a name for such a thing.
Evan Reese

Comments

  • john_mcclaryjohn_mcclary Member, Developers Posts: 3,936 PRO
    There are a few rounding funcitons

    ceil() rounds up to  nearest whole number
    floor() rounds down to nearest whole number
    round() rounds to nearest up or down to nearest whole number
    roundToPrecision() will round with decimals

    var listStart = [1.9859,9.23,9.2525,11.652,0.654110111];     
            var listFloor = [];
            var listCeil = [];
            var listRound = [];
            var listRoundToPrecision = [];
            
            for (var item in listStart)
            {
                listFloor = append(listFloor, floor(item));
                listCeil = append(listCeil, ceil(item));
                listRound = append(listRound, round(item));
                listRoundToPrecision = append(listRoundToPrecision, roundToPrecision(item, 2));
            }
            
            println("Original List: " ~ listStart);
            println("Rounded List Down: " ~ listFloor);
            println("Rounded List Up: " ~ listCeil);
            println("Rounded List Nearest: " ~ listRound);
            println("Rounded List Nearest (2 place decimal): " ~ listRoundToPrecision);
    
    
    // result \\
    Original List: [ 1.9859 , 9.23 , 9.2525 , 11.652 , 0.654110111 ]
    Rounded List Down: [ 1 , 9 , 9 , 11 , 0 ]
    Rounded List Up: [ 2 , 10 , 10 , 12 , 1 ]
    Rounded List Nearest: [ 2 , 9 , 9 , 12 , 1 ]
    Rounded List Nearest (2 place decimal): [ 1.99 , 9.23 , 9.25 , 11.65 , 0.65 ]
    
    

  • konstantin_shiriazdanovkonstantin_shiriazdanov Member Posts: 1,221 ✭✭✭✭✭
    edited May 2020
    I have a function maxItem() which returns a max value of the list based on some other estimation function:
    <pre class="CodeBlock"><code>function maxItem(container is array, estimationFunction is function)<br>{<br>&nbsp;&nbsp;&nbsp; var max = container[0];<br>&nbsp;&nbsp;&nbsp; for (var item in container)<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (estimationFunction(item) > estimationFunction(max))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; max = item;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; return max;<br>}
    say we have a value "value" and estimation function may be:
    const estFunction = function(x){return -abs(x - value)};
    then you just do this:
    const roundedItem = maxItem(roundedArr, estFunction);
  • ilya_baranilya_baran Onshape Employees, Developers, HDM Posts: 1,212
    If your array is small, Konstantin's method works fine.  If you have a large array, the efficient method for solving this problem is binary search: https://en.wikipedia.org/wiki/Binary_search_algorithm#Procedure_for_finding_the_leftmost_element (use that procedure and then pick the closer one of A[L] and A[L-1])
    Ilya Baran \ VP, Architecture and FeatureScript \ Onshape Inc
  • EvanReeseEvanReese Member, Mentor Posts: 2,136 ✭✭✭✭✭
    Thanks for all the info everyone!
    Evan Reese
Sign In or Register to comment.