haskell - Why does flooring infinity not throw some error? -
found myself having case equivalent of floor $ 1/0
beingness executed.
λ> 1/0 infinity
this normal behavior far understand but, when infinity
floor
'd or ceiling
'd
λ> floor $ 1/0 179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586298239947245938479716304835356329624224137216
instead of failing, big number produced. why?
maybe more importantly, how can distinguish non faulty result without using filter before applying function?
the first question perhaps not important, i'll seek reply sec question first.
once have number, if know came floor x
, can't know whether x
valid representation of 2^1024
or if infinity. can assume outside of range of double invalid , produced infinity, negative infinity, nan or like. quite simple check if value valid using one/many of functions in realfloat
, isnan
, isinfinite
, etc.
you utilize data number = n | posinf | neginf
. write:
instance realfrac => realfrac (number a) ... floor (n n) = floor n floor posinf = error "floor of positive infinity" floor neginf = error "floor of negative infinity" ..
which approach best based on utilize case.
maybe right floor (1/0)
error. value garbage anyways. improve deal garbage or error?
but why 2^1024
? took @ source ghc.float
:
properfraction (f# x#) = case decodefloat_int# x# of (# m#, n# #) -> allow m = i# m# n = i# n# in if n >= 0 (fromintegral m * (2 ^ n), 0.0) else allow = if m >= 0 m `shiftr` negate n else negate (negate m `shiftr` negate n) f = m - (i `shiftl` negate n) in (fromintegral i, encodefloat (fromintegral f) n) floor x = case properfraction x of (n,r) -> if r < 0.0 n - 1 else n
note decodefloat_int#
returns mantissa , exponent. according wikipedia:
positive , negative infinity represented thus: sign = 0 positive infinity, 1 negative infinity. biased exponent = 1 bits. fraction = 0 bits.
for float
, means base of operations of 2^23, since there 23 bits in base, , exponent of 105 (why 105? have no idea. think should 255 - 127 = 128, seems 128 - 23). value of floor
fromintegral m * (2 ^ n)
or base*(2^exponent) == 2^23 * 2^105 == 2^128
. double value 1024.
haskell floating-point infinity floor
No comments:
Post a Comment