List of Math Operations

Is there a list of valid math expressions and operation such as cos( ), log( ), abs( ), etcetera?

Best Answer

Answers

  • NeilCooke
    NeilCooke Moderator, Onshape Employees Posts: 6,126 image
    Yes, please see the FeatureScript documentation under the help menu. 
    Senior Director, Technical Services, EMEA
  • brucebartlett
    brucebartlett Member, OS Professional, Mentor, User Group Leader Posts: 2,150 PRO
    Engineer ı Product Designer ı Onshape Consulting Partner
    Twitter: @onshapetricks  & @babart1977   
  • michael_mcdonald047
    michael_mcdonald047 Member Posts: 23 ✭✭
    Thanks abefeldman, I was a little surprised to not see a div operation but see now I can just nest a '/' inside a floor() to obtain an equivalent result.  Whereas div(a,b) or (a div b) = floor(a / b).
  • mahir
    mahir Member, Developers Posts: 1,324 ✭✭✭✭✭
    @michael_mcdonald047, be careful. I suspect A div B = floor(A/B) only when A/B is positive. Case in point, 5 div -2 = -2, but I believe floor(5/-2) = -3. If there was a SIGN function you could use sign(A/B)*floor(abs(A/B)). But as far as I can tell, there is no sign function. You could substitute sign(X)=X/abs(X).
  • mahir
    mahir Member, Developers Posts: 1,324 ✭✭✭✭✭
    Or you can use an inline "if".

    AdivB = (A/B>= 0) ? floor(A/B) : ceil(A/B)
  • Alex_Pitt
    Alex_Pitt Member Posts: 78 PRO
    Hi, How do I square a config variable in a numeric field? (not a feature script)  I'm trying to get the hypotenuse length given #rise & #run for an array feature in a part studio. I would guess sqrt(#rise^2+#run^2) but that didn't work.  Realised I didn't know how to square a number! None of following worked: #run^2    #run*#run   #run.#run. 



    Had a quick look at the Feature Script documentation but not sure which part is relevant.

    Found the following but can't see a square function anywhere.

    https://www.onshape.com/en/resource-center/tech-tips/tech-tip-using-numeric-fields

  • romeograham
    romeograham Member, csevp Posts: 783 PRO
    You may also be running into a problem with units: your #rise variable includes mm as units. If you square that, you get mm*mm (which expresses an area, not a linear dimension). The part pattern is looking for a linear distance (only mm, not mm*mm).

    You can divide by mm in your expression to cancel the units ((#rise/mm)*(#rise/mm)) which will give you a numerical value, that will get interpreted as a linear dimension (a "length") which is what the Pattern is looking for.

    You could also change your Variables to be unitless - then this wouldn't be an issue.

    Hope that helps!
  • ilya_baran
    ilya_baran Onshape Employees, Developers, HDM Posts: 1,277 image
    sqrt(#rise^2+#run^2) should work if #rise and #run are both lengths.  Please post a link to a version of your document where this expression isn't working and I or someone here will be able to tell you what's wrong.
    Ilya Baran \ VP, Architecture and FeatureScript \ Onshape Inc
  • doug_leary
    doug_leary Member Posts: 3 ✭✭

    As of 3-Sept-2025 the following syntax works:

    (sqrt(#rise^2 + #run^2) / mm) mm