Comparison of negative and positive floating-point zero

Hello,

The following evaluates to false in SWI-Prolog:

?- -0.0 == 0.0.
false

In my program’s calculations, I would like -0.0 to be equal to 0.0.

Is this a bug or a feature? and how can I get the equality -0.0 == 0.0 to work without introducing a special predicate to handle this?

Cheers,
Amine.

Hi Amine,

You can use the numerical equality comparison operator =:=/2:

?- 0.0 =:= -0.0.
true.

?= -0.0 =:= 0.
true.

?- 0 =:= 0.0.
true.
1 Like

Thanks a lot for the prompt help :slight_smile: Now things are great.
For the record, the equality -0.0 == 0.0 works fine in Sicstus (from which I am porting my program).

Possibly the SICStus reader simply maps -0.0 to 0.0. The status of the IEEE float specs for the `unusual’ numbers in Prolog is quite unclear. SWI (and ECLiPSe) handle -0.0, ±Inf and NaN. Most systems don’t, where -0.0 == 0.0 and the others cannot be created as the operations that would create them raise an exception rather then the special value. Raising exceptions is defined by ISO and also done in SWI, but special floats can be exchanged through the foreign interface and be read and written such that we can support serializations (e.g., XML) that support these values.