简体   繁体   中英

What is the equivalent of Math.Round() with MidpointRounding.AwayFromZero in Delphi?

How do I use c# similar Math.Round with MidpointRounding.AwayFromZero in Delphi?

What will be the equivalent of:

double d = 2.125;
Console.WriteLine(Math.Round(d, 2, MidpointRounding.AwayFromZero));

Output: 2.13

In Delphi?

I believe the Delphi RTL's SimpleRoundTo function does essentially this, at least if the FPU rounding mode is "correct". Please read its documentation and implementation carefully, and then decide if it is good enough for your purposes.

But beware that setting the rounding mode for a single rounding operation like this is using a global change to solve a local problem. This might cause problems (multi-threading, libraries, etc.).

Bonus chatter: Had the question been about "regular" rounding (to an integer), I think I'd tried an approach like

function RoundMidpAway(const X: Real): Integer;
begin
  Result := Trunc(X);
  if Abs(Frac(X)) >= 0.5 then
    Inc(Result, Sign(X));
end;

instead.

Of course, it is possible to write a similar function even for the general case of n fractional digits. (But be careful to handle edge cases, overflows, floating-point issues, etc., correctly.)

Update: I believe the following does the trick (and is fast):

function RoundMidpAway(const X: Real): Integer; overload;
begin
  Result := Trunc(X);
  if Abs(Frac(X)) >= 0.5 then
    Inc(Result, Sign(X));
end;

function RoundMidpAway(const X: Real; ADigit: integer): Real; overload;
const
  PowersOfTen: array[-10..10] of Real =
    (
      0.0000000001,
      0.000000001,
      0.00000001,
      0.0000001,
      0.000001,
      0.00001,
      0.0001,
      0.001,
      0.01,
      0.1,
      1,
      10,
      100,
      1000,
      10000,
      100000,
      1000000,
      10000000,
      100000000,
      1000000000,
      10000000000
    );
var
  MagnifiedValue: Real;
begin
  if not InRange(ADigit, Low(PowersOfTen), High(PowersOfTen)) then
    raise EInvalidArgument.Create('Invalid digit index.');
  MagnifiedValue := X * PowersOfTen[-ADigit];
  Result := RoundMidpAway(MagnifiedValue) * PowersOfTen[ADigit];
end;

Of course, if you'd use this function in production code, you'd also add at least 50 unit test cases that test its correctness (to be run daily).

Update: I believe the following version is more stable:

function RoundMidpAway(const X: Real; ADigit: integer): Real; overload;
const
  FuzzFactor = 1000;
  DoubleResolution = 1E-15 * FuzzFactor;
  PowersOfTen: array[-10..10] of Real =
    (
      0.0000000001,
      0.000000001,
      0.00000001,
      0.0000001,
      0.000001,
      0.00001,
      0.0001,
      0.001,
      0.01,
      0.1,
      1,
      10,
      100,
      1000,
      10000,
      100000,
      1000000,
      10000000,
      100000000,
      1000000000,
      10000000000
    );
var
  MagnifiedValue: Real;
  TruncatedValue: Real;
begin

  if not InRange(ADigit, Low(PowersOfTen), High(PowersOfTen)) then
    raise EInvalidArgument.Create('Invalid digit index.');
  MagnifiedValue := X * PowersOfTen[-ADigit];

  TruncatedValue := Int(MagnifiedValue);
  if CompareValue(Abs(Frac(MagnifiedValue)), 0.5, DoubleResolution * PowersOfTen[-ADigit]) >= EqualsValue  then
    TruncatedValue := TruncatedValue + Sign(MagnifiedValue);

  Result := TruncatedValue * PowersOfTen[ADigit];

end;

but I haven't fully tested it. (Currently it passes 900+ unit test cases , but I don't consider the test suite quite sufficient yet.)

What you're looking for is SimpleRoundTo function in combination with SetRoundMode . As the documentations says:

SimpleRoundTo returns the nearest value that has the specified power of ten. In case AValue is exactly in the middle of the two nearest values that have the specified power of ten (above and below), this function returns:

  • The value toward plus infinity if AValue is positive.

  • The value toward minus infinity if AValue is negative and the FPU rounding mode is not set to rmUp

Note that the second parameter to the function is TRoundToRange which refers to exponent (power of 10) rather than number of fractional digis in .NET's Math.Round method. Therefore to round to 2 decimal places you use -2 as round-to range.

uses Math, RTTI;

var
  LRoundingMode: TRoundingMode;
begin
  for LRoundingMode := Low(TRoundingMode) to High(TRoundingMode) do
  begin
    SetRoundMode(LRoundingMode);
    Writeln(TRttiEnumerationType.GetName(LRoundingMode));
    Writeln(SimpleRoundTo(2.125, -2).ToString);
    Writeln(SimpleRoundTo(-2.125, -2).ToString);
  end;
end;

rmNearest

2,13

-2,13

rmDown

2,13

-2,13

rmUp

2,13

-2,12

rmTruncate

2,13

-2,13

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM