Re: [R] - round() strange behaviour

2007-08-06 Thread Jim Lemon
(Ted Harding) wrote:
 ...
 Thus always up or always down means that the difference between
 rounded numbers is always the same as between the unrounded numbers
 which for other methods the differences can differ by 2.0.
 This may or may not matter, but it is something to think about
 when choosing a rounding method.
 
Ah, but Ted, this is why, as I remarked in a private message to the 
original poster, we were also taught never to round until the final 
result. Had I not had a rather extended debate with an engineer who 
apparently thought that one could get away with successive rounding, I 
would not be so sensitive on the issue.

Jim

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] - round() strange behaviour

2007-08-06 Thread John Logsdon
In R you may be tempted to use rounding to index something in an array.  Hence 
my concern and suggestion that there be another argument to round() (and 
signif()) that explicitly sets the rounding mechanism.  

In Fortran IDNINT does as I would expect.  You could do floor(x+0.5) but many 
people, myself included before this conversation, will unwittingly round and 
it will almost always work but the exception is something that may even be 
difficult to reproduce where random numbers are involved without setting the 
seed.  

In this context you cannot leave rounding until the end of course!  Clearly in 
an arithmetic context, rounding should always be left to the end but I don't 
think that is to avoid rounding-to-even but just to avoid unnecessary loss of 
precision in the calculation.  Ask most engineers - and even physicists - and 
I suspect they would not be aware of this debate even though it dates from 
the 1940s.

On Monday 06 August 2007 10:31:22 Jim Lemon wrote:
 (Ted Harding) wrote:
  ...
  Thus always up or always down means that the difference between
  rounded numbers is always the same as between the unrounded numbers
  which for other methods the differences can differ by 2.0.
  This may or may not matter, but it is something to think about
  when choosing a rounding method.

 Ah, but Ted, this is why, as I remarked in a private message to the
 original poster, we were also taught never to round until the final
 result. Had I not had a rather extended debate with an engineer who
 apparently thought that one could get away with successive rounding, I
 would not be so sensitive on the issue.

 Jim

 __
 R-help@stat.math.ethz.ch mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide
 http://www.R-project.org/posting-guide.html and provide commented, minimal,
 self-contained, reproducible code.



-- 
Best wishes

John

John Logsdon   Try to make things as simple
Quantex Research Ltd, Manchester UK as possible but not simpler
[EMAIL PROTECTED]  [EMAIL PROTECTED]
+44(0)161 445 4951/G:+44(0)7717758675   www.quantex-research.com

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] - round() strange behaviour

2007-08-06 Thread Duncan Murdoch
On 06/08/2007 5:46 AM, John Logsdon wrote:
 In R you may be tempted to use rounding to index something in an array.  
 Hence 
 my concern and suggestion that there be another argument to round() (and 
 signif()) that explicitly sets the rounding mechanism.  
 
 In Fortran IDNINT does as I would expect.  You could do floor(x+0.5) but many 
 people, myself included before this conversation, will unwittingly round and 
 it will almost always work but the exception is something that may even be 
 difficult to reproduce where random numbers are involved without setting the 
 seed.  

As you mention, where you want to round 0.5 upwards, you can use the 
function

roundup - function(x, digits=0) {
   factor - 10^digits
   trunc(x*factor + 0.5)/factor
}

But beware of floating point representations:  most decimal numbers 
ending in 5 can't be represented exactly, so you'll see oddities like this:

  roundup(2.45, 1)
[1] 2.5
  roundup(2.4 + 0.05, 1)
[1] 2.4

which is partially explained by this:

  2.45 - (2.4 + 0.05)
[1] 4.440892e-16

I suspect the same sort of thing would happen in Fortran's IDNINT though 
it might be harder to see, because most Fortran compilers do fairly 
substantial optimizations, and it might be that they actually store 2.4 
+ 0.05 and 2.45 as the same constant.  So I don't think round() leaves 
any new traps for the unwary:  if x = 0.5 is the result of a 
calculation, it might actually be stored as 0.499 and round(x) 
should return 0 even using roundup().

