How to spot potential source of WTF

  • At least in Delphi programs you can check for "own implementation" of math rounding of floats. Everybody who programs in Delphi knows, that Delphi uses banker's rounding, but no too many people knows, that this setting is a default one and can be easily changed by setting math co-processor control word, or changing round mode by using SetRoundMode() function, or (at least in newer versions of Delphi) by using specialized rounding functions from Math unit (i.e. SimpleRoundTo()). So check for something like below:

    function RoundFloat(const Value: Extended;
      const Precision: Integer): Extended;
      nTmp, nRound:  Extended;
      if  Value < 0.0  then
      nRound := -0.5000001 else
      nRound := +0.5000001;
      if  Precision <> 0  then
        nTmp := Power(10, Precision);
        Result := Int(Value * nTmp + nRound) / nTmp;
      end { Precision > 0 } else
      Result := Int(Value + nRound);

  • Awesome! Wish I knew how to do it in C#.nET.

    I don't use Delphi, but knowing that you can change the default rounding behavior is wonderful. I can't figure out how to do it using C#.NET.  Thankfully when we were doing tax calculations, the comptroller was happy with banker's rounding (default in C#.NET).

    The fact that we were dealing entirely with positive numbers didn't hurt either. ;)

  • Notice the entry for Math.Round (Decimal, MidpointRounding):

    "Rounds a decimal value to the nearest integer. A parameter specifies how to round the value if it is midway between two other numbers."

    So if you were calling System.Math.Round(x), you just change your call and add one parameter:


    And now you know.

Log in to reply

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.