Re: [Haskell-cafe] Why 'round' does not just round numbers ?

2008-10-27 Thread Henning Thielemann


On Mon, 27 Oct 2008, L.Guo wrote:


I think this is unresonable. then try it in GHC 6.8.3.


Prelude round 3.5
4
Prelude round 2.5
2


Is there any explanation about that ?


It's the definition we learnt in school ...

I think one reason is that repeated rounding should not be worse than 
rounding in one go. Consider the rule 'use ceiling when the first removed 
digit is 5'. Then


0.45 - (round to one place) - 0.5 - (round to integer) - 1

but

0.45 - (round to integer) - 0


This is avoided by the school definition.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


RE: [Haskell-cafe] Why 'round' does not just round numbers ?

2008-10-27 Thread Mitchell, Neil
Hi,

That is a fairly standard implementation of rounding for financial
institutions. Consider

sum . map round

Over the list [3.5,2.5]

With rounding to the nearest even integer for 0.5's you get 6, otherwise
if you always round up you get 7. If you bias towards rounding up you
get a general upwards trend as numbers are rounded, which is bad, while
the even condition ensures that statistically it averages to the same
thing.

For more details see:
http://en.wikipedia.org/wiki/Rounding#Round-to-even_method

Thanks

Neil


 -Original Message-
 From: [EMAIL PROTECTED] 
 [mailto:[EMAIL PROTECTED] On Behalf Of L.Guo
 Sent: 27 October 2008 9:49 am
 To: MailList Haskell-Cafe
 Subject: [Haskell-cafe] Why 'round' does not just round numbers ?
 
 Hi all:
 
 
 I just read about definitions of Prelude [1], and noticing that.
 
 In 6.4.6 Coercions and Component Extraction, it discribes like this:
 
 
 round x returns the nearest integer to x, the even integer 
 if x is equidistant between two integers.
 
 
 I think this is unresonable. then try it in GHC 6.8.3.
 
 
 Prelude round 3.5
 4
 Prelude round 2.5
 2
 
 
 Is there any explanation about that ?
 
 
 [1] The Haskell 98 Report: Predefined Types and Classes
 http://haskell.org/onlinereport/basic.html
 
 Regards
 --
 L.Guo
 2008-10-27
 
 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe
 
 

==
Please access the attached hyperlink for an important electronic communications 
disclaimer: 

http://www.credit-suisse.com/legal/en/disclaimer_email_ib.html
==

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Why 'round' does not just round numbers ?

2008-10-27 Thread Janis Voigtlaender

Henning Thielemann wrote:


On Mon, 27 Oct 2008, L.Guo wrote:


I think this is unresonable. then try it in GHC 6.8.3.


Prelude round 3.5
4
Prelude round 2.5
2


Is there any explanation about that ?



It's the definition we learnt in school ...


Hmm, Henning, this is strange. The two of us went to the very same
school, but I know for a fact that I learnt 2.5 to round to 3, not 2 as
above. ;-)

I think one reason is that repeated rounding should not be worse than 
rounding in one go. Consider the rule 'use ceiling when the first 
removed digit is 5'. Then


0.45 - (round to one place) - 0.5 - (round to integer) - 1

but

0.45 - (round to integer) - 0


That is of course true (and was the topic of heated discussion with my
fourth grade math teacher), but does not explain 2.5 - 2.

Ciao, Janis.

--
Dr. Janis Voigtlaender
http://wwwtcs.inf.tu-dresden.de/~voigt/
mailto:[EMAIL PROTECTED]

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Why 'round' does not just round numbers ?

2008-10-27 Thread Henning Thielemann


On Mon, 27 Oct 2008, Janis Voigtlaender wrote:


Henning Thielemann wrote:


On Mon, 27 Oct 2008, L.Guo wrote:


I think this is unresonable. then try it in GHC 6.8.3.


Prelude round 3.5
4
Prelude round 2.5
2


Is there any explanation about that ?



It's the definition we learnt in school ...


