4 ms·
I'll show you what surprised me tonight. This is what I came up with to see how Common Lisp handles "minus zero": (setq +zero +0.0d0) (setq -zero -0.0d
by ksaj 6y ago
I'll show you what surprised me tonight. This is what I came up with to see how Common Lisp handles "minus zero":
(setq +zero +0.0d0)
(setq -zero -0.0d0)
(* -zero +zero)
I just set two variables. One for positive zero, and one for negative zero. The decimal business is so that Lisp will use floating point, which lets the negative zero happen. Then I multiply them together.
Expectation: multiplying a positive by a negative should give me a negative number.
On my laptop I have LispWorks (commercial), sbcl (pretty much the most common non-commercial version of Common Lisp in use), and clisp (my favourite old standby that I grew up with, but sometimes like to use because it's fast and pretty forgiving when you're just hashing something out quickly.) Here's what they produce:
-0.0d0 ; sbcl
-0.0D0 ; LispWorks
0.0d0 ; clisp
That makes clisp a bit of an outlier in my (very limited) survey. I'm guessing its a clisp bug that not many people would notice. But I don't have other Lisps to compare. I doubt it would mess up anyone's code. But at the very least, sometimes the - and + signs are good for showing intent.
So I'm a bit surprised that my answer to your question has to be "It depends."
* (oddp ?)
T
*
- lispm 6y agoFor the record, that's not a CLISP bug. It's a design decision mentioned in its manual. The Common Lisp standard allows to have a negative float zero, but it does not require it.
- kazinator 6y ago> Expectation: multiplying a positive by a negative should give me a negative number. What is your definition of "negative number"? There is one in Lisp: the minusp function. What does that say? -0.0 is not a negative number, but a redundant representation of zero possible in a sign-magnitude system.
- ksaj 6y agoUser lispm points out that this was a design decision that's in the clisp documentation. I couldn't find the reference, but I did find that they also compute any "negative integer" as zero, so that might seem consistent at the least. Mainly I pointed this out because OP was asking about how my programming language handles it. LispWorks, sbcl and clisp are technically all the same language, so there is no singular answer to that question when it comes to Common Lisp.
- lispm 6y ago12.2.4 https://clisp.sourceforge.io/impnotes.html#num-concepts https://clisp.sourceforge.io/impnotes.html#num-concepts and http://www.lispworks.com/documentation/HyperSpec/Body/t_float.htm http://www.lispworks.com/documentation/HyperSpec/Body/t_floa... > In addition, there is a floating-point zero; depending on the implementation, there can also be a ``minus zero''. If there is no minus zero, then 0.0 and -0.0 are both interpreted as simply a floating-point zero. (= 0.0 -0.0) is always true. If there is a minus zero, (eql -0.0 0.0) is false, otherwise it is true.
- ksaj 6y agoAsk, and ye shall receive. Thanks for the links.
- kazinator 6y agoHow the language handles it is that neither 0.0 nor -0.0 satisfy either the minusp or plusp function, and the numbers are equal under eql or weaker. I think the only way to write a comparison function f such that (f -0.0 0.0) yields nil would be to print the numbers into character strings and compare those. But even that wouldn't be portable to CLISP.
- ksaj 6y agoA rising zero / falling zero is the difference between freezing and melting, where you can see zero as a fuzzy number that is too low to consider close to 1, and too high to consider close to -1, while distinctly approaching one or the other. Here's a LispWorks session: CL-USER 1 > (minusp -0.0f0) ; as you said NIL CL-USER 2 > (< -0.f0 0.0f0) ; as minusp & you predict NIL CL-USER 3 > (* -0.f0 0.f0) ; but... -0.0 CL-USER 4 > It seems inconsistent that the multiply function understands that zero is approaching -1, but less-than doesn't.