//整除与余数: DivMod const a = 11; b = 3; var x,y: Word; begin ShowMessage(IntToStr(a div b)); {整除得 3} ShowMessage(IntToStr(a mod b)); {余数 2} DivMod(a,b,x,y); ShowMessage(IntToStr(x)); {3} ShowMessage(IntToStr(y)); {2} end; //返回整数: Ceil、Floor var d1,d2: Real; const d = 1.2; begin {向大补入} d1 := Ceil(d); d2 := Ceil(-d); ShowMessage(FloatToStr(d1)); {2} ShowMessage(FloatToStr(d2)); {-1} {向小舍入} d1 := Floor(d); d2 := Floor(-d); ShowMessage(FloatToStr(d1)); {1} ShowMessage(FloatToStr(d2)); {-2} end; //最大值与最小值: Max、Min var d: Real; begin d := Max(5.2, -2); ShowMessage(FloatToStr(d)); {5.2} d := Min(5.2, -2); ShowMessage(FloatToStr(d)); {-2} end; //Double 数组求和: Sum var d: Real; const arr: array[0..3] of Double = (1, 2, 3, -4.5); begin d := Sum(arr); {要求必须是 Double 数组} ShowMessage(FloatToStr(d)); {1.5} end; //Double 数组求平均值: Mean var arr: array[0..5] of Double; d: Double; begin arr[0] := -1.2; arr[1] := 1.0; arr[2] := 2.5; arr[3] := 3.0; arr[4] := 4.5; arr[5] := 25.0; d := Mean(arr); ShowMessage(FloatToStr(d)); {5.8} end; //计算一个数的 n 次方: Power var d: Real; begin d := Power(3,2); {3 的 2 次方} ShowMessage(FloatToStr(d)); {9} end; //判断在一个误差范围内, 一个数字是否为0: IsZero const d1 = 0.4; d2 = -0.1; d3 = 3; var b: Boolean; begin b := IsZero(d1, 0.4); {参数2是误差范围} ShowMessage(BoolToStr(b)); {True} b := IsZero(d2, 0.4); ShowMessage(BoolToStr(b)); {True} b := IsZero(d3, 0.4); ShowMessage(BoolToStr(b)); {False} b := IsZero(d3); {参数2是可选的, 默认是0} ShowMessage(BoolToStr(b)); {False} end; //比较两个参数在一定误差范围内是否相等: SameValue var b: Boolean; begin b := SameValue(1.2, 1.21, 0.01); {误差是 0.01} ShowMessage(BoolToStr(b)); {True} b := SameValue(1.2, 1.21); {误差默认是 0} ShowMessage(BoolToStr(b)); {False} end; //判断一个数的正负 var v: TValueSign; {-1..1} begin v := Sign(22); ShowMessage(IntToStr(v)); {返回 1 表示是正数} v := Sign(-22); ShowMessage(IntToStr(v)); {返回 -1 表示是负数} v := Sign(0); ShowMessage(IntToStr(v)); {返回 0 表示是 0} end; //舍入位数: RoundTo、SimpleRoundTo var d: Real; const d1 = 1234567; d2 = 1.234; d3 = 1.235; d4 = - 1.235; begin d := RoundTo(d1,3); ShowMessage(FloatToStr(d)); {123500} d := RoundTo(d2,-2); ShowMessage(FloatToStr(d)); {1.23} d := RoundTo(d3,-2); ShowMessage(FloatToStr(d)); {1.24} d := RoundTo(d4,-2); ShowMessage(FloatToStr(d)); {-1.24} d := SimpleRoundTo(d1,3); ShowMessage(FloatToStr(d)); {123500} d := SimpleRoundTo(d2,-2); ShowMessage(FloatToStr(d)); {1.23} d := SimpleRoundTo(d3,-2); ShowMessage(FloatToStr(d)); {1.24} d := SimpleRoundTo(d4,-2); ShowMessage(FloatToStr(d)); {-1.24} {RoundTo 使用银行家舍入法; SimpleRoundTo 使用不均衡算法舍入, 适合更大的值} end;
Math 中常用的数学函数
最新推荐文章于 2021-02-27 05:57:11 发布