Adding Numbers and valueWithUnits (map)

SebastianMaklary
SebastianMaklary Member Posts: 37 PRO

Hi

I'm trying to assign a variable value which is the addition of two other values where one is a number and the other is the output of an acos(), which makes it a valueWithUnits.

The problem is something similar to:

var ay = 5 + acos(100/102);

Which gives the error as seen on the screenshot.

image.png

How can the output of an acos() be made into a number that can be used in this sort of calculation?

Answers

  • Matt_Shields
    Matt_Shields Member, Onshape Employees Posts: 993 PRO

    acos returns radians. you need something like

    var ay = 5*radian + acos(100/102);

  • joshtargo
    joshtargo Member Posts: 684 EDU

    In these sort of situations you can always do

    println(acos(100/102));

    and see what it's returning.

  • SebastianMaklary
    SebastianMaklary Member Posts: 37 PRO

    Aha, so the issue was more that one number was unitless while the other was not. It works after multiplying radian to the number.

    Now, I just have to do the rest of the calculations to see if it results in what I want it to. :)

    I'm however still a bit confused about why some things will just print the number and the units while other things will print the whole map. On the screenshot below, I am printing out psie_y which is now a ValueWithUnits (map) after I have multiplied radians to the unitless numbers. On line 103, I am then printing out the value of dy, which is just a diameter but that prints out as just the number and unit. Why do my angles not also just print out as a number and unit?

    image.png
  • joshtargo
    joshtargo Member Posts: 684 EDU

    Maybe because some of those are declared as arrays and they are showing you the full element (map) stored in the array? Not sure actually.

  • _anton
    _anton Member, Onshape Employees Posts: 554 image

    It's because of the string concatenation - in prepending a string to the ValueWithUnits, it makes println unable to stringify just the value. You can get around it by doing print("psie_y "); println(psie_y);.

  • SebastianMaklary
    SebastianMaklary Member Posts: 37 PRO

    That would be a notable difference between the different things that I'm printing out here.
    I'm used to do math in Matlab/Octave which isn't units aware and just treats everything as a number, so it's a bit of a learning curve to get to the point where I can put my function in a sketch.