本文主要是介绍Lazarus分数计算,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
分数
Fractions 是使用分数进行计算的单元。 .
一、TFraction类型
TFraction是“高级记录”:具有方法和属性的记录。
二、TFraction属性
2.1分子属性Numerator :Int64;
获取或设置分子部分
2.2分母属性Denominator: Int64;
获取或设置分母部分
尝试设置分母为0,会弹出 EZeroDivide异常
三、TFraction方法
3.1Normalize;
通过将分子和Dominatr除以它们的最大公约数来收缩分数 例如 10/5 变为2/1
3.2ToString:String;
返回分数的字符串表示形式(不执行收缩), 例如当分子Numerator = 10 并且分母 Denominator = 5, 该函数返回 '10/5'
3.3 Resolve: String;
返回分数收缩后的字符串表示形式,例如,当分子Numerator = 4并且分母Denominator = 3, 函数返回'1 1/3'
3.4function ToFloat:Double;
返回分数的浮点表示形式,例如,当分子 Numerator = 1并且分母Denominator = 3, 函数返回 0.3333333333333
四、分配和创建分数
-
function Fraction(ANumerator, ADenominator: Int64): TFraction;
创建分数,例如Fraction(1,2) 代表 分数1/2
-
function Fraction(AIntPart, ANumerator, ADenominator: Int64): TFraction;
创建分数,例如 Fraction(1,1,2) 代表分数 1 1/2
五、支持的计算
-
=(等于)
-
<
(少于) -
>
(大于 ) -
<=(小于或等于)
-
> =(大于或等于)
-
:=
(赋值):此运算符允许在右侧使用Int64或String -
+
(加) -
-
(减) -
*
(乘):对于分数和Int64重载 -
/
(除):对于分数和Int64重载 -
**(幂):仅允许整数(在右侧)
六、数学函数
-
函数Min(a,b:TFraction):TFraction;inline;overload;
返回一个if(a <= b),否则重新运行b
-
函数Max(a,b:TFraction):TFraction;nline;overload;;
返回一个if(a> = b),否则返回b
-
函数InRange(const AValue,AMin,AMax:TFraction):boolean;nline;overload;;
返回真,如果(安勤> = AMIN)和(安勤<= AMAX),否则返回假
-
函数功能sureRange(const AValue,AMin,AMax:TFraction):TFraction;nline;overload;
如果(AValue <AMin),则返回AMin;如果(AValue> AMax),则返回AMax;否则,返回AValue
-
函数Sign(const AValue:TFraction):TValueSign;inline;overload;;
如果(AValue <0)则重新运行NegativeValue,如果(AValue> 0)则返回PositiveValue,否则返回ZeroValue
-
函数IsZero(const AValue:TFraction):boolean;overload;;
如果(AValue = 0)返回True,否则返回False
-
函数Abs(const AValue:TFraction):TFraction;overload;
如果(AValue> = 0)返回AValue,否则返回-AValue;
七、通用函数
-
function GreatestCommonDivisor(a, b: Int64): Int64;
返回a和b的最大除数
-
function Floor(D: Double): Int64; overload;
返回D的整数部分 (subtracted with 1, if (D < 0))
八、转换函数
-
函数FloatToFraction(Value,Precision:Double):TFraction;
以分数形式返回Value的近似值,例如FloatTofraction(0.5,0.01)返回1/2(一半)
精度定义了可接受的值(Abs(Result)-Abs(Value)<= Precision。
精度与值的关系最大为15
FloatToFraction实际上是一个函数变量,它已初始化为MF_FloatTofraction()函数。您可以根据需要分配自己的功能。
-
函数TryFloatToFraction(Value,Precision:Double; out F:TFraction; AcceptPrecisionError:Boolean):boolean;
返回值取决于AcceptPrecisionError值:
如果(AcceptPrecisionError = True),则如果可以找到近似值,则该函数将重新运行True
如果(AcceptPrecisionError = False)如果可以找到近似值,则函数返回True,并且Abs(Abs(Result)-Abs(Value))<=精度
仅当函数返回True时F的值才有意义
-
函数FloatToFractionDef(Value,Precision:Double; Def:TFraction; AcceptPrecisionError:Boolean):TFraction;
如果TryFloatToFraction(Value,Precision,F,AcceptPrecisionError:Boolean)成功,则返回找到的近似值(F),否则返回Def
-
函数StrToFraction(const S:String):TFraction;
返回以S表示的分数,失败时引发EConvertError
-
函数TryStrToFraction(const S:String; out F:TFraction):布尔值;
如果S可以转换为分数,则重新运行True,否则返回False
F的值仅在f函数返回True时才有意义
-
函数StrToFractionDef(const S:String; Def:TFraction):TFraction;
如果转换成功,则返回以S表示的分数,否则返回Def
九、示例
program example;{$mode objfpc}{$H+}
{$apptype console}uses
Classes, sysutils, fractions, math;var
F1, F2: TFraction;
D, Prec: Double;
i: Integer;
begin
F1 := Fraction(1,1,3); // 1 1/3
F2 := Fraction(4,3); // 4/3
writeln('F1.ToString = ',F1.ToString); // '4/3'
writeln('F1.Resolve = ',F1.Resolve); // '1 1/3'
writeln('F1.ToFloat = ',F1.ToFloat:16:16); // 1.3333333333333333
writeln('F2.ToString = ',F2.ToString); // '4/3'
writeln('(F1 = F2) = ',F1=F2); //True F1 := Fraction(1,2);
F2 := Fraction(1,3);
writeln(F1.ToString,' * ',F2.ToString,' = ',(F1*F2).Resolve); // '1/6'
writeln(F1.ToString,' / ',F2.ToString,' = ',(F1/F2).Resolve); // '1 1/2'
writeln(F1.ToString,' + ',F2.ToString,' = ',(F1+F2).Resolve); // '5/6'
writeln(F1.ToString,' - ',F2.ToString,' = ',(F1-F2).Resolve); // '1/6'
writeln(F1.ToString,' ** 2 = ',(F1**2).Resolve); // '1/6' D := 0.25;
F1 := FloatToFraction(D, 0.000001);
writeln('FloatTofraction(0.25) -> ',F1.ToString); // '1/4'
writeln;
writeln('Approximations of Pi:');
writeln(' [Pi = ',Pi:16:16,']');
Prec := 1.0;
for i := 1 to 10 do
begin
Prec := Prec / 10;
F2 := FloatTofraction(Pi, Prec);
writeln('FloatTofraction(Pi,',Prec:10:10,') = ',Format('%-13s',[F2.Resolve]),' [',F2.ToFloat:16:16,']');
end;
end.
{$mode objfpc}{$H+}
{$apptype console}uses
Classes, sysutils, fractions, math;var
F1, F2: TFraction;
D, Prec: Double;
i: Integer;
begin
F1 := Fraction(1,1,3); // 1 1/3
F2 := Fraction(4,3); // 4/3
writeln('F1.ToString = ',F1.ToString); // '4/3'
writeln('F1.Resolve = ',F1.Resolve); // '1 1/3'
writeln('F1.ToFloat = ',F1.ToFloat:16:16); // 1.3333333333333333
writeln('F2.ToString = ',F2.ToString); // '4/3'
writeln('(F1 = F2) = ',F1=F2); //True F1 := Fraction(1,2);
F2 := Fraction(1,3);
writeln(F1.ToString,' * ',F2.ToString,' = ',(F1*F2).Resolve); // '1/6'
writeln(F1.ToString,' / ',F2.ToString,' = ',(F1/F2).Resolve); // '1 1/2'
writeln(F1.ToString,' + ',F2.ToString,' = ',(F1+F2).Resolve); // '5/6'
writeln(F1.ToString,' - ',F2.ToString,' = ',(F1-F2).Resolve); // '1/6'
writeln(F1.ToString,' ** 2 = ',(F1**2).Resolve); // '1/6' D := 0.25;
F1 := FloatToFraction(D, 0.000001);
writeln('FloatTofraction(0.25) -> ',F1.ToString); // '1/4'
writeln;
writeln('Approximations of Pi:');
writeln(' [Pi = ',Pi:16:16,']');
Prec := 1.0;
for i := 1 to 10 do
begin
Prec := Prec / 10;
F2 := FloatTofraction(Pi, Prec);
writeln('FloatTofraction(Pi,',Prec:10:10,') = ',Format('%-13s',[F2.Resolve]),' [',F2.ToFloat:16:16,']');
end;
end.
输出:
F1.ToString = 4/3
F1.Resolve = 1 1/3
F1.ToFloat = 1.3333333333333333
F2.ToString = 4/3
(F1 = F2) = TRUE
1/2 * 1/3 = 1/6
1/2 / 1/3 = 1 1/2
1/2 + 1/3 = 5/6
1/2 - 1/3 = 1/6
1/2 ** 2 = 1/4
FloatTofraction(0.25) -> 1/4Approximations of Pi:
[Pi = 3.1415926535897932]
FloatTofraction(Pi,0.1000000000) = 3 1/7 [3.1428571428571428]
FloatTofraction(Pi,0.0100000000) = 3 1/7 [3.1428571428571428]
FloatTofraction(Pi,0.0010000000) = 3 16/113 [3.1415929203539825]
FloatTofraction(Pi,0.0001000000) = 3 16/113 [3.1415929203539825]
FloatTofraction(Pi,0.0000100000) = 3 16/113 [3.1415929203539825]
FloatTofraction(Pi,0.0000010000) = 3 16/113 [3.1415929203539825]
FloatTofraction(Pi,0.0000001000) = 3 4703/33215 [3.1415926539214212]
FloatTofraction(Pi,0.0000000100) = 3 4703/33215 [3.1415926539214212]
FloatTofraction(Pi,0.0000000010) = 3 4703/33215 [3.1415926539214212]
FloatTofraction(Pi,0.0000000001) = 3 14093/99532 [3.1415926536189365]
F1.Resolve = 1 1/3
F1.ToFloat = 1.3333333333333333
F2.ToString = 4/3
(F1 = F2) = TRUE
1/2 * 1/3 = 1/6
1/2 / 1/3 = 1 1/2
1/2 + 1/3 = 5/6
1/2 - 1/3 = 1/6
1/2 ** 2 = 1/4
FloatTofraction(0.25) -> 1/4Approximations of Pi:
[Pi = 3.1415926535897932]
FloatTofraction(Pi,0.1000000000) = 3 1/7 [3.1428571428571428]
FloatTofraction(Pi,0.0100000000) = 3 1/7 [3.1428571428571428]
FloatTofraction(Pi,0.0010000000) = 3 16/113 [3.1415929203539825]
FloatTofraction(Pi,0.0001000000) = 3 16/113 [3.1415929203539825]
FloatTofraction(Pi,0.0000100000) = 3 16/113 [3.1415929203539825]
FloatTofraction(Pi,0.0000010000) = 3 16/113 [3.1415929203539825]
FloatTofraction(Pi,0.0000001000) = 3 4703/33215 [3.1415926539214212]
FloatTofraction(Pi,0.0000000100) = 3 4703/33215 [3.1415926539214212]
FloatTofraction(Pi,0.0000000010) = 3 4703/33215 [3.1415926539214212]
FloatTofraction(Pi,0.0000000001) = 3 14093/99532 [3.1415926536189365]
这篇关于Lazarus分数计算的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!