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.

Why can I not use variables in math expressions?

Kevin_BeitelKevin_Beitel Member Posts: 1
edited May 2019 in Community Support
It seems that existing variables are not valid in math expressions. I get a valid expression when defining #Y as a value of #X/2, but #X^(1/2) or sqrt(#X) are invalid expressions. Is this not allowed or am I doing something wrong?

Best Answer

  • kevin_o_toole_1kevin_o_toole_1 Onshape Employees, Developers, HDM Posts: 565
    Answer ✓
    Right, an operation like sqrt(1 meter) isn't well-defined: Does it mean 1 meter? 10 centimeters? Or nothing at all? Onshape says the latter :smile:

    If #X has length units, you can use an expression like sqrt(#X / inch) * inch, which is well-defined.

Answers

  • konstantin_shiriazdanovkonstantin_shiriazdanov Member Posts: 1,221 ✭✭✭✭✭
    it may be because of the units of #x, linear operations keep units the same but power of the value with units doesnt
  • kevin_o_toole_1kevin_o_toole_1 Onshape Employees, Developers, HDM Posts: 565
    Answer ✓
    Right, an operation like sqrt(1 meter) isn't well-defined: Does it mean 1 meter? 10 centimeters? Or nothing at all? Onshape says the latter :smile:

    If #X has length units, you can use an expression like sqrt(#X / inch) * inch, which is well-defined.

  • brian_bradybrian_brady Member, Developers Posts: 505 EDU
    I get around this by using unitless variables.
  • john_mcclaryjohn_mcclary Member, Developers Posts: 3,944 PRO
    I also use unitless until the bitter end
  • marci_van_dortmarci_van_dort Member Posts: 6

    I don't see what is unclear about the definition. For example (10 mm)^2 = 100 mm², and onshape knows how to handle this just fine. Taking a square root is no different, except the power is 1/2. The units then would be mm^(1/2). In your example, (1 m)^0.5 = 1 m^0.5; if the value were given e.g. in cm: sqrt(100 cm) = 10 sqrt(cm) = 10 sqrt(1/100 m) = 10 * 1/10 * sqrt(m) = 1 m^0.5. This is all perfectly consistent, there is nothing vague about it, and I don't see why using this would not be allowed.

    Sure, units like these don't make much physical sense, but that doesn't matter. As e.g. an intermediate result, it could be perfectly valid.

    Having to convert my variables to be unitless before performing an operation, and then manually adding in the correct dimension at the end is very cumbersome, it makes writing an expression take minutes instead of seconds. What makes it worse is the inconsistency: some operations, like positive integer powers of values with units seem to be allowed, but others aren't. I often find myself having to break down expressions into their smallest parts, checking each element to make sure it evaluates properly, and then slowly building it back up again with the necessary hacks like dividing by units before doing some operations.

Sign In or Register to comment.