Carp/core/Double.carp

122 lines
3.5 KiB
Plaintext
Raw Permalink Normal View History

2018-01-24 18:08:18 +03:00
(system-include "carp_double.h")
2018-01-24 17:53:18 +03:00
2021-07-05 15:48:35 +03:00
(doc Double "is the default floating point number type.")
2017-06-26 12:15:03 +03:00
(defmodule Double
2017-12-15 00:48:19 +03:00
(def pi 3.141592653589793)
2020-05-11 05:53:35 +03:00
(implements pi Double.pi)
2017-11-22 20:25:03 +03:00
(def e 2.718281828459045)
(register MAX Double "CARP_DBL_MAX")
feat: register MAX and MIN macros for stdint types (#1412) * feat: register MAX and MIN macros for stdint types Adds MAX and MIN for each INT<N> type and MAX for each UINT<N> type. These are macros defined by stdint.h and are sometimes useful for bounds determinations and conversions and such. * feat: make MAX and MIN interfaces Since several numeric types define maximum and minimum values, it makes sense for these to be defined as interfaces. This commit also makes existing definitions of MAX and MIN for Carp's numeric types implement the interfaces. * fix: respect let binding shadowing in memory management (#1413) * fix: respect let binding shadowing in memory management Previously, we didn't account for shadowing in let bindings in our memory management routines. This led to rare situations in which multiple deleters might be added for a single variable name, for example: ```clojure (defn n [xs] (let [xs [1 2 3] n &xs] n)) ``` The borrow checker would fail on this code since it would assign `xs` two deleters, one for the untyped argument and another for the let binding. Instead, we now perform *exclusive* ownership transfer for the duration of the let scope--when a shadow is introduced, the previous deleters for that variable name are dropped until the end of the let scope, since we evaluate all instances of the shadowed name to the more local binding. At the end of the let scope, the original deleter is restored. Fixes issue #597 * refactor: improved dead reference error for let Since let scopes resolve to their bodies, we can report the body of the let as the xobj producing an error when a dead reference is returned. * test: update error message for dead refs in let * test: add regression test for issue #597 Ensure we don't regress and fail to manage memory when let bindings shadow function argument names. * fix: respect symbol modes on interface concretization (#1415) * fix: respect symbol modes on interface concretization When concretizing interfaces (finding the appropriate implementation at a call site) we previously set the lookup mode of all such resolved symbols to CarpLand AFunction. This incorrectly overwrites the lookup mode of Externally registered types, causing them to emit incorrect C when the user specifies an override. We now preserve whatever lookup mode is assigned to the implementation the concretization resolves the interface to. This not only fixes the external override emission issue, but should be more correct in general. fixes #1414 * test: add regression test for issue #1414
2022-04-13 10:32:46 +03:00
(implements MAX MAX)
2017-10-24 14:52:32 +03:00
(register = (Fn [Double Double] Bool))
(register < (Fn [Double Double] Bool))
(register > (Fn [Double Double] Bool))
(register neg (Fn [Double] Double))
2017-06-26 12:15:03 +03:00
(register + (Fn [Double Double] Double))
(register - (Fn [Double Double] Double))
(register * (Fn [Double Double] Double))
(register / (Fn [Double Double] Double))
(register to-int (Fn [Double] Int))
(register from-int (Fn [Int] Double))
(register to-float (Fn [Double] Float))
(register from-float (Fn [Float] Double))
(register to-long (Fn [Double] Long))
(register from-long (Fn [Long] Double))
2018-06-07 19:24:30 +03:00
(register to-bytes (Fn [Double] Long))
(register copy (Fn [(Ref Double)] Double))
(implements + Double.+)
(implements - Double.-)
(implements * Double.*)
(implements / Double./)
(implements < Double.<)
(implements > Double.>)
(implements = Double.=)
(implements copy Double.copy)
(implements neg Double.neg)
(implements to-int Double.to-int)
(implements from-int Double.from-int)
(register abs (Fn [Double] Double))
2017-10-20 01:44:25 +03:00
(register acos (Fn [Double] Double))
(register asin (Fn [Double] Double))
(register atan (Fn [Double] Double))
(register atan2 (Fn [Double Double] Double))
(register ceil (Fn [Double] Double))
(register cos (Fn [Double] Double))
(register cosh (Fn [Double] Double))
(register exp (Fn [Double] Double))
(register floor (Fn [Double] Double))
(register frexp (Fn [Double (Ref Int)] Double))
(register ldexp (Fn [Double Int] Double))
(register log (Fn [Double] Double))
(register log10 (Fn [Double] Double))
(register mod (Fn [Double Double] Double))
(register modf (Fn [Double (Ref Double)] Double))
(register pow (Fn [Double Double] Double))
(register sin (Fn [Double] Double))
(register sinh (Fn [Double] Double))
(register sqrt (Fn [Double] Double))
(register tan (Fn [Double] Double))
(register tanh (Fn [Double] Double))
2017-10-24 14:52:32 +03:00
(implements abs Double.abs)
(implements acos Double.acos)
(implements asin Double.asin)
(implements atan Double.atan)
(implements atan2 Double.atan2)
(implements ceil Double.ceil)
(implements cos Double.cos)
(implements cosh Double.cosh)
(implements exp Double.exp)
(implements floor Double.floor)
(implements frexp Double.frexp)
(implements ldexp Double.ldexp)
(implements log Double.log)
(implements log10 Double.log10)
(implements mod Double.mod)
(implements modf Double.modf)
(implements pow Double.pow)
(implements sin Double.sin)
(implements sinh Double.sinh)
(implements sqrt Double.sqrt)
(implements tan Double.tan)
(implements tanh Double.tanh)
(doc approx "checks whether `x` and `y` are approximately equal.
The margin of error is `0.00001`.")
2017-10-24 14:52:32 +03:00
(defn approx [x y]
(Generics.approx x y))
2017-10-24 14:52:32 +03:00
(doc zero "returns the value `0.0`.")
(defn zero []
0.0)
(implements zero Double.zero)
(defn inc [x]
(+ 1.0 x))
(implements inc Double.inc)
(defn dec [x]
(- x 1.0))
(implements dec Double.dec)
(defn add-ref [x y]
(Double.+ @x @y))
(implements add-ref Double.add-ref)
2018-01-15 18:27:05 +03:00
)
(defmodule DoubleRef
(defn = [a b]
(Double.= @a @b))
(implements = DoubleRef.=)
2018-01-15 18:27:05 +03:00
(defn < [a b]
(Double.< @a @b))
(implements < DoubleRef.<)
2018-01-15 18:27:05 +03:00
(defn > [a b]
(Double.> @a @b))
(implements > DoubleRef.>)
2017-10-24 14:52:32 +03:00
)