Custom Literals Support
Custom Literals Support
Note: This document is AI-translated. Please refer to the source code for specific APIs and behaviors.
Overview
Literals.hpp is the custom literals support module in the ast project, providing a series of custom literal operators for unit conversion, making unit conversions in code more intuitive and readable. All conversions are based on constants defined in Constants.h.
Features
Supported Unit Types
Angle Units
- Radian:
_rad- Directly represents radian values - Degree:
_deg- Converts degrees to radians
Length Units
- Meter:
_m- Directly represents meter values - Kilometer:
_km- Converts kilometers to meters - Centimeter:
_cm- Converts centimeters to meters - Millimeter:
_mm- Converts millimeters to meters - Micrometer:
_um- Converts micrometers to meters - Nanometer:
_nm- Converts nanometers to meters - Astronomical Unit:
_au- Converts astronomical units to meters
Time Units
- Second:
_s- Directly represents second values - Minute:
_min- Converts minutes to seconds - Hour:
_h- Converts hours to seconds - Day:
_day- Converts days to seconds - Millisecond:
_ms- Converts milliseconds to seconds - Microsecond:
_us- Converts microseconds to seconds - Nanosecond:
_ns- Converts nanoseconds to seconds - Picosecond:
_ps- Converts picoseconds to seconds
Speed Units
- Meter/Second:
_m_s- Directly represents meter/second values - Kilometer/Second:
_km_s- Converts kilometers/second to meters/second - Kilometer/Hour:
_km_h- Converts kilometers/hour to meters/second
Angular Velocity Units
- Radian/Second:
_rad_s- Directly represents radian/second values - Degree/Second:
_deg_s- Converts degrees/second to radians/second
Type Support
All literal operators provide two overloads:
- Long double (
long double) - Unsigned long long (
unsigned long long)
Usage Examples
Angle Unit Example
#include "AstUtil/Literals.hpp"
int main(){
AST_USING_NAMESPACE;
// 角度转换示例
double angle1 = 90.0_deg; // 90度转换为弧度 (约1.5708 rad)
double angle2 = 3.14159_rad; // 直接使用弧度值
double quarter = 0.25 * 360_deg; // 90度
}Length Unit Example
#include "AstUtil/Literals.hpp"
int main(){
AST_USING_NAMESPACE;
// 长度转换示例
double distance1 = 5.5_km; // 5.5公里转换为米 (5500米)
double distance2 = 1000_cm; // 1000厘米转换为米 (10米)
double distance3 = 1.0_au; // 1天文单位转换为米
double thickness = 0.5_mm; // 0.5毫米转换为米
}Time Unit Example
#include "AstUtil/Literals.hpp"
int main(){
AST_USING_NAMESPACE;
// 时间转换示例
double time1 = 24_h; // 24小时转换为秒 (86400秒)
double time2 = 30_min; // 30分钟转换为秒 (1800秒)
double time3 = 500_ms; // 500毫秒转换为秒 (0.5秒)
double shortDelay = 10_us; // 10微秒转换为秒
}Speed Unit Example
#include "AstUtil/Literals.hpp"
int main(){
AST_USING_NAMESPACE;
// 速度转换示例
double speed1 = 300000_km_s; // 300000千米/秒转换为米/秒
double speed2 = 100_km_h; // 100千米/小时转换为米/秒 (约27.78 m/s)
double speed3 = 25_m_s; // 直接使用米/秒
}Angular Velocity Unit Example
#include "AstUtil/Literals.hpp"
int main(){
AST_USING_NAMESPACE;
// 角速度转换示例
double angSpeed1 = 30_deg_s; // 30度/秒转换为弧度/秒
double angSpeed2 = 0.1_rad_s; // 直接使用弧度/秒
}Practical Calculation Example
#include "AstUtil/Literals.hpp"
#include "AstUtil/Constants.h"
int main(){
AST_USING_NAMESPACE;
// 计算示例
double speed = 100_km / 1_h; // 计算速度 (约27.78 m/s)
double frequency = 1.0 / 1_ms; // 计算频率 (1000 Hz)
double circumference = 2.0 * kPI * 1.0_km; // 计算周长
}Design Notes
Unit System Foundation
- Angle: Internally uses radians
- Length: Internally uses meters
- Time: Internally uses seconds
- Speed: Internally uses meters/second
- Angular Velocity: Internally uses radians/second
Namespace
All literal operators are defined in the ast::literals namespace and can be easily used by importing with using namespace ast::literals;.
Compile-Time Calculation
All literal operators use the constexpr keyword, supporting compile-time calculations for improved performance.
Precision Considerations
Uses double type as the return value, providing sufficient precision for astronomical calculations.