Loading
/custom-emojis/emojis/contour-map.png
Templates
📚
Articles & Resources
📖
Guides & Support
🌵
CalcTree
Estados de Vigas de Concreto
Bust Common Myths About Java Programming
Loading
/custom-emojis/emojis/calculator.png
Tensile Strength and Capacity Control of the W-Shape Sections According to AISC 360-16
Loading
/custom-emojis/emojis/calculator.png
Concrete Cylinder Strength Vs Cube Strength
Loading
/custom-emojis/emojis/calculator.png
Earthquake Design Action Calculation
Sıvılaşma Verileri Tablosu
Loading
/custom-emojis/emojis/rc-beam.png
Concrete Column Designer to AS3600
EM Wave Propagation Calculator
section properties with units
Forward Kinematics of Robotic Arm with 6 Degrees of Freedom
İKSA YAPILARI PROJELENDİRME HİZMET BEDELİ (2024)
GEOTEKNİK RAPOR (EK-B) ASGARİ HİZMET BEDELİ (2024)
ZEMİN İYİLEŞTİRME/DERİN TEMEL PROJELENDİRME ASGARİ HİZMET BEDELİ (2024) (İMO)
🚀
Projectile motion
Loading
/custom-emojis/emojis/bending-moment.png
Dezi et. al (2010)
🤾
Projectile motion
⚖️

Units in Python

In CalcTree, you can easily use units across your calculations. Here, we explain how to reference a quantity inside Python, perform operations on it, and reference the quantities defined in Python elsewhere on the CalcTree page.
When using formulae on a CalcTree page, you can use units of measurement to define quantities. For example, in a math parameter:
Using units in a math parameter



distance
:1.00 m


Let's start by referencing this quantity inside a Python source.

Referencing a quantity in Python

You can reference an existing quantity from your calculations inside Python. For example, the parameter defined above, can be referenced in Python as _page_distance, which will be of quantity type:
Referencing the distance parameter in Python



check_type
:<class 'pint.Quantity'>


you can get the magnitude and the unit of a quantity using the magnitude and unit methods:
Accessing magnitude and unit of a quantity



distance_magnitude
:1.00



distance_unit
:meter



Defining a new quantity in Python

For defining a quantity in Python, you can use the ct.quantity method. For example to define a quantity of 1.5 seconds:
Defining a quantity in Python


You can also define a quantity of 1.0 magnitude of a unit, using the ct.units method:
Defining a quantity of one magnitude of a unit



one_second
:1.00 s



Operations on quantities

You can multiply or divide quantities, which will result in a derived quantity. For example:
Creating a derived quantity



speed
:0.80 m / s


You can also add or subtract quantities of the same type. The necessary unit conversions will happen automatically. For example:
Adding two compatible quantities



total_distance
:1.3048 m


Note that you can't use math.sqrt() on a quantity. you can use the power operator instead:

use the ** operator instead of math.sqrt()


Built-in quality control with units

If a quantity of incompatible type is tried in a Python addition or subtraction operation, CalcTree will prevent the calculation from proceeding and shows the relevant error. For example this line will inforce a quantity of Length for the parameter distance when received inside Python:
Enforce a quantity type of Length to be passed to Python

thus, sending a quantity other than length as the distance will not be accepted:
Quality control with units

if you have a Python script that assumes certain quantities for its inputs, you can add quality control to it using the above capability.


Unit conversion

The conversion of units will be done automatically inside Python, for example when adding and subtracting compatible quantities. If you want to explicitly convert a quantity into a desired unit, you can use the .to() method inside Python, or the to keyword in Math parameters on the page:

unit conversion inside Python



inch to mm
:25.40 mm

unit conversion in math parameters



Automatic prefix selection

When the magnitude of a quantity becomes too small or too large, CalcTree automatically picks an appropriate unit prefix, so that the magnitude becomes a more readable number, for example:

you can override this behavior by using the to conversion keyword mentioned in the previous section:


Underlying library used for units

CalcTree uses a Python library called pint to handle units. You can access pint documentation here.




Still have more questions?

Join the conversation on Slack below!