Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Absolute value of a number in Clojure

Tags:

clojure

How can the absolute number of a value be calculated in Clojure?

(abs  1) => 1 (abs -1) => 1 (abs  0) => 0 
like image 841
Drew Noakes Avatar asked Feb 13 '14 11:02

Drew Noakes


1 Answers

For double, float, long and int you can use the java.lang.Math method abs (Math/abs -1)

Take care it won't work for decimals, ratio's, bigint(eger)s and other Clojure numeric types. The official clojure contrib math library that tries guarantee working correctly with all of these is clojure.math.numeric-tower

like image 84
NielsK Avatar answered Oct 09 '22 16:10

NielsK