Unit Dimension System
Unit Dimension System
Note: This document is AI-translated.
The unit dimension system is a core utility module in the ast library for handling units and dimensions of physical quantities. This system supports numerical operations with units, automatic unit conversion, and dimension consistency checks to ensure the correctness of physical calculations.
Core Concepts
Dimension
Dimension is the basic attribute of a physical quantity, representing the type of the physical quantity. For example, length, mass, and time are fundamental dimensions, while velocity and acceleration are derived dimensions.
Warning
Currently supported derived dimensions must be in the form of products of powers of fundamental dimensions, where exponents must be integers ranging from -8 to 7.
Fundamental Dimensions
The ast library defines the following fundamental dimensions:
- Mass
- Length
- Angle
- Time
- Temperature
- Current
- Amount
- Luminous Intensity
Dimension Operations
Dimensions support multiplication, division, and power operations to create derived dimensions:
#include "AstUtil/Quantity.hpp"
int main(){
AST_USING_NAMESPACE
// 创建速度量纲(长度/时间)
Dimension velocityDim = Dimension::Length() / Dimension::Time();
// 创建加速度量纲(长度/时间²)
Dimension accelerationDim = velocityDim / Dimension::Time();
// 创建面积量纲(长度²)
Dimension areaDim = Dimension::Length().pow(2);
}Unit
Unit is the specific representation of a dimension, used to measure the size of physical quantities. Each unit has a corresponding dimension and scaling factor.
Predefined Units
The ast library provides a rich set of predefined units, including:
- Length: mm, cm, m, km, in, ft, yd, mi
- Time: s, min, h, day
- Mass: kg, g, mg, lb
- Angle: rad, deg
- Force: N
- Current: A
- Area: m²
- Volume: m³, L
Unit Operations
Units can be multiplied, divided, and raised to powers to create composite units:
#include "AstUtil/Quantity.hpp"
int main(){
AST_USING_NAMESPACE
// 创建米每秒单位
Unit mps = Unit::Meter() / Unit::Second();
// 创建千米每小时单位
Unit kmph = Unit::Kilometer() / Unit::Hour();
// 创建平方米单位
Unit sqMeter = Unit::Meter() * Unit::Meter();
}Quantity
Quantity is a numerical value with a unit, combining numerical value and unit information, and supporting numerical operations with units.
Creating Quantities
#include "AstUtil/Quantity.hpp"
int main(){
AST_USING_NAMESPACE
// 创建5米的长度
Quantity length = 5.0 * Unit::Meter();
// 创建10米每秒的速度
Quantity velocity = 10.0 * (Unit::Meter() / Unit::Second());
// 创建标量(无量纲)
Quantity scalar = Quantity::Scalar(42.0);
}Quantity Operations
Quantities support addition, subtraction, multiplication, and division, but require compatible dimensions:
#include "AstUtil/Quantity.hpp"
int main(){
AST_USING_NAMESPACE
// 加法:相同量纲的数量可以相加
Quantity length1 = 5.0 * Unit::Meter();
Quantity length2 = 100.0 * Unit::Centimeter();
Quantity totalLength = length1 + length2; // 自动转换为相同单位
// 乘法:不同量纲的数量相乘得到新量纲
Quantity area = length1 * length2; // 结果为5.0 m²
// 除法:数量相除得到新量纲
Quantity velocity = length1 / (10.0 * Unit::Second()); // 结果为0.5 m/s
}Dimension Consistency Check
The system automatically checks dimension consistency during operations to prevent physically unreasonable calculations:
#include "AstUtil/Quantity.hpp"
int main(){
AST_USING_NAMESPACE
Quantity length = 5.0 * Unit::Meter();
Quantity time = 10.0 * Unit::Second();
// 合理:长度 + 长度
Quantity totalLength = length + length;
// 不合理:长度 + 时间(编译时不会报错,但运行时可能出现问题)
// Quantity invalid = length + time;
}