Clojure进阶:数学计算

本文翻译自Mathematics with Clojure
本文包含了使用Clojure的内建函数,扩展包和部分JDK功能进行数学计算.
版权:
This work is licensed under a Creative Commons Attribution 3.0 Unported License (including images & stylesheets). The source is available on Github.


1 准备

这里的一些例子使用了math.numeric-towermath.combinatorics库.所以需要
你在代码的命名空间中包含如下代码:


(:require [clojure.math.numeric-tower :as math]
          [clojure.math.combinatorics :as combo])
或者在REPL里载入:


(require '[clojure.math.numeric-tower :as math])
(require '[clojure.math.combinatorics :as combo])

2 方法

2.1 简单计算


(+ 3 4)    ;=> 7
(- 3 4)    ;=> -1
(* 3 4)    ;=> 12
(/ 3 4)    ;=> 3/4  (an exact ratio)
(/ 3.0 4)  ;=> 0.75

(inc 5)    ;=> 6
(dec 5)    ;=> 4
想要计算整数的除法,取余和取模,请看 quot, rem, mod
想计算指数的平方根,四舍五入,上下限,绝对值和最大公约数/最小公倍数,请看 math.numeric-tower


2.2 三角函数

使用Java平台提供的函数:


Math/PI       ;=> 3.14159...
(Math/sin x)
(Math/cos x)
(Math/tan x)
还有很多的函数,你可以查看 java.lang.Math


2.3 组合数学

对于组合数学相关函数(比如:combinations和permutations),请看math.combinatorics

Author: Ivan (ivan.pig@foxmail.com)

Date: 2014-02-09 日

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值