Reputation: 157
I'm relatively new to haskell, and know that we can use the negation to negate a list. The negation does not negate the number zero in a regular list of type float, int, and integer. But what if you had a list of a different data type? If I negate a different data type, then the number zero in that list will also be negated. Is there a way to not negate numbers like 0 and 0.0 in the list?
Upvotes: 0
Views: 723
Reputation: 153007
You say
The negation does not negate the number zero in a regular list of type float
but this assertion is incorrect. See:
> negate 0 :: Float
-0.0
> negate 0 :: Double
-0.0
> map negate [0] :: [Float]
[-0.0]
The behavior of the rest of your code follows directly from this fact. For further reading I highly recommend What Every Computer Scientist Should Know About Floating-Point Arithmetic, which includes an in-depth discussion of why floating point must have a negative zero distinct from zero. But the short version is this sentence from page 201:
If zero did not have a sign, then the relation (1/(1/x)) = x would fail to hold when x = ±∞.
Upvotes: 4