Duncan Murdoch

 In this context you cannot leave rounding until the end of course!  Clearly 
 in 
 an arithmetic context, rounding should always be left to the end but I don't 
 think that is to avoid rounding-to-even but just to avoid unnecessary loss of 
 precision in the calculation.  Ask most engineers - and even physicists - and 
 I suspect they would not be aware of this debate even though it dates from 
 the 1940s.
 
 On Monday 06 August 2007 10:31:22 Jim Lemon wrote:
 (Ted Harding) wrote:
 ...
 Thus always up or always down means that the difference between
 rounded numbers is always the same as between the unrounded numbers
 which for other methods the differences can differ by 2.0.
 This may or may not matter, but it is something to think about
 when choosing a rounding method.
 Ah, but Ted, this is why, as I remarked in a private message to the
 original poster, we were also taught never to round until the final
 result. Had I not had a rather extended debate with an engineer who
 apparently thought that one could get away with successive rounding, I
 would not be so sensitive on the issue.

 Jim

 __
 R-help@stat.math.ethz.ch mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide
 http://www.R-project.org/posting-guide.html and provide commented, minimal,
 self-contained, reproducible code.
 
 


__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] - round() strange behaviour

2007-08-05 Thread Mark Wardle
I use round() to prepare numbers for display in Sweave documents.
While I had appreciated the round to even issue before, I had not
given it much thought when automatically writing my output
functions/scripts/Sexprs...

On 04/08/07, Ted Harding [EMAIL PROTECTED] wrote:
 On 04-Aug-07 21:57:28, John Logsdon wrote:
  I must admit I had never realised this so thanks to Monica for
  raising it.
 
  Round-to-even is used for statistical reasons and there is some
  point as it obviously reduces bias. But why should the decision
  be to round to the nearest even number?  rounding to the nearest
  odd number would be equally valid.  (signif(x,0) is the same as
  round(). )

 A fair point! But see below.

  There is a debate and sometimes you need symmetric rounding.
  Perhaps there should be an option in round that defaults to
  round-to-even for compatibility but includes the various other
  rounding approaches as seen for example in
  http://en.wikipedia.org/wiki/Rounding

 As wikipedia says: Round-to-even is used rather than round-to-odd
 as the latter rule would prevent rounding to a result of zero.

 And there's also stochastic rounding -- toss a penny. But you wouldn't
 get the same answer next time.

 And I can recall -- man years ago -- being in an environment where
 the practice was to round alternately up and down. (These were
 engineers).

 John's comparisons between FORTRAN, octave and (implicitly) R
 are interesting.

 As a general reflection: any method of rounding involves throwing
 away some of the information in the data, and this will have
 consequences. So the question is: what consequences are you
 happiest with?

 One consequence of rounding to nearest even (or nearest odd) is
 that this can give the worst possible outcome in terms of

 (rounded X - rounded Y) compared with (unrounded X - unrounded Y).

 For example, rounding to even integer X =1.5 -- 2.0 and
 Y = 0.5 -- 0.0 gives

   X - Y = 1.0 , rounded X - rounded Y = 2.0

 whereas the difference is 1.0 if you always round up, or always down,
 and this is also exactly the difference between the unrounded values..

 Rounding to nearest odd would give X = 1.5 -- 1.0, Y = 0.5 -- 1.0
 thus difference 0.0 in this case, but again difference 2.0 for
 X = 2.5 -- 3.0, Y = 1.5 -- 1.0

 And alternate rounding would give either 2.0 or 0.0 depending
 on the phase of X and Y.

 Thus always up or always down means that the difference between
 rounded numbers is always the same as between the unrounded numbers
 which for other methods the differences can differ by 2.0.
 This may or may not matter, but it is something to think about
 when choosing a rounding method.

  [...]

 Best wishes to all,
 Ted.

 
 E-Mail: (Ted Harding) [EMAIL PROTECTED]
 Fax-to-email: +44 (0)870 094 0861
 Date: 04-Aug-07   Time: 23:53:50
 -- XFMail --

 __
 R-help@stat.math.ethz.ch mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.


 __
 This email has been scanned by the MessageLabs Email Security System.
 For more information please visit http://www.messagelabs.com/email
 __



