Module_Numerics_zh - vorcyc/Vorcyc.Mathematics GitHub Wiki
本模块提供了多个与数值计算相关的类型。这些类型包括整数、浮点数、复数、有理数、分数等。这些类型提供了高精度的数值计算功能,并且可以与其他数值类型进行转换。
以下所有类型都位于命名空间 : Vorcyc.Mathematics.Numerics
📒目录
- 🔖 Vorcyc.Mathematics.Numerics.BigFloat 结构体
- 🔖 Vorcyc.Mathematics.Numerics.Complex 结构体
- 🔖 Vorcyc.Mathematics.Numerics.ComplexFp32 结构体
- 🔖 Vorcyc.Mathematics.Numerics.Int24 结构体
- 🔖 Vorcyc.Mathematics.Numerics.IntRange 结构体
- 🔖 Vorcyc.Mathematics.Numerics.Number 类
- 🔖 Vorcyc.Mathematics.Numerics.Point 结构体
- 🔖 Vorcyc.Mathematics.Numerics.PointFp32 结构体
- 🔖 Vorcyc.Mathematics.Numerics.Range 类
- 🔖 Vorcyc.Mathematics.Numerics.Rational 结构体
- 🔖 Vorcyc.Mathematics.Numerics.RectangleFP32 结构体
- 🔖 Vorcyc.Mathematics.Numerics.Size 结构体
- 🔖 Vorcyc.Mathematics.Numerics.SizeFp32 结构体
- 🔖 Vorcyc.Mathematics.Numerics.UInt24 结构体
一个表示高精度浮点数的结构体。
- 表示高精度浮点数
- 支持加、减、乘、除等运算
- 支持与其他数值类型的转换
- 支持平方根、幂运算等高级数学运算
-
字段:
DataBits,Scale,Size,Exponent,IsZero,OutOfPrecision,IsPositive,IsNegative,Sign,Int -
属性:
GetPrecision(只读),GetAccuracy(只读) -
方法:
Floor(实例),Ceiling(实例),Sqrt(静态),Pow(静态),Parse(静态),TryParse(静态) -
运算符重载:
+,-,*,/ -
接口实现:
IComparable,IComparable<BigFloat>,IEquatable<BigFloat>
BigFloat a = new BigFloat(123.456);
BigFloat b = new BigFloat("789.012");
BigFloat resultAdd = a + b;
BigFloat resultSub = a - b;
BigFloat resultMul = a * b;
BigFloat resultDiv = a / b;
BigFloat resultSqrt = BigFloat.Sqrt(a);
BigFloat resultPow = BigFloat.Pow(a, 2);
Console.WriteLine($"Add: {resultAdd}");
Console.WriteLine($"Subtract: {resultSub}");
Console.WriteLine($"Multiply: {resultMul}");
Console.WriteLine($"Divide: {resultDiv}");
Console.WriteLine($"Sqrt: {resultSqrt}");
Console.WriteLine($"Pow: {resultPow}");一个表示复数的结构体,具有泛型数值类型。
- 表示复数
- 支持加、减、乘、除等运算
- 支持极坐标转换
- 支持求模、求共轭等操作
-
字段:
Real,Imaginary -
属性:
Magnitude,Phase -
方法:
Add,Subtract,Multiply,Divide,Negate,Conjugate,Reciprocal,Abs,Sqrt,Pow,Log,Exp,Sin,Cos,Tan,Asin,Acos,Atan,Sinh,Cosh,Tanh -
运算符重载:
+,-,*,/,==,!= -
接口实现:
IEquatable<Complex<T>>,IFormattable,INumberBase<Complex<T>>,ISignedNumber<Complex<T>>
Complex<double> c1 = new Complex<double>(1.0, 2.0);
Complex<double> c2 = new Complex<double>(3.0, 4.0);
Complex<double> resultAdd = c1 + c2;
Complex<double> resultSub = c1 - c2;
Complex<double> resultMul = c1 * c2;
Complex<double> resultDiv = c1 / c2;
double magnitude = Complex<double>.Abs(c1);
Complex<double> conjugate = Complex<double>.Conjugate(c1);
Console.WriteLine($"Add: {resultAdd}");
Console.WriteLine($"Subtract: {resultSub}");
Console.WriteLine($"Multiply: {resultMul}");
Console.WriteLine($"Divide: {resultDiv}");
Console.WriteLine($"Magnitude: {magnitude}");
Console.WriteLine($"Conjugate: {conjugate}");一个表示复数的结构体,具有单精度浮点数分量。
- 表示复数
- 支持加、减、乘、除等运算
- 支持极坐标转换
- 支持求模、求共轭等操作
-
字段:
Real,Imaginary -
属性:
Magnitude,Phase -
方法:
Add,Subtract,Multiply,Divide,Negate,Conjugate,Reciprocal,Abs,Sqrt,Pow,Log,Exp,Sin,Cos,Tan,Asin,Acos,Atan,Sinh,Cosh,Tanh -
运算符重载:
+,-,*,/,==,!= -
接口实现:
IEquatable<ComplexFp32>,IFormattable,INumberBase<ComplexFp32>,ISignedNumber<ComplexFp32>
ComplexFp32 c1 = new ComplexFp32(1.0f, 2.0f);
ComplexFp32 c2 = new ComplexFp32(3.0f, 4.0f);
ComplexFp32 resultAdd = c1 + c2;
ComplexFp32 resultSub = c1 - c2;
ComplexFp32 resultMul = c1 * c2;
ComplexFp32 resultDiv = c1 / c2;
float magnitude = ComplexFp32.Abs(c1);
ComplexFp32 conjugate = ComplexFp32.Conjugate(c1);
Console.WriteLine($"Add: {resultAdd}");
Console.WriteLine($"Subtract: {resultSub}");
Console.WriteLine($"Multiply: {resultMul}");
Console.WriteLine($"Divide: {resultDiv}");
Console.WriteLine($"Magnitude: {magnitude}");
Console.WriteLine($"Conjugate: {conjugate}");一个表示24位有符号整数的结构体。
- 表示24位有符号整数
- 支持加、减、乘、除等运算
- 支持与其他数值类型的转换
-
运算符重载:
+,-,*,/ -
接口实现:
IMinMaxValue<Int24>,IAdditionOperators<Int24, Int24, Int24>,ISubtractionOperators<Int24, Int24, Int24>,IMultiplyOperators<Int24, Int24, Int24>,IDivisionOperators<Int24, Int24, Int24>,IAdditiveIdentity<Int24, Int24>,IComparable,IComparable<Int24>,IFormattable
Int24 a = new Int24(123456);
Int24 b = new Int24(654321);
Int24 resultAdd = a + b;
Int24 resultSub = a - b;
Int24 resultMul = a * b;
Int24 resultDiv = a / b;
Console.WriteLine($"Add: {resultAdd}");
Console.WriteLine($"Subtract: {resultSub}");
Console.WriteLine($"Multiply: {resultMul}");
Console.WriteLine($"Divide: {resultDiv}");一个表示整数范围的结构体。
- 表示整数范围
- 检查值是否在范围内
- 计算范围的交集
-
属性:
Min,Max,Length -
方法:
IsInside,Intersection,IsOverlapping,ToString -
运算符重载:
==,!= -
接口实现:
IEquatable<IntRange>,IEnumerable<int>
IntRange range = new IntRange(1, 10);
bool isInside = range.IsInside(5);
IntRange intersection = range.Intersection(new IntRange(5, 15));
Console.WriteLine($"IsInside: {isInside}");
Console.WriteLine($"Intersection: [{intersection.Min}, {intersection.Max}]");一个表示数字的类,作为数字列表,具有任意精度。
- 表示任意精度的数字
- 支持加、减、乘、除等运算
- 支持与其他数值类型的转换
-
属性:
DigitsCount,IntegerDigitsCount,FloatingDigitsCount,FirstDigit,LastDigit -
方法:
Abs,Round,Floor,Ceiling,GetFloatingPart,AppendFirstDigit,RemoveFirstDigit,AppendLastDigit,RemoveLastDigit -
运算符重载:
+,-,*,/ -
接口实现:
IComparable<Number>
Number n1 = new Number("12345678901234567890");
Number n2 = new Number("98765432109876543210");
Number resultAdd = n1 + n2;
Number resultSub = n1 - n2;
Number resultMul = n1 * n2;
Number resultDiv = n1 / n2;
Console.WriteLine($"Add: {resultAdd}");
Console.WriteLine($"Subtract: {resultSub}");
Console.WriteLine($"Multiply: {resultMul}");
Console.WriteLine($"Divide: {resultDiv}");一个表示二维平面上点的结构体,具有泛型数值类型。
- 表示二维平面上的点
- 支持加、减运算
- 支持距离计算
-
属性:
X,Y,IsEmpty -
方法:
Add,Subtract,ToString -
运算符重载:
+,-(与Size<T>、(T width, T height)元组或标量T一起使用;不支持Point<T>±Point<T>) -
接口实现:
IAdditionOperators<Point<T>, Size<T>, Point<T>>,ISubtractionOperators<Point<T>, Size<T>, Point<T>>,IEquatable<Point<T>>
Point<int> p = new Point<int>(1, 2);
Size<int> s = new Size<int>(3, 4);
Point<int> resultAdd = p + s;
Point<int> resultSub = p - s;
Point<float> pf1 = new Point<float>(1.0f, 2.0f);
Point<float> pf2 = new Point<float>(3.0f, 4.0f);
float distance = Point<float>.Distance(pf1, pf2);
Console.WriteLine($"Add: {resultAdd}");
Console.WriteLine($"Subtract: {resultSub}");
Console.WriteLine($"Distance: {distance}");一个表示二维平面上点的结构体,具有单精度浮点数坐标。
- 表示二维平面上的点
- 支持加、减运算
-
属性:
X,Y,IsEmpty -
方法:
Add,Subtract,ToString -
运算符重载:
+,-,==,!=
PointFp32 p = new PointFp32(1.0f, 2.0f);
SizeFp32 s = new SizeFp32(3.0f, 4.0f);
PointFp32 resultAdd = p + s;
PointFp32 resultSub = p - s;
Console.WriteLine($"Add: {resultAdd}");
Console.WriteLine($"Subtract: {resultSub}");一个表示值范围的类,具有泛型数值类型。
- 表示值范围
- 检查值是否在范围内
- 计算范围的交集和并集
-
属性:
Value,Minimum,Maximum -
方法:
IsInside,Intersection(当范围不重叠时返回null),Union,IsOverlapping,ToString -
接口实现:
IEquatable<Range<T>>
Range<int> range = new Range<int>(1, 10);
bool isInside = range.IsInside(5);
Range<int> intersection = range.Intersection(new Range<int>(5, 15));
Range<int> union = range.Union(new Range<int>(5, 15));
Console.WriteLine($"IsInside: {isInside}");
Console.WriteLine($"Intersection: [{intersection.Minimum}, {intersection.Maximum}]");
Console.WriteLine($"Union: [{union.Minimum}, {union.Maximum}]");一个表示有理数的结构体,具有泛型整数类型。
- 表示有理数
- 支持加、减、乘、除等运算
- 支持与其他数值类型的转换
-
字段:
Numerator,Denominator -
运算符:
+,-,*,/ -
方法:
Negate,Reciprocal,Abs,ToDouble,ToFloatingPointNumber<TFloatingNumber>() -
接口实现:
IComparable<Rational<T>>,IEquatable<Rational<T>>
Rational<int> r1 = new Rational<int>(1, 2);
Rational<int> r2 = new Rational<int>(3, 4);
Rational<int> resultAdd = r1 + r2;
Rational<int> resultSub = r1 - r2;
Rational<int> resultMul = r1 * r2;
Rational<int> resultDiv = r1 / r2;
Console.WriteLine($"Add: {resultAdd}");
Console.WriteLine($"Subtract: {resultSub}");
Console.WriteLine($"Multiply: {resultMul}");
Console.WriteLine($"Divide: {resultDiv}");一个表示具有浮点数坐标和尺寸的矩形结构体。
- 表示矩形
- 检查点是否在矩形内
- 计算矩形的交集和并集
-
属性:
X,Y,Width,Height,Location,Size,IsEmpty,Left,Right,Top,Bottom -
方法:
Contains,Inflate,Intersect,IntersectsWith,Offset,ToString -
运算符重载:
==,!=
RectangleFP32 rect = new RectangleFP32(0.0f, 0.0f, 10.0f, 20.0f);
bool contains = rect.Contains(5.0f, 5.0f);
RectangleFP32 intersection = RectangleFP32.Intersect(rect, new RectangleFP32(5.0f, 5.0f, 10.0f, 20.0f));
RectangleFP32 union = RectangleFP32.Union(rect, new RectangleFP32(5.0f, 5.0f, 10.0f, 20.0f));
Console.WriteLine($"Contains: {contains}");
Console.WriteLine($"Intersection: {intersection}");
Console.WriteLine($"Union: {union}");一个表示二维尺寸的结构体,具有泛型数值类型。
- 表示二维尺寸
- 支持加、减、乘、除等运算
-
属性:
Width,Height -
方法:
ToString -
运算符重载:
+,-,*,/(每个均与标量T一起使用;不支持Size<T>±Size<T>)
使用示例:
Size<int> size = new Size<int>(10, 20);
Size<int> resultAdd = size + 2;
Size<int> resultSub = size - 2;
Size<int> resultMul = size * 2;
Size<int> resultDiv = size / 2;
Console.WriteLine($"Add: {resultAdd}");
Console.WriteLine($"Subtract: {resultSub}");
Console.WriteLine($"Multiply: {resultMul}");
Console.WriteLine($"Divide: {resultDiv}");一个表示二维尺寸的结构体,具有单精度浮点数尺寸。
- 表示二维尺寸
- 支持加、减、乘、除等运算
-
属性:
Width,Height,IsEmpty -
方法:
Add,Subtract,ToString -
运算符重载:
+,-,==,!=
使用示例:
SizeFp32 size1 = new SizeFp32(10.0f, 20.0f);
SizeFp32 size2 = new SizeFp32(5.0f, 5.0f);
SizeFp32 resultAdd = size1 + size2;
SizeFp32 resultSub = size1 - size2;
Console.WriteLine($"Add: {resultAdd}");
Console.WriteLine($"Subtract: {resultSub}");一个表示24位无符号整数的结构体。
- 表示24位无符号整数
- 支持加、减、乘、除等运算
- 支持与其他数值类型的转换
-
运算符重载:
+,-,*,/ -
接口实现:
IMinMaxValue<UInt24>,IAdditionOperators<UInt24, UInt24, UInt24>,ISubtractionOperators<UInt24, UInt24, UInt24>,IMultiplyOperators<UInt24, UInt24, UInt24>,IDivisionOperators<UInt24, UInt24, UInt24>,IAdditiveIdentity<UInt24, UInt24>
使用示例:
UInt24 a = new UInt24(123456);
UInt24 b = new UInt24(654321);
UInt24 resultAdd = a + b;
UInt24 resultSub = a - b;
UInt24 resultMul = a * b;
UInt24 resultDiv = a / b;
Console.WriteLine($"Add: {resultAdd}");
Console.WriteLine($"Subtract: {resultSub}");
Console.WriteLine($"Multiply: {resultMul}");
Console.WriteLine($"Divide: {resultDiv}");