Hmm, Henning, this is strange. The two of us went to the very same
school, but I know for a fact that I learnt 2.5 to round to 3, not 2 as
above. ;-)


I meant the school before GCG! :-]

So it seems to differ between schools and even teachers. (Much like the 
question whether zero should be counted as natural number or not.)



I think one reason is that repeated rounding should not be worse than 
rounding in one go. Consider the rule 'use ceiling when the first removed 
digit is 5'. Then


0.45 - (round to one place) - 0.5 - (round to integer) - 1

but

0.45 - (round to integer) - 0


That is of course true (and was the topic of heated discussion with my
fourth grade math teacher), but does not explain 2.5 - 2.


Because doing otherwise would be odd rounding. ;-)
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Why 'round' does not just round numbers ?

2008-10-27 Thread Felipe Lessa
(Janis, sorry for e-mailiing just for you on the first time.)

On Mon, Oct 27, 2008 at 8:15 AM, Janis Voigtlaender
[EMAIL PROTECTED] wrote:
 That is of course true (and was the topic of heated discussion with my
 fourth grade math teacher), but does not explain 2.5 - 2.

If you round to odd instead of round to even, then 4.5 rounds to 5,
which would may need rounding again and introduce double rounding
errors. For a simple example,

2.45 - 2.4 - 2
2.45 - 2.5 - 3

The Wikipedia article that was linked in this thread talks about this
problem as well.

-- 
Felipe.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Why 'round' does not just round numbers ?

2008-10-27 Thread Janis Voigtlaender

Felipe Lessa wrote:

On Mon, Oct 27, 2008 at 8:15 AM, Janis Voigtlaender
[EMAIL PROTECTED] wrote:


That is of course true (and was the topic of heated discussion with my
fourth grade math teacher), but does not explain 2.5 - 2.



If you round to odd instead of round to even, then 4.5 rounds to 5,


Well, of course I did not learn to round to odd. I learned to round .5
to above, but not to do repeated rounding.

So we would never have either of


2.45 - 2.4 - 2
2.45 - 2.5 - 3


as you suggest.

Instead, always in one go:

2.45 - 2

Because of the 4, whereas any digit following it would be ignored:

2.4x - x

In fact, by your explanation, you would have:

3.46 - 3.5 - 4

Which is most odd. 3.46 is definitely closer to 3 than to 4.

--
Dr. Janis Voigtlaender
http://wwwtcs.inf.tu-dresden.de/~voigt/
mailto:[EMAIL PROTECTED]

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Why 'round' does not just round numbers ?

2008-10-27 Thread Janis Voigtlaender

Janis Voigtlaender wrote:


2.4x - x


That's supposed to be 2.4x - 2, of course.

--
Dr. Janis Voigtlaender
http://wwwtcs.inf.tu-dresden.de/~voigt/
mailto:[EMAIL PROTECTED]
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Why 'round' does not just round numbers ?

2008-10-27 Thread Felipe Lessa
On Mon, Oct 27, 2008 at 8:30 AM, Janis Voigtlaender
[EMAIL PROTECTED] wrote:
 Well, of course I did not learn to round to odd. I learned to round .5
 to above, but not to do repeated rounding.

Nobody rounds in passes, of course =). I was talking about two
successive rounds.

 In fact, by your explanation, you would have:

 3.46 - 3.5 - 4

 Which is most odd. 3.46 is definitely closer to 3 than to 4.

If you're doing a double rounding, then this error will always happen.
Round to even avoids this error when the last digit was a 5, which is
good.

-- 
Felipe.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Why 'round' does not just round numbers ?

2008-10-27 Thread Janis Voigtlaender

Henning Thielemann wrote:
I think one reason is that repeated rounding should not be worse than 
rounding in one go. Consider the rule 'use ceiling when the first 
removed digit is 5'. Then


0.45 - (round to one place) - 0.5 - (round to integer) - 1


But repeated rounding *is* worse than rounding in one go, under any
reasonable scheme:

3.46 - 3.5 - 4

vs.

3.46 - 3