-- 
Dr. Mark Wardle
Clinical research fellow and specialist registrar, Neurology
Cardiff, UK

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] - round() strange behaviour

2007-08-04 Thread John Logsdon
I must admit I had never realised this so thanks to Monica for raising it.  

Round-to-even is used for statistical reasons and there is some point as it 
obviously reduces bias.  But why should the decision  be to round to the 
nearest even number?  rounding to the nearest odd number would be equally 
valid.  (signif(x,0) is the same as round(). )

There is a debate and sometimes you need symmetric rounding.  Perhaps there 
should be an option in round that defaults to round-to-even for compatibility 
but includes the various other rounding approaches as seen for example in 
http://en.wikipedia.org/wiki/Rounding

BTW with Fortran the 'nearest integer' approach is used:

  REAL*8 TEST
10CONTINUE
 READ(*,*)TEST
 WRITE(*,*)TEST,IDNINT(TEST)
  GOTO 10
  END

Compiling this under g77 and gfortran gives the result:
~ $ g77 idnint.f -o idnint
~ $ ./idnint
1.5
  1.5 2
2.5
  2.5 3

~ $ gfortran idnint.f -o idnint
~ $ ./idnint
2.5
   2.503
1.5
   1.502

Octave:

octave:1 round(1.5)
ans = 2
octave:2 round(2.5)
ans = 3

On Thursday 02 August 2007 21:46:36 Monica Pisica wrote:
 Hi again,

 Mea culpa for not reading help pages before hand - but one would not go
 there when the function syntax is obvious and known  besides other
 programming languages (for example IDL) with same function do not round to
 the next even number  so i do get 3 for round(2.5), and i didn't
 realize that it is normal for R to give a 2 instead of 3. Date: Thu, 2
 Aug 2007 22:38:49 +0200 From: [EMAIL PROTECTED] To:
 [EMAIL PROTECTED] CC: r-help@stat.math.ethz.ch Subject: Re: [R] -
 round() strange behaviour  On Thu, 2 Aug 2007, Monica Pisica wrote:  
  Hi,   I am getting some strange results using round - it seems that
 it depends if the number before the decimal point is odd or even   
 For example:round(1.5)[1] 2 round(2.5)[1] 2  While i would
 expect that round(2.5) be 3 and not 2.   Do you have any explanation
 for that?  Yes: you obviously did not read the man page! Please do. Z 
  I really appreciate your input,   Monica   
 _ 
 [[trailing spam removed]]   [[alternative HTML version deleted]]  
 __  R-help@stat.math.ethz.ch
 mailing list  https://stat.ethz.ch/mailman/listinfo/r-help  PLEASE do
 read the posting guide http://www.R-project.org/posting-guide.html  and
 provide commented, minimal, self-contained, reproducible code.  
 _
 Messenger Caf� � open for fun 24/7. Hot games, cool activities served
 daily. Visit now.

   [[alternative HTML version deleted]]



-- 
Best wishes

John

John Logsdon   Try to make things as simple
Quantex Research Ltd, Manchester UK as possible but not simpler
[EMAIL PROTECTED]  [EMAIL PROTECTED]
+44(0)161 445 4951/G:+44(0)7717758675   www.quantex-research.com

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] - round() strange behaviour

2007-08-04 Thread Ted Harding
On 04-Aug-07 21:57:28, John Logsdon wrote:
 I must admit I had never realised this so thanks to Monica for
 raising it.  
 
 Round-to-even is used for statistical reasons and there is some
 point as it obviously reduces bias. But why should the decision
 be to round to the nearest even number?  rounding to the nearest
 odd number would be equally valid.  (signif(x,0) is the same as
 round(). )

