Carp/core/Double.carp
scottolsen 040e9e4391 Add an implements primitive, update core
This change adds a new primitive Implements which changes interface
implementations from being implicit to being explicit. Going forward,
users will have to declare (implements <interface> <implementation>) to
explicitly add a function to an interface. This provides two benefits:

- Prevents unwitting name clashes. Previously, if one defined a function
  that happened to have the same name as an interface, it was
  automatically assumed the function implemented that interface when this
  is not always the intention, especially in large programs.
- Name flexibility. One can now implement an interface with a function
  that has a different name than the interface, which allows for greater
  flexibility.

I've updated core to make the necessary calls to the new primitive.

Since str and copy are derived automatically for types, we treat these
functions as a special case and auto-implement the interfaces.
2020-05-09 12:59:47 -04:00

115 lines
3.2 KiB
Plaintext

(system-include "carp_double.h")
(defmodule Double
(def pi 3.141592653589793)
(def e 2.718281828459045)
(register MAX Double "CARP_DBL_MAX")
(register = (Fn [Double Double] Bool))
(register < (Fn [Double Double] Bool))
(register > (Fn [Double Double] Bool))
(register neg (Fn [Double] Double))
(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))
(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))
(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))
(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.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`.")
(defn approx [x y]
(Generics.approx x y))
(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 inc Double.dec)
(defn add-ref [x y]
(Double.+ @x @y))
(implements add-ref Double.add-ref)
)
(defmodule DoubleRef
(defn = [a b]
(Double.= @a @b))
(defn < [a b]
(Double.< @a @b))
(defn > [a b]
(Double.> @a @b))
)