That was actually the debate with that teacher. Unbelievable as that
still is to me today, she advocated the 3.46 - 3.5 - 4 route...

And yes, Henning, you are right, we didn't yet share school in fourth
grade when rounding was taught.

Ciao, Janis.

--
Dr. Janis Voigtlaender
http://wwwtcs.inf.tu-dresden.de/~voigt/
mailto:[EMAIL PROTECTED]
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Why 'round' does not just round numbers ?

2008-10-27 Thread Janis Voigtlaender

Felipe Lessa wrote:

On Mon, Oct 27, 2008 at 8:30 AM, Janis Voigtlaender
[EMAIL PROTECTED] wrote:


Well, of course I did not learn to round to odd. I learned to round .5
to above, but not to do repeated rounding.



Nobody rounds in passes, of course =). 


Oh, Mrs. I forgot her name actually did ;-)

--
Dr. Janis Voigtlaender
http://wwwtcs.inf.tu-dresden.de/~voigt/
mailto:[EMAIL PROTECTED]

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Why 'round' does not just round numbers ?

2008-10-27 Thread Henning Thielemann


On Mon, 27 Oct 2008, Janis Voigtlaender wrote:


Henning Thielemann wrote:
I think one reason is that repeated rounding should not be worse than 
rounding in one go. Consider the rule 'use ceiling when the first removed 
digit is 5'. Then


0.45 - (round to one place) - 0.5 - (round to integer) - 1


But repeated rounding *is* worse than rounding in one go, under any
reasonable scheme:

3.46 - 3.5 - 4


With the rounding-to-even route this would be

3.46 - 3.4 - 3

so rounding in passes is no worse than rounding in one go for this 
example.



vs.

3.46 - 3

That was actually the debate with that teacher. Unbelievable as that
still is to me today, she advocated the 3.46 - 3.5 - 4 route ...


I also know a didact which tells teachers that 1 has no prime 
decomposition. Oh, I see, she may have copied that from Wikipedia:

   http://en.wikipedia.org/wiki/Prime_factorisation
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Why 'round' does not just round numbers ?

2008-10-27 Thread Ketil Malde
Janis Voigtlaender [EMAIL PROTECTED] writes:

 If you round to odd instead of round to even, then 4.5 rounds to 5,

 Well, of course I did not learn to round to odd. I learned to round .5
 to above, but not to do repeated rounding.

Since just about every floating point operation involves some sort of
loss of precision, repeated rounding is a fact of life.  Or use
rationals. 

-k
-- 
If I haven't seen further, it is by standing in the footprints of giants
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Why 'round' does not just round numbers ?

2008-10-27 Thread Daniel Fischer
Am Montag, 27. Oktober 2008 11:46 schrieb Henning Thielemann:
 On Mon, 27 Oct 2008, Janis Voigtlaender wrote:
  Henning Thielemann wrote:
  I think one reason is that repeated rounding should not be worse than
  rounding in one go. Consider the rule 'use ceiling when the first
  removed digit is 5'. Then
 
  0.45 - (round to one place) - 0.5 - (round to integer) - 1
 
  But repeated rounding *is* worse than rounding in one go, under any
  reasonable scheme:
 
  3.46 - 3.5 - 4

 With the rounding-to-even route this would be

 3.46 - 3.4 - 3

Wait, that cannot be. 6  5, so 3.46 - 3.5 even with banker's rounding.


 so rounding in passes is no worse than rounding in one go for this
 example.

Rounding in passes is bad per se, because there are pretty large intervals 
where that gives a different result from a direct rounding.

  vs.
 
  3.46 - 3
 
  That was actually the debate with that teacher. Unbelievable as that
  still is to me today, she advocated the 3.46 - 3.5 - 4 route ...

 I also know a didact which tells teachers that 1 has no prime
 decomposition. Oh, I see, she may have copied that from Wikipedia:
 http://en.wikipedia.org/wiki/Prime_factorisation

I can believe that makes sense to somebody who considers 0 an unnatural 
number, an empty product must be frightening for them.

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Why 'round' does not just round numbers ?

