该 stdlib_constants 模块提供了数学常量和最常见的物理常量。
警告:最常见的物理常量的名称保持简短,因为它们位于专门的模块中。但是,如果存在重叠的名称,它们始终可以像下面这样重命名
use stdlib_constants, only: clight => c
该 stdlib_codata 模块将所有 codata(物理)常量定义为派生类型。该模块是使用一个简单的 用 Python 编写的解析器 自动生成的。最新的 codata 常量是由 NIST 在 2022 年发布的。codata 常量的所有值都以双精度实数提供。名称很长,可以使用较短的名称进行别名。
派生类型 codata_constant_type 定义了
4 个成员
name
(字符串)value
(双精度实数)uncertainty
(双精度实数)unit
(字符串)2 个类型绑定过程
print
:打印常量成员的值;to_real
:获取以所需精度表示的值或不确定度。模块级接口 to_real 可用于获取常数值或不确定度。
to_real
- 获取常数值或其不确定度。实验性
将 codata_constant_type 转换为 real
(至少为 sp
或 dp
)标量。警告:由于指数的值,某些常量无法转换为单精度 sp
实数。
r =
to_real (c, mold [, uncertainty])
c
:参数具有 intent(in)
,并且应为 codata_constant_type 类型。
mold
:参数具有 intent(in)
,并且应为 real
类型。注意:mold
参数的类型定义了结果的类型。
uncertainty
(可选):参数具有 intent(in)
,并且应为 logical
类型。它指定是否需要返回不确定度而不是值。默认为 .false.
。
返回 real
类型的标量,该标量是 codata 常量的值或不确定度。
program example_constants
use stdlib_constants, only: c, pi=>PI_dp
use stdlib_codata, only: alpha=>ALPHA_PARTICLE_ELECTRON_MASS_RATIO
use stdlib_codata_type, only : to_real
use stdlib_kinds, only: dp, sp
! Use most common physical constants defined as double precision reals
print *, "speed of light in vacuum= ", c
! Use of mathematical constants such as PI
print *, "PI as double precision real= ", pi
! Use codata_constant type for evaluating the value to the desired precision
print *, "Value of alpha... evaluated to double precision=", alpha%to_real(1.0_dp)
print *, "Uncertainty of alpha... evaluated to double precision=", alpha%to_real(1.0_sp, .true.)
print *, "Value of alpha... evaluated to single precision=", alpha%to_real(1.0_sp)
! Convert a codata constant to a real
print *, "Value of the alpha... evaluated to double precision=", to_real(alpha, 1.0_dp)
! Print out codata constant attributes: name, value, uncertainty and unit
call alpha%print()
end program example_constants