Recent

Author Topic: Round to nearest integer or round up if exactly in middle?  (Read 19600 times)

Thaddy

  • Hero Member
  • *****
  • Posts: 18765
  • To Europe: simply sell USA bonds: dollar collapses
Re: Round to nearest integer or round up if exactly in middle?
« Reply #15 on: April 04, 2017, 03:49:51 pm »
@Thaddy
It's not a bug: "In the case of .5, the algorithm uses "banker's rounding": .5 values are always rounded towards the even number. "
http://www.freepascal.org/docs-html/rtl/system/round.html
Oops, you are correct. And I am using bankers rounding all my life (except true standard bankers rounding is usually always on the sixth decimal (to the fifth), balance sheets and current accounts in 1000th' excluded);
I should have known better.
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

srcstorm

  • New Member
  • *
  • Posts: 21
Re: Round to nearest integer or round up if exactly in middle?
« Reply #16 on: April 04, 2017, 06:32:19 pm »
You can try this workaround:

Code: Pascal  [Select][+][-]
  1. function RoundHalfUpSymmetric(const d: double): integer;
  2. begin
  3.   result := 0;
  4.   if d <> 0.0 then
  5.   begin
  6.     result := trunc(d);
  7.     if abs(frac(d)) >= 0.5 then
  8.       if d > 0.0 then
  9.         inc(result)
  10.       else
  11.         dec(result)
  12.   end
  13. end;
  14.  

Thaddy

  • Hero Member
  • *****
  • Posts: 18765
  • To Europe: simply sell USA bonds: dollar collapses
Re: Round to nearest integer or round up if exactly in middle?
« Reply #17 on: April 04, 2017, 06:36:29 pm »
I would write
Code: Pascal  [Select][+][-]
  1. d.frac
Provided sysutils is included..
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

Mr.Madguy

  • Hero Member
  • *****
  • Posts: 881
Re: Round to nearest integer or round up if exactly in middle?
« Reply #18 on: April 05, 2017, 08:52:13 am »
I don't know. 0.5 is 1^-2, i.e. it's number, that can be precisely represented by either fixed point or floating point number. It's binary 0.1. That means, Trunc(X + 0.5) should work properly in virtually any situation.
Is it healthy for project not to have regular stable releases?
Just for fun: Code::Blocks, GCC 13 and DOS - is it possible?

Thaddy

  • Hero Member
  • *****
  • Posts: 18765
  • To Europe: simply sell USA bonds: dollar collapses
Re: Round to nearest integer or round up if exactly in middle?
« Reply #19 on: April 05, 2017, 10:58:14 am »
I don't know. 0.5 is 1^-2, i.e. it's number, that can be precisely represented by either fixed point or floating point number. It's binary 0.1. That means, Trunc(X + 0.5) should work properly in virtually any situation.
No. Because of the rounding mode of the CPU. Which is configurable. (SetRoundMode)
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

Gerhard Fourie

  • Newbie
  • Posts: 6
Re: Round to nearest integer or round up if exactly in middle?
« Reply #20 on: August 05, 2017, 06:27:44 pm »
 I do a lot of technical programming, and "banker's" rounding does not do it for me.

My Delphi "MyUtils.pas" unit contains this function:

      function RoundCorrect(R: Real): LongInt;
      begin
        Result:= Trunc(R);       // extract the integer part
        if Frac(R) >= 0.5 then   // if fractional part >= 0.5 then...
          Result:= Result + 1;   // ...add 1
      end;

Handoko

  • Hero Member
  • *****
  • Posts: 5524
  • My goal: build my own game engine using Lazarus
Re: Round to nearest integer or round up if exactly in middle?
« Reply #21 on: August 05, 2017, 06:33:56 pm »
Hello Gerhard Fourie,
Welcome to the forum.

You don't need to write your own function. You can use SetRoundMode just as Thaddy said.
https://www.freepascal.org/docs-html/rtl/math/setroundmode.html
« Last Edit: August 05, 2017, 06:36:06 pm by Handoko »

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Round to nearest integer or round up if exactly in middle?
« Reply #22 on: August 05, 2017, 06:36:01 pm »
I do a lot of technical programming, and "banker's" rounding does not do it for me.

My Delphi "MyUtils.pas" unit contains this function:

      function RoundCorrect(R: Real): LongInt;
      begin
        Result:= Trunc(R);       // extract the integer part
        if Frac(R) >= 0.5 then   // if fractional part >= 0.5 then...
          Result:= Result + 1;   // ...add 1
      end;
and this works correctly with negative numbers too?
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Handoko

  • Hero Member
  • *****
  • Posts: 5524
  • My goal: build my own game engine using Lazarus
Re: Round to nearest integer or round up if exactly in middle?
« Reply #23 on: August 05, 2017, 06:46:19 pm »
According to this wiki page, it is correct and called as Round Half Up
https://en.wikipedia.org/wiki/Rounding#Round_half_up


Sorry, I was wrong. Need more samples to test.

According to this page, that code doesn't produce correct result:
https://en.wikipedia.org/wiki/Rounding#Comparison_of_rounding_modes
« Last Edit: August 05, 2017, 07:00:14 pm by Handoko »

Thaddy

  • Hero Member
  • *****
  • Posts: 18765
  • To Europe: simply sell USA bonds: dollar collapses
Re: Round to nearest integer or round up if exactly in middle?
« Reply #24 on: August 05, 2017, 07:42:23 pm »
Handoko, lest you forgot, https://www.freepascal.org/docs-html/rtl/math/setroundmode.html.
I gave that already some time ago.

Note that it is the responsibility of the programmer to restore the rounding mode if he needs something else than the default.
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

Gerhard Fourie

  • Newbie
  • Posts: 6
Re: Round to nearest integer or round up if exactly in middle?
« Reply #25 on: August 05, 2017, 09:43:32 pm »
I do a lot of technical programming, and "banker's" rounding does not do it for me.

My Delphi "MyUtils.pas" unit contains this function:

      function RoundCorrect(R: Real): LongInt;
      begin
        Result:= Trunc(R);       // extract the integer part
        if Frac(R) >= 0.5 then   // if fractional part >= 0.5 then...
          Result:= Result + 1;   // ...add 1
      end;
and this works correctly with negative numbers too?

You are right, negative numbers need a bit of a correction:

      function RoundCorrect(R: Real): LongInt;
      begin
        Result:= Trunc(R);                          // extract the integer part
        if Abs(Frac(R)) >= 0.5 then                 // if fractional part >= 0.5 then...
          Result := Result + Trunc(2.0 * Frac(R));  // add +1 or -1, depending on R's sign
      end;

PS I wrote this for Delphi5, which to my knowledge did not have 'SetRoundMode' as an option.

 

TinyPortal © 2005-2018