A fair point! But see below.

 There is a debate and sometimes you need symmetric rounding.
 Perhaps there should be an option in round that defaults to
 round-to-even for compatibility but includes the various other
 rounding approaches as seen for example in 
 http://en.wikipedia.org/wiki/Rounding

As wikipedia says: Round-to-even is used rather than round-to-odd
as the latter rule would prevent rounding to a result of zero.

And there's also stochastic rounding -- toss a penny. But you wouldn't
get the same answer next time.

And I can recall -- man years ago -- being in an environment where
the practice was to round alternately up and down. (These were
engineers).

John's comparisons between FORTRAN, octave and (implicitly) R
are interesting.

As a general reflection: any method of rounding involves throwing
away some of the information in the data, and this will have
consequences. So the question is: what consequences are you
happiest with?

One consequence of rounding to nearest even (or nearest odd) is
that this can give the worst possible outcome in terms of

(rounded X - rounded Y) compared with (unrounded X - unrounded Y).

For example, rounding to even integer X =1.5 -- 2.0 and
Y = 0.5 -- 0.0 gives

  X - Y = 1.0 , rounded X - rounded Y = 2.0

whereas the difference is 1.0 if you always round up, or always down,
and this is also exactly the difference between the unrounded values..

Rounding to nearest odd would give X = 1.5 -- 1.0, Y = 0.5 -- 1.0
thus difference 0.0 in this case, but again difference 2.0 for
X = 2.5 -- 3.0, Y = 1.5 -- 1.0

And alternate rounding would give either 2.0 or 0.0 depending
on the phase of X and Y.

Thus always up or always down means that the difference between
rounded numbers is always the same as between the unrounded numbers
which for other methods the differences can differ by 2.0.
This may or may not matter, but it is something to think about
when choosing a rounding method.

 [...]

Best wishes to all,
Ted.


E-Mail: (Ted Harding) [EMAIL PROTECTED]
Fax-to-email: +44 (0)870 094 0861
Date: 04-Aug-07   Time: 23:53:50
-- XFMail --

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] - round() strange behaviour

2007-08-02 Thread Monica Pisica

Hi,
 
I am getting some strange results using round - it seems that it depends if the 
number before the decimal point is odd or even 
 
For example:
 
 round(1.5)[1] 2 round(2.5)[1] 2
While i would expect that round(2.5) be 3 and not 2.
 
Do you have any explanation for that?
 
I really appreciate your input,
 
Monica
 
 
_
[[trailing spam removed]]

[[alternative HTML version deleted]]

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] - round() strange behaviour

2007-08-02 Thread Peter Dalgaard
Monica Pisica wrote:
 Hi,
  
 I am getting some strange results using round - it seems that it depends if 
 the number before the decimal point is odd or even 
  
 For example:
  
   
 round(1.5)[1] 2 round(2.5)[1] 2
 
 While i would expect that round(2.5) be 3 and not 2.
  
 Do you have any explanation for that?
  
   
http://www.google.com/search?q=round+to+even;

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] - round() strange behaviour

2007-08-02 Thread Thomas Lumley
On Thu, 2 Aug 2007, Monica Pisica wrote:


 Hi,

 I am getting some strange results using round - it seems that it depends if 
 the number before the decimal point is odd or even 

Yes. This is explained in the help page for round().

   -thomas

Thomas Lumley   Assoc. Professor, Biostatistics
[EMAIL PROTECTED]   University of Washington, Seattle

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] - round() strange behaviour

2007-08-02 Thread JRG
On 2 Aug 2007 at 20:16, Monica Pisica wrote:

 
 Hi,
  
 I am getting some strange results using round - it seems that it depends if 
 the number before the decimal point is odd or even 
  
 For example:
  
  round(1.5)[1] 2 round(2.5)[1] 2
 While i would expect that round(2.5) be 3 and not 2.
  
 Do you have any explanation for that?
  

