Clojure - 用大数字计算 [英] Clojure - Calculate with big numbers

查看:30
本文介绍了Clojure - 用大数字计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 clojure 中计算 !1000,我怎么做才不会出现整数溢出异常?

I want to calculate !1000 in clojure, how can I do this without getting a integer-overflow exception?

我现在的阶乘代码是:(reduce * (range 1 1001)).

My factorial code is right now: (reduce * (range 1 1001)).

推荐答案

您可以使用 *' 运算符,该运算符通过自动将结果提升到 BigInt 来支持任意精度,以防它溢出:

You could use the *' operator which supports arbitrary precision by automatically promoting the result to BigInt in case it would overflow:

(reduce *' (range 1 1001))

这篇关于Clojure - 用大数字计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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