enumFromThenTo

Apr 25, 2010 22:46


last ([0, 1 / 13 .. 1] :: [Double]) <= 1
False

Что же это такое творится?

fp, haskell

Leave a comment

Comments 15

thedeemon April 25 2010, 18:56:31 UTC
Кто ж вещественные числа на равенство сравнивает? ССЗБ

Reply

voidex April 25 2010, 19:26:44 UTC
В данном вопросе это не суть.
Если он на [0, 5 .. 13] генерит мне [0, 5, 10, 15], то не я тут виноват, и то, что 13 с 15 сравнивать нельзя - не оправдание.

Reply

udpn February 1 2011, 16:29:52 UTC
И в real world haskell это подробно описано.

Reply


mibori April 25 2010, 19:06:29 UTC
вообще, это не порядок, согласен.

Prelude Data.List> [0, 1 / 13 .. 1] :: [Double]
[0.0,7.692307692307693e-2,0.15384615384615385,0.23076923076923078,
0.3076923076923077,0.38461538461538464,0.46153846153846156,0.5384615384615385,
0.6153846153846155,0.6923076923076925,0.7692307692307695,0.8461538461538465,
0.9230769230769235,1.0000000000000004]

Reply


deni_ok April 25 2010, 21:23:07 UTC


-- The Enum instances for Floats and Doubles are slightly unusual.
-- The `toEnum' function truncates numbers to Int. The definitions
-- of enumFrom and enumFromThen allow floats to be used in arithmetic
-- series: [0,0.1 .. 0.95]. However, roundoff errors make these somewhat
-- dubious. This example may have either 10 or 11 elements, depending on
-- how 0.1 is represented.
http://haskell.org/onlinereport/standard-prelude.html

Reply

voidex April 25 2010, 22:27:39 UTC
Понятное дело, что 0, 1/n .. 1 может до 1 и не дойти, но перейти? Это меня и удивило.

Reply

deni_ok April 26 2010, 18:18:01 UTC
Если вдуматься, то разумное решение (если уж делать Float и Double инстансами Enum).

Пробуем GHCi, version 6.8.3

GHCi, version 6.8.3: http://www.haskell.org/ghc/ :? for help
Loading package base ... linking ... done.
Prelude> [0, 1 / 13 .. 1] :: [Double]
[0.0,7.692307692307693e-2,0.15384615384615385,0.23076923076923078,0.307692307692
3077,0.38461538461538464,0.46153846153846156,0.5384615384615385,0.61538461538461
54,0.6923076923076923,0.7692307692307692,0.846153846153846,0.9230769230769229,0.
9999999999999998]
Пробуем GHCi, version 6.10.4

GHCi, version 6.10.4: http://www.haskell.org/ghc/ :? for help ( ... )

Reply

voidex April 29 2010, 09:46:31 UTC
По-моему, единственно разумное решение в таком случае, это map ((/ 13) . fromIntegral)[1..13]
За объяснение спасибо :)

Reply


geniepro April 26 2010, 04:04:00 UTC

Hugs> last ([0, 1 / 13 .. 1] :: [Double]) <= 1
True

Reply


lionet April 26 2010, 04:58:10 UTC
[vlm@staging:~]> uname -a
SunOS staging 5.10 Generic_118822-25 sun4u sparc SUNW,UltraAX-i2
[vlm@staging:~]> ghci
GHCi, version 6.8.2: http://www.haskell.org/ghc/ :? for help
Prelude> last ([0, 1 / 13 .. 1] :: [Double]) <= 1
True
Prelude> Connection to staging closed.
[vlm@nala:~]> uname -a
Darwin nala 9.8.0 Darwin Kernel Version 9.8.0: Wed Jul 15 16:57:01 PDT 2009; root:xnu-1228.15.4~1/RELEASE_PPC Power Macintosh
[vlm@nala:~]> ghci
GHCi, version 6.10.2: http://www.haskell.org/ghc/ :? for help
Prelude> last ([0, 1 / 13 .. 1] :: [Double]) <= 1
False
Prelude>

Reply

geniepro April 26 2010, 09:49:32 UTC
По ходу у них там в версии 6.10.х баг, зарепортил бы кто, что ли...

GHCi, version 6.8.3: http://www.haskell.org/ghc/ :? for help
Loading package base ... linking ... done.
Prelude> [0, 1 / 13 .. 1] :: [Double]
[0.0,7.692307692307693e-2,0.15384615384615385,0.23076923076923078,0.307692307692
3077,0.38461538461538464,0.46153846153846156,0.5384615384615385,0.61538461538461
54,0.6923076923076923,0.7692307692307692,0.846153846153846,0.9230769230769229,0.
9999999999999998]

GHCi, version 6.10.4: http://www.haskell.org/ghc/ :? for help ( ... )

Reply

lionet April 26 2010, 10:55:05 UTC
Походу, это вообще не связанная с floating point проблема, да:

Prelude> [0,1/13..1.03]
[0.0,7.692307692307693e-2,0.15384615384615385,0.23076923076923078,0.3076923076923077,0.38461538461538464,0.46153846153846156,0.5384615384615385,0.6153846153846155,0.6923076923076925,0.7692307692307695,0.8461538461538465,0.9230769230769235,1.0000000000000004]
Prelude>

Reply

lionet April 26 2010, 10:58:49 UTC
oops, так правильнее:

Prelude> [0,1/13..0.97]
[0.0,7.692307692307693e-2,0.15384615384615385,0.23076923076923078,0.3076923076923077,0.38461538461538464,0.46153846153846156,0.5384615384615385,0.6153846153846155,0.6923076923076925,0.7692307692307695,0.8461538461538465,0.9230769230769235,1.0000000000000004]
Prelude>

Reply


Leave a comment

Up