2008-10-27 Thread Henning Thielemann


On Mon, 27 Oct 2008, Daniel Fischer wrote:


Am Montag, 27. Oktober 2008 11:46 schrieb Henning Thielemann:

On Mon, 27 Oct 2008, Janis Voigtlaender wrote:

Henning Thielemann wrote:

I think one reason is that repeated rounding should not be worse than
rounding in one go. Consider the rule 'use ceiling when the first
removed digit is 5'. Then

0.45 - (round to one place) - 0.5 - (round to integer) - 1


But repeated rounding *is* worse than rounding in one go, under any
reasonable scheme:

3.46 - 3.5 - 4


With the rounding-to-even route this would be

3.46 - 3.4 - 3


Wait, that cannot be. 6  5, so 3.46 - 3.5 even with banker's rounding.


You are right, my oversight. So only the method helps, that the last digit 
is marked, whether it was generated by rounding up or down.

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Why 'round' does not just round numbers ?

2008-10-27 Thread Janis Voigtlaender

Ketil Malde wrote:

Janis Voigtlaender [EMAIL PROTECTED] writes:



If you round to odd instead of round to even, then 4.5 rounds to 5,




Well, of course I did not learn to round to odd. I learned to round .5
to above, but not to do repeated rounding.



Since just about every floating point operation involves some sort of
loss of precision, repeated rounding is a fact of life. 


Of course. But that was not the point of the discussion...

--
Dr. Janis Voigtlaender
http://wwwtcs.inf.tu-dresden.de/~voigt/
mailto:[EMAIL PROTECTED]
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Why 'round' does not just round numbers ?

2008-10-27 Thread Magnus Therning
2008/10/27 Janis Voigtlaender [EMAIL PROTECTED]:
 Janis Voigtlaender wrote:

 2.4x - x

 That's supposed to be 2.4x - 2, of course.

Ah, damn it.  I was hoping for a long discussion on just what math
would look like with rounding like that ;-)

/M

-- 
Magnus Therning(OpenPGP: 0xAB4DFBA4)
magnus@therning.org  Jabber: magnus@therning.org
http://therning.org/magnus identi.ca|twitter: magthe
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Why 'round' does not just round numbers ?

2008-10-27 Thread Lennart Augustsson
It is certainly what I learnt in school.  But that was another school.

On Mon, Oct 27, 2008 at 12:15 PM, Janis Voigtlaender
[EMAIL PROTECTED] wrote:
 Henning Thielemann wrote:

 On Mon, 27 Oct 2008, L.Guo wrote:

 I think this is unresonable. then try it in GHC 6.8.3.


 Prelude round 3.5
 4
 Prelude round 2.5
 2


 Is there any explanation about that ?


 It's the definition we learnt in school ...

 Hmm, Henning, this is strange. The two of us went to the very same
 school, but I know for a fact that I learnt 2.5 to round to 3, not 2 as
 above. ;-)

 I think one reason is that repeated rounding should not be worse than
 rounding in one go. Consider the rule 'use ceiling when the first removed
 digit is 5'. Then

 0.45 - (round to one place) - 0.5 - (round to integer) - 1

 but

 0.45 - (round to integer) - 0

 That is of course true (and was the topic of heated discussion with my
 fourth grade math teacher), but does not explain 2.5 - 2.

 Ciao, Janis.

 --
 Dr. Janis Voigtlaender
 http://wwwtcs.inf.tu-dresden.de/~voigt/
 mailto:[EMAIL PROTECTED]

 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Why 'round' does not just round numbers ?

2008-10-27 Thread Janis Voigtlaender

Lennart Augustsson wrote:

It is certainly what I learnt in school.  But that was another school.


