Back
float (clj)
(source)function
(float x)
Coerce to float
Examples
originrose/cortex
(ns cortex.util-test
(:refer-clojure :exclude [defonce])
(:require
#?(:cljs [cljs.test :refer-macros [deftest is testing]]
:clj [clojure.test :refer [deftest is testing]])
[clojure.core.matrix :as m]
[clojure.string :as str]
[cortex.util :refer :all]))
(deftest approx=-test
(testing "comparing floats"
(is (false? (approx= 0.1 10 11)))
(is (false? (approx= 0.1 10 11.0)))
(is (true? (approx= 0.1 10.0 11.0)))
(is (false? (approx= 0.09 10.0 11.0))))
(testing "comparing more than two floats"
(is (false? (approx= 0.1 10.0 10.75 11.5)))
(is (true? (approx= 0.1 10.0 10.5 11.0))))
(testing "comparing vectors"
(is (false? (approx= 0.1
(m/array :vectorz [10.0 11.0])
[11.5 10.0])))
(is (true? (approx= 0.1
(m/array :vectorz [10.0 11.0])
[11.0 10.0]))))
(testing "comparing nested data structures"
(is (false? (approx= 0.1
{:a [10.0 11.0] :b 10.0}
{:a [11.0 10.0] :c 10.0})))
(is (false? (approx= 0.1
{:a [10.0 11.0] :b 10.0}
{:a [12.0 10.0] :b 10.0})))
(is (true? (approx= 0.1
{:a [10.0 11.0] :b 10.0}
{:a [11.0 10.0] :b 10.0}))))
(testing "lists of different lengths"
(is (false? (approx= 0.1
[1.0 2.0 3.0]
[1.0 2.0])))))
metosin/compojure-api
(ns example.handler
"Asynchronous compojure-api application."
(:require [compojure.api.sweet :refer :all]
[ring.util.http-response :refer :all]
[manifold.deferred :as d]
[clojure.core.async :as async]
compojure.api.async))
(GET "/divide" []
:return {:result Float}
:query-params [x :- Long, y :- Long]
:summary "divide two numbers together"
(async/go (ok {:result (float (/ x y))}))))
hraberg/deuce
(ns deuce.emacs.floatfns
(:use [deuce.emacs-lisp :only (defun defvar)])
(:require [clojure.core :as c]
[deuce.emacs.alloc :as alloc])
(:refer-clojure :exclude [float]))
(defun ftruncate (arg)
"Truncate a floating point number to an integral float value.
Rounds the value toward zero."
(double (long arg)))
(defun abs (arg)
"Return the absolute value of ARG."
(if (float? arg)
(Math/abs (double arg))
(Math/abs (long arg))))
(defun ldexp (sgnfcand &optional exponent)
"Construct number X from significand SGNFCAND and exponent EXP.
Returns the floating point value resulting from multiplying SGNFCAND
(the significand) by 2 raised to the power of EXP (the exponent)."
(* (Math/pow 2 (or exponent 1)) sgnfcand))
(defun fceiling (arg)
"Return the smallest integer no less than ARG, as a float.
(Round toward +inf.)"
(Math/ceil arg))
(defun float (arg)
"Return the floating point number equal to ARG."
(double arg))
(defun ffloor (arg)
"Return the largest integer no greater than ARG, as a float.
(Round towards -inf.)"
(double (long arg)))
(defun truncate (arg &optional divisor)
"Truncate a floating point number to an int.
Rounds ARG toward zero.
With optional DIVISOR, truncate ARG/DIVISOR."
(long (/ arg (or divisor 1))))
(defun copysign (x1 x2)
"Copy sign of X2 to value of X1, and return the result.
Cause an error if X1 or X2 is not a float."
(Math/copySign (double x1) (double x2)))
(defun fround (arg)
"Return the nearest integer to ARG, as a float."
(double (long arg)))
(defun frexp (x)
"Get significand and exponent of a floating point number.
Breaks the floating point number X into its binary significand SGNFCAND
(a floating point value between 0.5 (included) and 1.0 (excluded))
and an integral exponent EXP for 2, such that:
(defun logb (arg)
"Returns largest integer <= the base 2 log of the magnitude of ARG.
This is the same as the exponent of a float."
(Math/getExponent (double arg)))
hraberg/deuce
(ns deuce.emacs.print
(:use [deuce.emacs-lisp :only (defun defvar)])
(:require [clojure.core :as c]
[clojure.string :as s]
[deuce.emacs.buffer :as buffer]
[deuce.emacs.data :as data]
[deuce.emacs.editfns :as editfns]
[deuce.emacs.fns :as fns])
(:refer-clojure :exclude [print]))
(defvar float-output-format nil
"The format descriptor string used to print floats.
This is a %-spec like those accepted by `printf' in C,
but with some restrictions. It must start with the two characters `%.'.
After that comes an integer precision specification,
and then a letter which controls the format.
The letters allowed are `e', `f' and `g'.
Use `e' for exponential notation \"DIG.DIGITSeEXPT\"
Use `f' for decimal point notation \"DIGITS.DIGITS\".
Use `g' to choose the shorter of those two formats for the number at hand.
The precision in any of these cases is the number of digits following
the decimal point. With `f', a precision of 0 means to omit the
decimal point. 0 is not allowed with `e' or `g'.
typedclojure/typedclojure
(ns ^:no-doc typed.ann.clojure
"Type annotations for the base Clojure distribution."
#?(:cljs (:require-macros [typed.ann-macros.clojure :as macros]))
(:require [clojure.core :as cc]
[typed.clojure :as t]
#?(:clj [typed.ann-macros.clojure :as macros])
#?(:clj typed.ann.clojure.jvm) ;; jvm annotations
#?(:clj clojure.core.typed))
#?(:clj
(:import (clojure.lang PersistentHashSet PersistentList
APersistentMap #_IPersistentCollection
#_ITransientSet
IRef)
(java.util Comparator Collection))))
cc/integer? (t/Pred t/AnyInteger)
cc/int? (t/Pred #?(:clj (t/U Long
Integer
Short
Byte)
:cljs (t/U t/CLJSInteger
goog.math.Integer
goog.math.Long)))
cc/pos-int? [t/Any :-> t/Bool
:filters {:then (is #?(:clj (t/U Long
Integer
Short
Byte)
:cljs (t/U t/CLJSInteger
goog.math.Integer
goog.math.Long)) 0)}]
cc/neg-int? [t/Any :-> t/Bool
:filters {:then (is #?(:clj (t/U Long
Integer
Short
Byte)
:cljs (t/U t/CLJSInteger
goog.math.Integer
goog.math.Long)) 0)}]
cc/nat-int? [t/Any :-> t/Bool
:filters {:then (is #?(:clj (t/U Long
Integer
Short
Byte)
:cljs (t/U t/CLJSInteger
goog.math.Integer
goog.math.Long)) 0)}]
cc/number? (t/Pred t/Num)
cc/double? (t/Pred #?(:clj Double
:cljs t/Num))
cc/float? (t/Pred #?(:clj (t/U Double Float)
:cljs t/Num))
cc/ident? (t/Pred t/Ident)
cc/simple-ident? [t/Any :-> t/Bool :filters {:then (is t/Ident 0)}]
cc/qualified-ident? [t/Any :-> t/Bool :filters {:then (is t/Ident 0)}]
cc/simple-symbol? [t/Any :-> t/Bool :filters {:then (is t/Sym 0)}]
cc/qualified-symbol? [t/Any :-> t/Bool :filters {:then (is t/Sym 0)}]
cc/simple-keyword? [t/Any :-> t/Bool :filters {:then (is t/Kw 0)}]
cc/qualified-keyword? [t/Any :-> t/Bool :filters {:then (is t/Kw 0)}]
cc/var? (t/Pred t/AnyVar)
cc/float [t/Num :-> #?(:cljs t/Num :default Float)]
cc/int [#?(:cljs t/Num :default (t/U Character t/Num)) :-> #?(:cljs t/AnyInteger :default Integer)]
;cast to java array
;; TODO rethink input and output types. eg.,
;; cc/booleans [(ReadyOnlyArray boolean) :-> (t/U nil (Array boolean))]
;; TODO objects??
;; cc/objects [(ReadyOnlyArray Object) :-> (t/U nil (ReadyOnlyArray Object))]
;;
;; TODO propagate to Numbers/booleans etc
;cc/booleans [t/Any :-> (t/U nil (Array boolean))]
;cc/bytes [t/Any :-> (t/U nil (Array byte))]
;cc/chars [t/Any :-> (t/U nil (Array char))]
;cc/shorts [t/Any :-> (t/U nil (Array short))]
;cc/ints [t/Any :-> (t/U nil (Array int))]
;cc/longs [t/Any :-> (t/U nil (Array long))]
;cc/floats [t/Any :-> (t/U nil (Array float))]
;cc/doubles [t/Any :-> (t/U nil (Array double))]