[ruby-talk:444256] Rounding half even rounds to odd number

Hey,

I was browsing December commit history and found a fix for round to even
function being commited for special cases.

In the unit tests there were a few new tests added. One of them looks like
this:

    assert_equal(767573.18759, 767573.1875850001.round(5, half: :even))

Shouldn't the function round to ...8 in this case? It struck me it does not
end with an even digit, but an odd one.

There are two or free unit tests that also end with odd digit.

Commit:

Tests:

Cheers,
Tom Smykowski

hi Tomasz

you ask about the rounding in ruby.
but why can we not just let the cpu's do that?

This is a big problem with Java. We do not want to repeat that.
It is not the job of the language to care of every bit of the floats.

I believe that x86 and arm now pretty much agree on ieee 754
It is not the job of the language to intervene with that.

In the end, the differences will only show up in the least significant digit.

It is quite important to have repducible results.
But if an algorithm is that bad, that it would diverge badly?
I actually opt for it, to compute with different roundings.

···

On Sun, Jun 25, 2023 at 11:39 PM Tomasz Smykowski via ruby-talk <ruby-talk@ml.ruby-lang.org> wrote:

Hey,

I was browsing December commit history and found a fix for round to even function being commited for special cases.

In the unit tests there were a few new tests added. One of them looks like this:

    assert_equal(767573.18759, 767573.1875850001.round(5, half: :even))

Shouldn't the function round to ...8 in this case? It struck me it does not end with an even digit, but an odd one.

There are two or free unit tests that also end with odd digit.

Commit: numeric.c: Fix round_half_even for specific values (#7023) · ruby/ruby@9f23789 · GitHub

Tests: https://github.com/ruby/ruby/blob/ed587abffb48da991c33d232fd151683458938a9/test/ruby/test_float.rb#L4

Cheers,
Tom Smykowski
______________________________________________
ruby-talk mailing list -- ruby-talk@ml.ruby-lang.org
To unsubscribe send an email to ruby-talk-leave@ml.ruby-lang.org
ruby-talk info -- Info | ruby-talk@ml.ruby-lang.org - ml.ruby-lang.org

______________________________________________
ruby-talk mailing list -- ruby-talk@ml.ruby-lang.org
To unsubscribe send an email to ruby-talk-leave@ml.ruby-lang.org
ruby-talk info -- Info | ruby-talk@ml.ruby-lang.org - ml.ruby-lang.org

I believe it's because 0.50001 isn't half, so it rounds naturally. The
half-even tie break only occurs at exactly ...5

···

On Mon, 26 Jun 2023 at 07:39, Tomasz Smykowski via ruby-talk <ruby-talk@ml.ruby-lang.org> wrote:

Hey,

I was browsing December commit history and found a fix for round to even function being commited for special cases.

In the unit tests there were a few new tests added. One of them looks like this:

    assert_equal(767573.18759, 767573.1875850001.round(5, half: :even))

Shouldn't the function round to ...8 in this case? It struck me it does not end with an even digit, but an odd one.

There are two or free unit tests that also end with odd digit.

Commit: numeric.c: Fix round_half_even for specific values (#7023) · ruby/ruby@9f23789 · GitHub

Tests: https://github.com/ruby/ruby/blob/ed587abffb48da991c33d232fd151683458938a9/test/ruby/test_float.rb#L4

Cheers,
Tom Smykowski
______________________________________________
ruby-talk mailing list -- ruby-talk@ml.ruby-lang.org
To unsubscribe send an email to ruby-talk-leave@ml.ruby-lang.org
ruby-talk info -- Info | ruby-talk@ml.ruby-lang.org - ml.ruby-lang.org

--
  Matthew Kerwin
  https://matthew.kerwin.net.au/
______________________________________________
ruby-talk mailing list -- ruby-talk@ml.ruby-lang.org
To unsubscribe send an email to ruby-talk-leave@ml.ruby-lang.org
ruby-talk info -- Info | ruby-talk@ml.ruby-lang.org - ml.ruby-lang.org

If you don't want to specify the rounding (to leave it up to the CPU,
as you say) then... don't specify the rounding? This is for people who
actually want to perform precise mathematical calculations with
repeatable results, and would like to continue using Ruby when doing
so.

Cheers

···

On Mon, 26 Jun 2023 at 11:13, Eike Dierks via ruby-talk <ruby-talk@ml.ruby-lang.org> wrote:

hi Tomasz

you ask about the rounding in ruby.
but why can we not just let the cpu's do that?

This is a big problem with Java. We do not want to repeat that.
It is not the job of the language to care of every bit of the floats.

I believe that x86 and arm now pretty much agree on ieee 754
It is not the job of the language to intervene with that.

In the end, the differences will only show up in the least significant digit.

It is quite important to have repducible results.
But if an algorithm is that bad, that it would diverge badly?
I actually opt for it, to compute with different roundings.

--
  Matthew Kerwin
  https://matthew.kerwin.net.au/
______________________________________________
ruby-talk mailing list -- ruby-talk@ml.ruby-lang.org
To unsubscribe send an email to ruby-talk-leave@ml.ruby-lang.org
ruby-talk info -- Info | ruby-talk@ml.ruby-lang.org - ml.ruby-lang.org

Thanks!

···

On 29/06/2023 01:42, Matthew Kerwin via ruby-talk wrote:

I believe it's because 0.50001 isn't half, so it rounds naturally. The
half-even tie break only occurs at exactly ...5

On Mon, 26 Jun 2023 at 07:39, Tomasz Smykowski via ruby-talk > <ruby-talk@ml.ruby-lang.org> wrote:

Hey,

I was browsing December commit history and found a fix for round to even function being commited for special cases.

In the unit tests there were a few new tests added. One of them looks like this:

     assert_equal(767573.18759, 767573.1875850001.round(5, half: :even))

Shouldn't the function round to ...8 in this case? It struck me it does not end with an even digit, but an odd one.

There are two or free unit tests that also end with odd digit.

Commit: numeric.c: Fix round_half_even for specific values (#7023) · ruby/ruby@9f23789 · GitHub

Tests: https://github.com/ruby/ruby/blob/ed587abffb48da991c33d232fd151683458938a9/test/ruby/test_float.rb#L4

Cheers,
Tom Smykowski
  ______________________________________________
  ruby-talk mailing list -- ruby-talk@ml.ruby-lang.org
  To unsubscribe send an email to ruby-talk-leave@ml.ruby-lang.org
  ruby-talk info -- Info | ruby-talk@ml.ruby-lang.org - ml.ruby-lang.org

--

z pozdrowieniami,
Tomasz Smykowski
______________________________________________
ruby-talk mailing list -- ruby-talk@ml.ruby-lang.org
To unsubscribe send an email to ruby-talk-leave@ml.ruby-lang.org
ruby-talk info -- Info | ruby-talk@ml.ruby-lang.org - ml.ruby-lang.org