Hmm, on reflection, taking Neil's explanation into account and the fact
that this rounding mode was referred to as banker's rounding, the
point may be that it was not only another school, but another world
(politically, pre-'89 ;-).

--
Dr. Janis Voigtlaender
http://wwwtcs.inf.tu-dresden.de/~voigt/
mailto:[EMAIL PROTECTED]

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Why 'round' does not just round numbers ?

2008-10-27 Thread Lennart Augustsson
I can't remember the method being called anything.
It was just what we were being taught.  With the obvious explanation
that .5 is right in the middle so always going one way would introduce
a bias.  This was circa 1969.


On Mon, Oct 27, 2008 at 3:30 PM, Janis Voigtlaender
[EMAIL PROTECTED] wrote:
 Lennart Augustsson wrote:

 It is certainly what I learnt in school.  But that was another school.

 Hmm, on reflection, taking Neil's explanation into account and the fact
 that this rounding mode was referred to as banker's rounding, the
 point may be that it was not only another school, but another world
 (politically, pre-'89 ;-).

 --
 Dr. Janis Voigtlaender
 http://wwwtcs.inf.tu-dresden.de/~voigt/
 mailto:[EMAIL PROTECTED]

 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Why 'round' does not just round numbers ?

2008-10-27 Thread Janis Voigtlaender

Lennart Augustsson wrote:

I can't remember the method being called anything.
It was just what we were being taught.  With the obvious explanation
that .5 is right in the middle so always going one way would introduce
a bias.  This was circa 1969.


Well, I wasn't serious about the political explanation.

And yes, the avoiding bias explanation makes sense, but not the this
way of rounding makes repeated rounding safe explanation.

--
Dr. Janis Voigtlaender
http://wwwtcs.inf.tu-dresden.de/~voigt/
mailto:[EMAIL PROTECTED]
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Why 'round' does not just round numbers ?

2008-10-27 Thread Magnus Therning
On Mon, Oct 27, 2008 at 9:48 AM, L.Guo [EMAIL PROTECTED] wrote:
 Hi all:


 I just read about definitions of Prelude [1], and noticing that.

 In 6.4.6 Coercions and Component Extraction, it discribes like this:


 round x returns the nearest integer to x, the even integer if x is 
 equidistant between two integers.


 I think this is unresonable. then try it in GHC 6.8.3.


 Prelude round 3.5
 4
 Prelude round 2.5
 2


 Is there any explanation about that ?


 [1] The Haskell 98 Report: Predefined Types and Classes
http://haskell.org/onlinereport/basic.html

This behaviour is not what I expect after reading the description at
http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:round
.  Given that this behaviour has caused a bit of confusion I think a
change to the documention might be in order.

/M

-- 
Magnus Therning(OpenPGP: 0xAB4DFBA4)
magnus@therning.org  Jabber: magnus@therning.org
http://therning.org/magnus identi.ca|twitter: magthe
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Why 'round' does not just round numbers ?

2008-10-27 Thread Magnus Therning
On Mon, Oct 27, 2008 at 1:37 PM, Lennart Augustsson
[EMAIL PROTECTED] wrote:
 I can't remember the method being called anything.
 It was just what we were being taught.  With the obvious explanation
 that .5 is right in the middle so always going one way would introduce
 a bias.  This was circa 1969.

By the time I was in the Swedish school system that seems to have
changed.  I had never heard of this method of rounding before reading
this thread :-)

/M

-- 
Magnus Therning(OpenPGP: 0xAB4DFBA4)
magnus@therning.org  Jabber: magnus@therning.org
http://therning.org/magnus identi.ca|twitter: magthe
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Why 'round' does not just round numbers ?

2008-10-27 Thread Thomas Davie

[1] The Haskell 98 Report: Predefined Types and Classes
  http://haskell.org/onlinereport/basic.html


This behaviour is not what I expect after reading the description at
http://haskell.org/ghc/docs/latest/html/libraries/base/ 
Prelude.html#v:round

.  Given that this behaviour has caused a bit of confusion I think a
change to the documention might be in order.


Given that the documentation says round x returns the nearest integer  
to x, I think pretty much any behavior can be expected -- there's no  
single integer nearest to 2.5.  The documentation certainly needs  
updated though.


Bob___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Why 'round' does not just round numbers ?

2008-10-27 Thread Brandon S. Allbery KF8NH

On 2008 Oct 27, at 6:00, Henning Thielemann wrote:

On Mon, 27 Oct 2008, L.Guo wrote:

I think this is unresonable. then try it in GHC 6.8.3.

Prelude round 3.5
4
Prelude round 2.5
2

Is there any explanation about that ?


It's the definition we learnt in school ...


Maybe you did; I learned 5/9 rounding.

--
brandon s. allbery [solaris,freebsd,perl,pugs,haskell] [EMAIL PROTECTED]
system administrator [openafs,heimdal,too many hats] [EMAIL PROTECTED]
electrical and computer engineering, carnegie mellon universityKF8NH


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Why 'round' does not just round numbers ?

2008-10-27 Thread Ketil Malde
Janis Voigtlaender [EMAIL PROTECTED] writes:

 Since just about every floating point operation involves some sort of
 loss of precision, repeated rounding is a fact of life. 

 Of course. But that was not the point of the discussion...

Well, allow me to contribute to keeping the discussion on topic by
stating that when I was in school, I was taught to round up.  Now if
you will excuse a momentary digression:

The point *I* wanted to make is that we have two qualitatively different
rounding modes: round up on 0.5, or round to even (or randomly, or
alternating), and they make sense in different contexts.

Doing computations with fixed precision, you keep losing precision,
and rounding bias accumulates - thus the need to use some non-biased
rounding. 

Doing (small scale) calculations on paper, you can avoid repeated
rounding, and only round the result.  In which case rounding up is
okay, you don't introduce the amount of bias as with repeated
rounding.  And if your input happens to be truncated, rounding up
becomes the right thing to do. 

Of course, Haskell should discard the rather tasteless IEEE754 crud,
and do its calculations on infinite streams of digits.  Then, rounding
upwards after 'take'ing a sufficient amount of decimals will be the
right thing to do.

-k
-- 
If I haven't seen further, it is by standing in the footprints of giants
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Why 'round' does not just round numbers ?

2008-10-27 Thread Janis Voigtlaender

Ketil Malde wrote:

Janis Voigtlaender [EMAIL PROTECTED] writes:



Since just about every floating point operation involves some sort of
loss of precision, repeated rounding is a fact of life. 




Of course. But that was not the point of the discussion...



Well, allow me to contribute to keeping the discussion on topic by
stating that when I was in school, I was taught to round up.  Now if
you will excuse a momentary digression:

The point *I* wanted to make is that we have two qualitatively different
rounding modes: round up on 0.5, or round to even (or randomly, or
alternating), and they make sense in different contexts.

Doing computations with fixed precision, you keep losing precision,
and rounding bias accumulates - thus the need to use some non-biased
rounding. 


Doing (small scale) calculations on paper, you can avoid repeated
rounding, and only round the result.  In which case rounding up is
okay, you don't introduce the amount of bias as with repeated
rounding.  And if your input happens to be truncated, rounding up
becomes the right thing to do. 


Of course, Haskell should discard the rather tasteless IEEE754 crud,
and do its calculations on infinite streams of digits.  Then, rounding
upwards after 'take'ing a sufficient amount of decimals will be the
right thing to do.


Yes, that all makes sense.

And I did not intend to cut you short.

--
Dr. Janis Voigtlaender
http://wwwtcs.inf.tu-dresden.de/~voigt/
mailto:[EMAIL PROTECTED]
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Why 'round' does not just round numbers ?

2008-10-27 Thread Jules Bean

This behaviour is not what I expect after reading the description at
http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:round
.  Given that this behaviour has caused a bit of confusion I think a
change to the documention might be in order.


The authority here is the report which says

round x returns the nearest integer to x, the even integer if x is 
equidistant between two integers.


However I agree the haddock ought to mirror the report.

Jules
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Why 'round' does not just round numbers ?

2008-10-27 Thread Martijn van Steenbergen

Ketil Malde wrote:

Of course, Haskell should discard the rather tasteless IEEE754 crud,
and do its calculations on infinite streams of digits.  Then, rounding
upwards after 'take'ing a sufficient amount of decimals will be the
right thing to do.


Except arbitrary-precision real arithmetic is not as trivial as you make 
it sound, and using an infinite stream of digits is not sufficient by 
itself.


See also: http://www.haskell.org/haskellwiki/Exact_real_arithmetic

Martijn.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Why 'round' does not just round numbers ?

2008-10-27 Thread John A. De Goes


It's well known from numerical analysis that you can achieve the best  
general behavior by rounding to even in half the cases, and rounding  
to odd in half the cases. It's usually deterministic by looking at  
the digit to the right of the round point.


Regards,

John A. De Goes
N-BRAIN, Inc.
http://www.n-brain.net
[n minds are better than n-1]

On Oct 27, 2008, at 10:59 AM, Jules Bean wrote:


This behaviour is not what I expect after reading the description at
http://haskell.org/ghc/docs/latest/html/libraries/base/ 
Prelude.html#v:round

.  Given that this behaviour has caused a bit of confusion I think a
change to the documention might be in order.


The authority here is the report which says

round x returns the nearest integer to x, the even integer if x is  
equidistant between two integers.


However I agree the haddock ought to mirror the report.

Jules
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Why 'round' does not just round numbers ?

2008-10-27 Thread Peter Gavin

L.Guo wrote:

Hi all:


I just read about definitions of Prelude [1], and noticing that.

In 6.4.6 Coercions and Component Extraction, it discribes like this:


round x returns the nearest integer to x, the even integer if x is equidistant 
between two integers.


I think this is unresonable. then try it in GHC 6.8.3.


Prelude round 3.5
4
Prelude round 2.5
2


Is there any explanation about that ?



This is the same exact behavior found in the FPU of most processors, except that 
you usually don't notice because the rounding occurs between very small values.


The reason for doing it this way is that e.g. 2.5 is exactly between 2 and 3, 
and rounding *up* every time would cause an uneven bias toward 3.  To counteract 
that effect, rounding to the nearest even integer is used, which causes the half 
of the x.5 values to round up, and the other half to round down.


Pete
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Why 'round' does not just round numbers ?

2008-10-27 Thread Henning Thielemann


On Mon, 27 Oct 2008, Ketil Malde wrote:


Of course, Haskell should discard the rather tasteless IEEE754 crud,
and do its calculations on infinite streams of digits.  Then, rounding
upwards after 'take'ing a sufficient amount of decimals will be the
right thing to do.


When I implemented just this in
  http://darcs.haskell.org/numericprelude/src/Number/Positional.hs
 I used redundant sets of digits. In a base-n number I use the digits 1-n 
to n-1. That is, the truncated number abc.def actually represents the 
interval [abc.def-0.001, abd.def+0.001]. This representation is not unique 
(more non-unique than standard base-n representation), but testing numbers 
with infinitely many places for equality is not possible anyway. So (==) 
is obsolete, just like for floating point numbers. The advantage is that 
carries are reduced considerably. It allows to compute (sqrt 2)^2, which 
is not possible in standard representation, because one can never decide 
whether the result is 2.0... or 1.9 (Pentium's division 
algorithm also uses digits -1, 0, 1 (we could call them mone (minus one), 
none, one) for reduced carries.)
 In this redundant representation one could simply round by truncation, 
the result depends on the particular sequence of digits used to represent 
a number, and the rounding would not become worse by repeated rounding 
(also because it is already worse than the Haskel 98 rounding since the 
error with respect to the last digit is not only 0.5 but up to 1 :-).

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Why 'round' does not just round numbers ?

2008-10-27 Thread Henning Thielemann


On Mon, 27 Oct 2008, Janis Voigtlaender wrote:


Lennart Augustsson wrote:

I can't remember the method being called anything.
It was just what we were being taught.  With the obvious explanation
that .5 is right in the middle so always going one way would introduce
a bias.  This was circa 1969.


Well, I wasn't serious about the political explanation.

And yes, the avoiding bias explanation makes sense, but not the this
way of rounding makes repeated rounding safe explanation.


In measured data the .5-case should be very rare - a null set? However I 
assume that .5 happens more often in practice - because of prior rounding, 
which was shown to be bad practice in this thread. :-)

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


