Back

pow (clj)

(source)

function

(pow a b)
Returns the value of a raised to the power of b. For more details on special cases, see: https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html#pow-double-double-

Examples

clojure
(ns clojure.test-clojure.math
  (:require
    [clojure.test :refer :all]
    [clojure.math :as m]))

(deftest test-pow
  (is (= 1.0 (m/pow 4.0 0.0)))
  (is (= 1.0 (m/pow 4.0 -0.0)))
  (is (= 4.2 (m/pow 4.2 1.0)))
  (is (NaN? (m/pow 4.2 ##NaN)))
  (is (NaN? (m/pow ##NaN 2.0)))
  (is (= ##Inf (m/pow 2.0 ##Inf)))
  (is (= ##Inf (m/pow 0.5 ##-Inf)))
  (is (= 0.0 (m/pow 2.0 ##-Inf)))
  (is (= 0.0 (m/pow 0.5 ##Inf)))
  (is (NaN? (m/pow 1.0 ##Inf)))
  (is (pos-zero? (m/pow 0.0 1.5)))
  (is (pos-zero? (m/pow ##Inf -2.0)))
  (is (= ##Inf (m/pow 0.0 -2.0)))
  (is (= ##Inf (m/pow ##Inf 2.0)))
  (is (pos-zero? (m/pow -0.0 1.5)))
  (is (pos-zero? (m/pow ##-Inf -1.5)))
  (is (neg-zero? (m/pow -0.0 3.0)))
  (is (neg-zero? (m/pow ##-Inf -3.0)))
  (is (= ##Inf (m/pow -0.0 -1.5)))
  (is (= ##Inf (m/pow ##-Inf 2.5)))
  (is (= ##-Inf (m/pow -0.0 -3.0)))
  (is (= ##-Inf (m/pow ##-Inf 3.0)))
  (is (= 4.0 (m/pow -2.0 2.0)))
  (is (= -8.0 (m/pow -2.0 3.0)))
  (is (= 8.0 (m/pow 2.0 3.0))))

(deftest test-ulp
  (is (NaN? (m/ulp ##NaN)))
  (is (= ##Inf (m/ulp ##Inf)))
  (is (= ##Inf (m/ulp ##-Inf)))
  (is (= Double/MIN_VALUE (m/ulp 0.0)))
  (is (= (m/pow 2 971) (m/ulp Double/MAX_VALUE)))
  (is (= (m/pow 2 971) (m/ulp (- Double/MAX_VALUE)))))
migae/lab.clj.appengine
(ns migae.math
  (:require [clojure.math.numeric-tower :as math]
            [compojure.core :refer :all]
            [compojure.route :as route]
            [ring.util.response :as rsp]
            [ring.util.servlet :as ring]
            [ring.middleware.defaults :refer :all]))

(defroutes math-routes
  (context
   "/math" []
   (GET "/plus" {params :query-params}  ; query string params
        (let [x (read-string (get params "x"))
              y (read-string (get params "y"))]
          (str (+ x y) "\n")))
   (GET "/foo" []
         (str "bar"))
   (POST "/minus" [x y :as req]        ; body params
         (str (- (read-string x) (read-string y)) "\n"))
   (GET "/times/:x/:y" [x y]           ; named (path) params
        (-> (rsp/response (str (* (read-string x) (read-string y)) "\n"))
            (rsp/content-type "text/html")))
   (GET "/power" {:keys [headers] :as req} ; header params
        (let [x (read-string (get headers "x-x"))
              y (read-string (get headers "x-y"))]
          (str (math/expt x y) "\n")))
    (route/not-found "<h1>Math API not found</h1>")))