Skip to content

Commit b77703d

Browse files
authored
Added == as an alias for = (#868)
Hi, could you please consider patch to add `==` as an alias for `=`. Fixes #859. I've also updated the doc to describe the difference of int vs float comparison in basilisp vs Clojure. Thanks --------- Co-authored-by: ikappaki <[email protected]>
1 parent 32df937 commit b77703d

File tree

5 files changed

+43
-2
lines changed

5 files changed

+43
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212
* Added `basilisp.stacktrace` namespace (#721)
1313
* Added support for `*flush-on-newline*` to flush the `prn` and `println` output stream after the last newline (#865)
1414
* Added support for binding destructuring in `for` bindings (#774)
15+
* Added `==` as an alias to `=` (#859)
1516

1617
### Changed
1718
* Cause exceptions arising from compilation issues during macroexpansion will no longer be nested for each level of macroexpansion (#852)

docs/differencesfromclojure.rst

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,23 @@ Type Differences
3232

3333
* Sorted sets, sorted maps, and array maps are not implemented (support is tracked in `#416 <https://github.com/basilisp-lang/basilisp/issues/416>`_).
3434

35+
.. _arithmetic_comparison:
36+
37+
Arithmetic Comparison
38+
---------------------
39+
40+
Basilisp, in contrast to Clojure, does not distinguish between integer (``int``) and floating point (``float``) as `separate categories for equality comparison purposes <https://clojure.org/guides/equality>`_ where the ``=`` comparison between any ``int`` and ``float`` returns ``false``. Instead, it adopts Python's ``=`` comparison operator semantics, where the ``int`` is optimistically converted to a ``float`` before the comparison. However, beware that this conversion can lead to `certain caveats in comparison <https://stackoverflow.com/a/30100743>`_ where in rare cases seemingly exact ``int`` and ``float`` numbers may still compare to ``false`` due to limitations in floating point number representation.
41+
42+
In Clojure, this optimistic equality comparison is performed by the ``==`` function. In Basilisp, ``==`` is aliased to behave the same as ``=``.
43+
44+
.. note::
45+
46+
Basilisp's ``=`` will perform as expected when using Python `Decimal <https://docs.python.org/3/library/decimal.html>`__ typed :ref:`floating_point_numbers`.
47+
48+
.. seealso::
49+
50+
Python's `floating point arithmetic <https://docs.python.org/3/tutorial/floatingpoint.html>`_ documentation
51+
3552
.. _concurrent_programming:
3653

3754
Concurrent Programming

docs/pyinterop.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ Type hints may be applied to :lpy:form:`def` names, function arguments and retur
246246
Return annotations are combined as by ``typing.Union``, so ``typing.Union[str, str] == str``.
247247
The annotations for individual arity arguments are preserved in their compiled form, but they are challenging to access programmatically.
248248

249-
.. _arithmeticdivision
249+
.. _arithmeticdivision:
250250

251251
Arithmetic division
252252
-------------------

src/basilisp/core.lpy

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -815,7 +815,11 @@
815815
(operator/is_ x y))
816816

817817
(defn =
818-
"Return ``true`` if ``x`` and ``y`` are equal, otherwise ``false``\\."
818+
"Return ``true`` if ``x`` and ``y`` are equal, otherwise ``false``\\.
819+
820+
Number equality follows Python's ``=`` semantics whereby \"a
821+
comparison between numbers of different types behaves as though the
822+
exact values of those numbers were being compared\"."
819823
([_] true)
820824
([x & args]
821825
(if (seq (rest args))
@@ -829,6 +833,11 @@
829833
[& args]
830834
(not (apply = args)))
831835

836+
(defn ==
837+
"Alias for lpy:fn:`=`."
838+
[& args]
839+
(apply = args))
840+
832841
(defn >
833842
"Return ``true`` if arguments are monotonically decreasing, otherwise ``false``\\."
834843
([_] true)

tests/basilisp/test_core_fns.lpy

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2211,3 +2211,17 @@
22112211
(.flush *out*)
22122212
(is (= (bio-write expected os/linesep "ab" os/linesep "ab" " " 1 os/linesep)
22132213
(.getvalue bio)))))))
2214+
2215+
;;;;;;;;;;;;;;;;
2216+
;; arithmetic ;;
2217+
;;;;;;;;;;;;;;;;
2218+
2219+
(deftest arithmetic-equality-test
2220+
(testing "int and float type equality"
2221+
(is (not= 1 1.00001))
2222+
(is (= 1 1.0000000000000000000000009)))
2223+
2224+
(testing "== is an alias of ="
2225+
(let [f 1.0000000000000000000000009]
2226+
(is (= 1 f))
2227+
(is (== 1 f)))))

0 commit comments

Comments
 (0)