Clojure-arithmetic-operators
提供:Dev Guides
Clojure-算術演算子
Clojure言語は、通常の算術演算子を任意の言語としてサポートします。 Clojureで使用できる算術演算子は次のとおりです。
Operator | Description | Example |
---|---|---|
+ | Addition of two operands | (+ 1 2) will give 3 |
− | Subtracts second operand from the first | (- 2 1) will give 1 |
* | Multiplication of both operands | (* 2 2) will give 4 |
/ | Division of numerator by denominator | (float (/3 2)) will give 1.5 |
inc | Incremental operators used to increment the value of an operand by 1 | inc 5 will give 6 |
dec | Incremental operators used to decrement the value of an operand by 1 | dec 5 will give 4 |
max | Returns the largest of its arguments | max 1 2 3 will return 3 |
min | Returns the smallest of its arguments | min 1 2 3 will return 1 |
rem | Remainder of dividing the first number by the second | rem 3 2 will give 1 |
例
次のコードスニペットは、さまざまな演算子の使用方法を示しています。
(ns clojure.examples.hello
(:gen-class))
;; This program displays Hello World
(defn Example []
(def x (+ 2 2))
(println x)
(def x (- 2 1))
(println x)
(def x (* 2 2))
(println x)
(def x (float(/2 1)))
(println x)
(def x (inc 2))
(println x)
(def x (dec 2))
(println x)
(def x (max 1 2 3))
(println x)
(def x (min 1 2 3))
(println x)
(def x (rem 3 2))
(println x))
(Example)
上記のプログラムは、次の出力を生成します。
出力
4
1
4
2.0
3
1
3
1
1