首页 > 其他 > 详细

Clojure进阶:数学计算

时间:2014-02-20 13:34:03      阅读:502      评论:0      收藏:0      [点我收藏+]

本文翻译自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 日

Clojure进阶:数学计算

原文:http://blog.csdn.net/ivan_pig/article/details/19503881

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!