RE: [Haskell-cafe] Why 'round' does not just round numbers ?

2008-10-27 Thread ajb

G'day all.

Quoting Mitchell, Neil [EMAIL PROTECTED]:


With rounding to the nearest even integer for 0.5's you get 6, otherwise
if you always round up you get 7. If you bias towards rounding up you
get a general upwards trend as numbers are rounded, which is bad, while
the even condition ensures that statistically it averages to the same
thing.


There are also many numeric algorithms for which the rounding pattern
in the least-significant digit is somewhat systematic.  A simple example
might be this:

x - round(x + 0.5)
x - round(x - 0.5)
x - round(x + 0.5)
x - round(x - 0.5)
... etc

If you try this procedure starting with, say, x = 1.5, you can see a
clear difference between round-up and round-to-even.  With round-up,
the rounding error is keeps increasing as the procedure continues.  With
round-to-even, the error is bounded.

Cheers,
Andrew Bromage
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


RE: [Haskell-cafe] Why 'round' does not just round numbers ?

2008-10-27 Thread ajb

G'day all.

Henning Thielemann suggested:

In measured data the .5-case should be very rare - a null set?  
However I assume that .5 happens more often in practice - because of  
prior rounding, which was shown to be bad practice in this thread.


The usual case in floating point is usually not 0.5, but some power of
0.5 just beyond the precision of the mantissa.  And this is actually
one of the most common cases in rounding, due to the fact that we use
binary arithmetic.

