Rounding to X digits

In message Re: Rounding to X digits

···

on Sat, 16 Oct 2004 08:23:49 +0900 Markus wrote:

On Sat, 16 Oct 2004, GOTO Kentaro wrote:
> sprintf(3) conform to ISO 31-0 but Float#round doesn't.
>
> puts 0.5.round #=> 1
> puts sprintf("%.0f", 0.5) #=> 0
>

     Both are (IIRC) ISO 31-0 conformant, but one uses ISO 31-0 B.3 rule A
and the other uses ISO 31-0 B.3 rule B.

     That said, one of the rules (the one sprintf uses, to round down
following even digits is an abomination and should be eliminated from the
face of the earth. nnn.nn5 should ALWAYS round up and rounded values
should not be re-rounded (which is what the contrary rule assumes is
happening).

Thanks Markus. I had misunderstood long time.

I found a citation (probably) from ISO 31-0 Annex B
http://www.dataaction.co.jp/31-e.htm

Gotoken

[Slim:~] gavinkis% ruby -e "p Array.new(10000).collect{ sprintf('%.0f',0.5) }.uniq"
["0"]
[Slim:~] gavinkis% ruby --version
ruby 1.8.2 (2004-07-29) [powerpc-darwin7.5.0]

···

On Oct 18, 2004, at 1:19 AM, Robert Klemme wrote:

    puts 0.5.round #=> 1
    puts sprintf("%.0f", 0.5) #=> 0

Which version of Ruby are you using?

sprintf("%.0f", 0.5)

=> "1"

RUBY_VERSION

=> "1.8.1"

--
(-, /\ \/ / /\/

Yes, that's exactly why it's there. But there are several reasons
(e.g. Benford's Law http://mathworld.wolfram.com/BenfordsLaw.html\) why
it doesn't even work for its intended purpose. The real answer is to
not re-round the data.

     As for developing strong feelings about it, all you need to do is
work downstream from someone who swears by it and try to do some valid
numerical analysis. The only person I ever knew with a worse lament was
a friend who worked downstream of an astronomy professor (who should
have been emeritusized years before) that routinely converted his images
to jpeg "to save disk space."

    -- Markus

···

On Fri, 2004-10-15 at 17:58, trans. (T. Onoma) wrote:

On Friday 15 October 2004 07:23 pm, markus@reality.com wrote:
> > puts 0.5.round #=> 1
> > puts sprintf("%.0f", 0.5) #=> 0
>
> Both are (IIRC) ISO 31-0 conformant, but one uses ISO 31-0 B.3 rule A
> and the other uses ISO 31-0 B.3 rule B.
>
> That said, one of the rules (the one sprintf uses, to round down
> following even digits is an abomination and should be eliminated from the
> face of the earth. nnn.nn5 should ALWAYS round up and rounded values
> should not be re-rounded (which is what the contrary rule assumes is
> happening).

Apparently the idea of even vs. odd rounding was to help prevent "inflational"
rounding --successive rounding pushing values upward. I think K&R actually
supported the idea. I'm not sure how I feel about it.

"Gavin Kistner" <gavin@refinery.com> schrieb im Newsbeitrag
news:1ABCF62A-2107-11D9-A7C3-000A959CF5AC@refinery.com...

>> puts 0.5.round #=> 1
>> puts sprintf("%.0f", 0.5) #=> 0
> Which version of Ruby are you using?
>>> sprintf("%.0f", 0.5)
> => "1"
>>> RUBY_VERSION
> => "1.8.1"

[Slim:~] gavinkis% ruby -e "p Array.new(10000).collect{

Do you think the 10000 makes it more convincing? :slight_smile:

sprintf('%.0f',0.5) }.uniq"
["0"]
[Slim:~] gavinkis% ruby --version
ruby 1.8.2 (2004-07-29) [powerpc-darwin7.5.0]

Now, is this due to the Ruby version or rather the libC version (I tested
with cygwin)?

Kind regards

    robert

···

On Oct 18, 2004, at 1:19 AM, Robert Klemme wrote:

"Markus" <markus@reality.com> schrieb im Newsbeitrag
news:1097901288.21256.339.camel@lapdog.reality.com...

> > > puts 0.5.round #=> 1
> > > puts sprintf("%.0f", 0.5) #=> 0
> >
> > Both are (IIRC) ISO 31-0 conformant, but one uses ISO 31-0 B.3

rule A

> > and the other uses ISO 31-0 B.3 rule B.
> >
> > That said, one of the rules (the one sprintf uses, to round

down

> > following even digits is an abomination and should be eliminated

from the

> > face of the earth. nnn.nn5 should ALWAYS round up and rounded

values

> > should not be re-rounded (which is what the contrary rule assumes is
> > happening).
>
> Apparently the idea of even vs. odd rounding was to help prevent

"inflational"

> rounding --successive rounding pushing values upward. I think K&R

actually

> supported the idea. I'm not sure how I feel about it.

     Yes, that's exactly why it's there. But there are several reasons
(e.g. Benford's Law http://mathworld.wolfram.com/BenfordsLaw.html\) why
it doesn't even work for its intended purpose. The real answer is to
not re-round the data.

     As for developing strong feelings about it, all you need to do is
work downstream from someone who swears by it and try to do some valid
numerical analysis. The only person I ever knew with a worse lament was
a friend who worked downstream of an astronomy professor (who should
have been emeritusized years before) that routinely converted his images
to jpeg "to save disk space."

A real "star destroyer"... :-))

*chuckle*

    robert

···

On Fri, 2004-10-15 at 17:58, trans. (T. Onoma) wrote:
> On Friday 15 October 2004 07:23 pm, markus@reality.com wrote:

Do you think the 10000 makes it more convincing? :slight_smile:

Obviously! :slight_smile:

No, I wasn't fully paying attention to this thread, but did see the talk about alternating rounding. I didn't know if it applied to the sprintf case, so, just in case it was a pseudo-random result I was checking if perhaps it varied between 0/1.

Now, is this due to the Ruby version or rather the libC version (I tested
with cygwin)?

Dunno; I simply offer my data point for observation, and will let others figger out the discrepancy.

···

On Oct 18, 2004, at 7:19 AM, Robert Klemme wrote: