在Clojure中BigInt和BigInteger的用例 [英] use-cases for BigInt versus BigInteger in Clojure

查看:135
本文介绍了在Clojure中BigInt和BigInteger的用例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找有关何时在Clojure中使用Clojure BigInt和Java BigInteger的指导。两个工作都很好,我假设使用BigInt的主要原因是利用 + = ,它们必须通过Java实例方法 .add .equals 访问。但是有很少的操作符,例如 isProbablePrime ,我只能从BigInteger访问。

I'm looking for guidance on when to use Clojure BigInt versus Java BigInteger in Clojure. Both work just fine, and I am assuming that the main reason to use BigInt is to take advantage of operators like + and =, which have to be accessed via the Java instance methods .add and .equals, for instance. But there are few operators, such as isProbablePrime, that I can only access from BigInteger.

从BigInt转移到BigInteger,反之亦然,但是两者的存在使得用例对我来说不清楚。我的膝盖反应只是坚持使用BigInteger在没有明确的标准,因为一些建议的用法似乎不工作。从 clojuredocs此处

It seems pretty easy to shift from BigInt to BigInteger or vice versa, but the presence of both makes the use-cases unclear for me. My knee-jerk reaction is just to stick with BigInteger in the absence of clear criteria since some of the suggested usages seem not to work. From clojuredocs here:

user=> (def x (bigint 97))
user=> (.isProbablePrime x 1)
IllegalArgumentException No matching method found: isProbablePrime for class     
clojure.lang.BigInt  clojure.lang.Reflector.invokeMatchingMethod (Reflector.java:53)


推荐答案

在C. Emerick等人的Clojure Programming al。,p.428,有一个边栏主题,当Java已经在BigInteger中提供一个时,为什么Clojure有自己的BigInt类?

In "Clojure Programming" by C. Emerick et. al., p.428, there is a sidebar topic, "Why Does Clojure Have Its Own BigInt Class When Java Already Provides One in BigInteger?"

更喜欢 BigInt 到Java的 BigInteger 。首先,后者的 .hashCode 实现与 Long 的不一致(每个类型中表示的相同数字给出不同的哈希值)。这通常不是你想要什么时比较例如。哈希映射。

They note two reasons to prefer BigInt to Java's BigInteger. First, the latter's .hashCode implementation is inconsistent with that of Long (the same number expressed in each type gives a different hash value). This is generally not what you want when comparing equivalent values in e.g. hash maps.

另一个原因是 BigInt

我会使用Clojure的数字类型,除非你有一个很好的理由不要(你使用 .isProbablePrime 表示你可能有足够的理由)。

I would use Clojure's numeric types unless you have a good reason not to (your use of .isProbablePrime suggests you might have a good enough reason).

这篇关于在Clojure中BigInt和BigInteger的用例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