I wouldn't call that strange behaviour; I'd call that correct.

---JRG


 I really appreciate your input,
  
 Monica
  
  
 _
 [[trailing spam removed]]
 
   [[alternative HTML version deleted]]
 
 __
 R-help@stat.math.ethz.ch mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.


John R. Gleason
Associate Professor

Syracuse University
430 Huntington Hall  Voice:   315-443-3107
Syracuse, NY 13244-2340  USA FAX: 315-443-4085

PGP public key at keyservers

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] - round() strange behaviour

2007-08-02 Thread Monica Pisica

Hi again,
 
Mea culpa for not reading help pages before hand - but one would not go there 
when the function syntax is obvious and known  besides other programming 
languages (for example IDL) with same function do not round to the next even 
number  so i do get 3 for round(2.5), and i didn't realize that it is 
normal for R to give a 2 instead of 3. Date: Thu, 2 Aug 2007 22:38:49 +0200 
From: [EMAIL PROTECTED] To: [EMAIL PROTECTED] CC: r-help@stat.math.ethz.ch 
Subject: Re: [R] - round() strange behaviour  On Thu, 2 Aug 2007, Monica 
Pisica wrote:Hi,   I am getting some strange results using round - 
it seems that it depends if the number before the decimal point is odd or even 
   For example:round(1.5)[1] 2 round(2.5)[1] 2  While i 
would expect that round(2.5) be 3 and not 2.   Do you have any explanation 
for that?  Yes: you obviously did not read the man page! Please do. Z   I 
really appreciate your input,   Monica
_  [[trailing 
spam removed]]   [[alternative HTML version deleted]]   
__  R-help@stat.math.ethz.ch 
mailing list  https://stat.ethz.ch/mailman/listinfo/r-help  PLEASE do read 
the posting guide http://www.R-project.org/posting-guide.html  and provide 
commented, minimal, self-contained, reproducible code.   
_
Messenger Café — open for fun 24/7. Hot games, cool activities served daily. 
Visit now.

[[alternative HTML version deleted]]

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] - round() strange behaviour

2007-08-02 Thread Achim Zeileis
On Thu, 2 Aug 2007, Monica Pisica wrote:


 Hi,

 I am getting some strange results using round - it seems that it depends if 
 the number before the decimal point is odd or even 

 For example:

  round(1.5)[1] 2 round(2.5)[1] 2
 While i would expect that round(2.5) be 3 and not 2.

 Do you have any explanation for that?

Yes: you obviously did not read the man page! Please do.
Z

 I really appreciate your input,

 Monica


 _
 [[trailing spam removed]]

   [[alternative HTML version deleted]]

 __
 R-help@stat.math.ethz.ch mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.



__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] - round() strange behaviour

2007-08-02 Thread David C. Howell
That is what it is supposed to do.

 From ?round
we get
Note that for rounding off a 5, the IEC 60559 standard is expected to 
be used, “go to the even digit”. Therefore round(0.5) is 0 and 
round(-1.5) is -2. However, this is dependent on OS services and on 
representation error (since e.g. 0.15 is not represented exactly, the 
rounding rule applies to the represented number and not to the printed 
number, and so round(0.15, 1) could be either 0.1 or 0.2). 



Monica Pisica wrote:
 Hi,
  
 I am getting some strange results using round - it seems that it depends if 
 the number before the decimal point is odd or even 
  
 For example:
  
 round(1.5)[1] 2 round(2.5)[1] 2
 While i would expect that round(2.5) be 3 and not 2.
  
 Do you have any explanation for that?
  
 I really appreciate your input,
  
 Monica
  
  
 _
 [[trailing spam removed]]
 
   [[alternative HTML version deleted]]
 
 __
 R-help@stat.math.ethz.ch mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.
 

-- 
David C. Howell
PO Box 770059
627 Meadowbrook Circle
Steamboat Springs, CO
80477

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.