Cheers,
Andrew Bromage
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Why 'round' does not just round numbers ?

2008-10-27 Thread Richard O'Keefe


On 27 Oct 2008, at 11:00 pm, Henning Thielemann wrote:



On Mon, 27 Oct 2008, L.Guo wrote:


I think this is unresonable. then try it in GHC 6.8.3.


Prelude round 3.5
4
Prelude round 2.5
2


Is there any explanation about that ?


It's the definition we learnt in school ...


Check

http://en.wikipedia.org/wiki/Rounding

where you will find that the version of rounding
generally taught in elementary schools is the
one used in Pascal, namely

round(X) = truncate(X + 0.5*sign(X))



(The Wikipedia entry says that Pascal uses a different
algorithm, but ISO 7185 says
If x is positive or zero, round(x) shall be equivalent to trunc(x 
+0.5); otherwise, round(x) shall be equivalent to trunc(x-0.5).)


I think one reason is that repeated rounding should not be worse  
than rounding in one go.


That would be nice, but while round-to-even _reduces_ the
harm from double rounding, it does not eliminate it.

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Why 'round' does not just round numbers ?

2008-10-27 Thread Richard O'Keefe

On 28 Oct 2008, at 11:11 am, Henning Thielemann wrote:
In measured data the .5-case should be very rare - a null set?  
However I assume that .5 happens more often in practice - because of  
prior rounding,


Think about money.
When I was a child, farthings (1/4 of a penny) had just been
dropped.  (By now, our smallest coin is 10c, formerly a shilling,
so in ~ 50 years the value of the smallest coin has eroded by a
factor of 48.) Ha'pennies (1/2 of a penny) were still around.
If you were adding up a sum of money, sums ending with 1/2 were
actually quite common.  When the ha'penny went the way of the
farthing, one still had to round sums in pounds shillings and
pence to sums in pounds and shillings, and sixpence (0.5 of a
shilling) was not an unlikely amount.

Now that the smallest coin is 10c, supermarkets still price
things in multiples of 1c, so in order to give change, they
have to round to a multuple of 10c.  Sums that end with 5c
are not at all unusual.

Considering that the point of the thread is what should we
expect rounding to do, it may be of interest that a couple of
years after the death of the 5c piece, supermarkets *still*
display their rounding rule at the cash registers.

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe