foldr1 min [(maxBound::Int)%1,1 % 2]

Hi How come
foldr1 min [(maxBound::Int) % 1,1 % 2] 2147483647 % 1
but
foldr1 min [2147483647 % 1,1 % 2] 1 % 2
Why??? /Bo Herlin

On Wed, 13 Apr 2005, Bo Herlin wrote:
Hi
How come
foldr1 min [(maxBound::Int) % 1,1 % 2] 2147483647 % 1
I guess that foldr1 min == minimum
but
foldr1 min [2147483647 % 1,1 % 2] 1 % 2
Why???
The first one certainly causes an overflow with machine word Ints whereas 2147483647 is an Integer and thus all other numbers are interpreted as Integers.

Hmm, too simple :-P ...Thanks Henning Thielemann wrote:
On Wed, 13 Apr 2005, Bo Herlin wrote:
Hi
How come
foldr1 min [(maxBound::Int) % 1,1 % 2]
2147483647 % 1
I guess that foldr1 min == minimum
but
foldr1 min [2147483647 % 1,1 % 2]
1 % 2
Why???
The first one certainly causes an overflow with machine word Ints whereas 2147483647 is an Integer and thus all other numbers are interpreted as Integers.

Hmm... let a = (maxBound :: Int)%1 in 1 < a && a < 1/2 == True
From the Ord instance for Ratio a (x:%y) < (x':%y') = x * y' < x' * y
So the comparison for 1 < a looks like
1 * 1 < (maxBound :: Int) * 1
which is true.
And the comparison for a < 1/2 looks like
(maxBound :: Int) * 2 < 1 * 1
==> -2 < 1
which is again true!
If you want well-behaved rationals, I suppose you have to use Ratio Integer.
Hope this helps,
- Cale
On 4/13/05, Bo Herlin
Hi
How come
foldr1 min [(maxBound::Int) % 1,1 % 2] 2147483647 % 1
but
foldr1 min [2147483647 % 1,1 % 2] 1 % 2
Why???
/Bo Herlin _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Yep, got it, Thanks! Cale Gibbard wrote:
Hmm... let a = (maxBound :: Int)%1 in 1 < a && a < 1/2 == True
From the Ord instance for Ratio a (x:%y) < (x':%y') = x * y' < x' * y
So the comparison for 1 < a looks like 1 * 1 < (maxBound :: Int) * 1 which is true. And the comparison for a < 1/2 looks like (maxBound :: Int) * 2 < 1 * 1 ==> -2 < 1 which is again true!
If you want well-behaved rationals, I suppose you have to use Ratio Integer.
Hope this helps, - Cale
On 4/13/05, Bo Herlin
wrote: Hi
How come
foldr1 min [(maxBound::Int) % 1,1 % 2] 2147483647 % 1
but
foldr1 min [2147483647 % 1,1 % 2] 1 % 2
Why???
/Bo Herlin _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
participants (3)
-
Bo Herlin
-
Cale Gibbard
-
Henning Thielemann