Skip to content

numerical equality with = and == #859

Closed
@ikappaki

Description

@ikappaki

Clojure has this concept with numerical equality that = returns true iff its arguments are numerically the same and fall under the same category:

https://clojure.org/guides/equality

Clojure’s = is true when called with two immutable scalar values, if:
    Both arguments are nil, true, false, the same character, or the same string (i.e. the same sequence of characters).
    Both arguments are symbols, or both keywords, with equal namespaces and names.
    Both arguments are numbers in the same 'category', and numerically the same, where category is one of:
        integer or ratio
        floating point (float or double)
        [BigDecimal](https://docs.oracle.com/javase/8/docs/api/java/math/BigDecimal.html).

That means that if we compare an integer or a ratio with the floating point number in Clojure that have the same numericall value, it will return false:

user> (= 1 1/1)
true
user> (= 1 1.0)
false

while in Basilisp

user> (= 1 1/1)
true
user> (= 1 1.0)
true

It appears to me this is a deliberate decision in the Clojure design, rather than a Java runtime decision, for example

jshell> 1 == 1.0
$5 ==> true

Clojure also offers the less restricive == core fn, which soley compares numbers for numerical equality without considering the category (same as basilisp's =)

user> (== 1 1.0)
true

Is the Clojure behavior something we want to support in Basilisp or shall we define == the same as =? I am inclining towards the first, but not sure what the implementation tradeoffs might be.

Thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    langIssue pertaining to Basilisp language modulesquestionFurther information is requested

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions