mirror of
https://github.com/carp-lang/Carp.git
synced 2024-09-11 05:25:28 +03:00
float: added all math function in math.h
This commit is contained in:
parent
ac1ff31a71
commit
64a82c2730
@ -4,12 +4,15 @@
|
||||
(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 sin (Fn [Double] Double))
|
||||
(register cos (Fn [Double] Double))
|
||||
(register tan (Fn [Double] Double))
|
||||
|
@ -1,5 +1,6 @@
|
||||
(defmodule Float
|
||||
(def π 3.1415926536f)
|
||||
(register neg (Fn [Float] Float))
|
||||
(register + (Fn [Float Float] Float))
|
||||
(register - (Fn [Float Float] Float))
|
||||
(register * (Fn [Float Float] Float))
|
||||
@ -8,4 +9,47 @@
|
||||
(register random-between (λ [Float Float] Float))
|
||||
(register str (Fn [Float] String))
|
||||
(register copy (Fn [(Ref Float)] Float))
|
||||
)
|
||||
|
||||
(register = (Fn [Float Float] Bool))
|
||||
(defn /= [x y]
|
||||
(not (= x y)))
|
||||
(register < (Fn [Float Float] Bool))
|
||||
(register > (Fn [Float Float] Bool))
|
||||
|
||||
(defn clamp [min, max, val]
|
||||
(if (> val max)
|
||||
max
|
||||
(if (< val min)
|
||||
min
|
||||
val)))
|
||||
|
||||
(defn approx [x y]
|
||||
(if (> x y)
|
||||
(< (- x y) 0.00001f)
|
||||
(< (- y x) 0.00001f)))
|
||||
|
||||
(register sin (Fn [Float] Float))
|
||||
(register cos (Fn [Float] Float))
|
||||
(register tan (Fn [Float] Float))
|
||||
(register acos (Fn [Float] Float))
|
||||
(register asin (Fn [Float] Float))
|
||||
(register atan (Fn [Float] Float))
|
||||
(register atan2 (Fn [Float Float] Float))
|
||||
(register cosh (Fn [Float] Float))
|
||||
(register sinh (Fn [Float] Float))
|
||||
(register tanh (Fn [Float] Float))
|
||||
(register exp (Fn [Float] Float))
|
||||
(register frexp (Fn [Float (Ref Int)] Float))
|
||||
(register ldexp (Fn [Float Int] Float))
|
||||
(register log (Fn [Float] Float))
|
||||
(register log10 (Fn [Float] Float))
|
||||
(register modf (Fn [Float (Ref Float)] Float))
|
||||
(register pow (Fn [Float Float] Float))
|
||||
(register sqrt (Fn [Float] Float))
|
||||
(register str (Fn [Float] String))
|
||||
(register floor (Fn [Float] Float))
|
||||
(register ceil (Fn [Float] Float))
|
||||
(register mod (Fn [Float Float] Float))
|
||||
(register copy (Fn [(Ref Float)] Float))
|
||||
(register abs (Fn [Float] Float))
|
||||
)
|
||||
|
@ -78,11 +78,16 @@ double Double__DIV_(double x, double y) { return x / y; }
|
||||
bool Double__LT_(double x, double y) { return x < y; }
|
||||
bool Double__GT_(double x, double y) { return x > y; }
|
||||
bool Double__EQ_(double x, double y) { return x == y; }
|
||||
double Double_neg(double x) { return -x; }
|
||||
|
||||
float Float__PLUS_(float x, float y) { return x + y; }
|
||||
float Float__MINUS_(float x, float y) { return x - y; }
|
||||
float Float__MUL_(float x, float y) { return x * y; }
|
||||
float Float__DIV_(float x, float y) { return x / y; }
|
||||
bool Float__LT_(float x, float y) { return x < y; }
|
||||
bool Float__GT_(float x, float y) { return x > y; }
|
||||
bool Float__EQ_(float x, float y) { return x == y; }
|
||||
double Float_neg(float x) { return -x; }
|
||||
|
||||
bool and(bool x, bool y) { return x && y; }
|
||||
bool or(bool x, bool y) { return x || y; }
|
||||
@ -245,6 +250,14 @@ double Double_from_MINUS_int(int x) {
|
||||
return (double)x;
|
||||
}
|
||||
|
||||
float Double_to_MINUS_float(double x) {
|
||||
return (float)x;
|
||||
}
|
||||
|
||||
double Double_from_MINUS_float(float x) {
|
||||
return (double)x;
|
||||
}
|
||||
|
||||
double Double_abs(double x) {
|
||||
return fabs(x);
|
||||
}
|
||||
@ -345,6 +358,90 @@ float Float_random_MINUS_between(float lower, float upper) {
|
||||
return lower + diff * r;
|
||||
}
|
||||
|
||||
float Float_abs(float x) {
|
||||
return fabs(x);
|
||||
}
|
||||
|
||||
float Float_acos(float x) {
|
||||
return acos(x);
|
||||
}
|
||||
|
||||
float Float_asin(float x) {
|
||||
return asin(x);
|
||||
}
|
||||
|
||||
float Float_atan(float x) {
|
||||
return atan(x);
|
||||
}
|
||||
|
||||
float Float_atan2(float y, float x) {
|
||||
return atan2(y, x);
|
||||
}
|
||||
|
||||
float Float_cos(float x) {
|
||||
return cos(x);
|
||||
}
|
||||
|
||||
float Float_cosh(float x) {
|
||||
return cosh(x);
|
||||
}
|
||||
|
||||
float Float_sin(float x) {
|
||||
return sin(x);
|
||||
}
|
||||
|
||||
float Float_sinh(float x) {
|
||||
return sinh(x);
|
||||
}
|
||||
|
||||
float Float_tanh(float x) {
|
||||
return tanh(x);
|
||||
}
|
||||
|
||||
float Float_exp(float x) {
|
||||
return exp(x);
|
||||
}
|
||||
|
||||
float Float_frexp(float x, int* exponent) {
|
||||
return frexp(x, exponent);
|
||||
}
|
||||
|
||||
float Float_ldexp(float x, int exponent) {
|
||||
return ldexp(x, exponent);
|
||||
}
|
||||
|
||||
float Float_log(float x) {
|
||||
return log(x);
|
||||
}
|
||||
|
||||
float Float_log10(float x) {
|
||||
return log10(x);
|
||||
}
|
||||
|
||||
float Float_modf(float x, float* integer) {
|
||||
return modf(x, (double*) integer);
|
||||
}
|
||||
|
||||
float Float_pow(float x, float y) {
|
||||
return pow(x, y);
|
||||
}
|
||||
|
||||
float Float_sqrt(float x) {
|
||||
return sqrt(x);
|
||||
}
|
||||
|
||||
float Float_ceil(float x) {
|
||||
return ceil(x);
|
||||
}
|
||||
|
||||
float Float_floor(float x) {
|
||||
return floor(x);
|
||||
}
|
||||
|
||||
float Float_mod(float x, float y) {
|
||||
return fmod(x, y);
|
||||
}
|
||||
|
||||
string Float_str(float x) {
|
||||
char *buffer = CARP_MALLOC(32);
|
||||
snprintf(buffer, 32, "%gf", x);
|
||||
|
@ -3,6 +3,12 @@
|
||||
|
||||
(defn main []
|
||||
(with-test test
|
||||
(assert-equal test
|
||||
-1.0
|
||||
(neg 1.0)
|
||||
"neg works as expected"
|
||||
Double.=
|
||||
Double.str)
|
||||
(assert-equal test
|
||||
0.0
|
||||
(acos 1.0)
|
138
test/float_math.carp
Normal file
138
test/float_math.carp
Normal file
@ -0,0 +1,138 @@
|
||||
(use Float)
|
||||
(use Test)
|
||||
|
||||
(defn main []
|
||||
(with-test test
|
||||
(assert-equal test
|
||||
-1.0f
|
||||
(neg 1.0f)
|
||||
"neg works as expected"
|
||||
Float.=
|
||||
Float.str)
|
||||
(assert-equal test
|
||||
0.0f
|
||||
(acos 1.0f)
|
||||
"acos works as expected"
|
||||
Float.=
|
||||
Float.str)
|
||||
(assert-equal test
|
||||
0.0f
|
||||
(asin 0.0f)
|
||||
"asin works as expected"
|
||||
Float.=
|
||||
Float.str)
|
||||
(assert-equal test
|
||||
0.0f
|
||||
(atan 0.0f)
|
||||
"atan works as expected"
|
||||
Float.=
|
||||
Float.str)
|
||||
(assert-equal test
|
||||
0.0f
|
||||
(atan2 0.0f 0.0f)
|
||||
"atan2 works as expected"
|
||||
Float.=
|
||||
Float.str)
|
||||
(assert-equal test
|
||||
1.0f
|
||||
(cos 0.0f)
|
||||
"cos works as expected"
|
||||
Float.=
|
||||
Float.str)
|
||||
(assert-equal test
|
||||
1.0f
|
||||
(cosh 0.0f)
|
||||
"cosh works as expected"
|
||||
Float.=
|
||||
Float.str)
|
||||
(assert-equal test
|
||||
1.0f
|
||||
(cosh 0.0f)
|
||||
"cosh works as expected"
|
||||
Float.=
|
||||
Float.str)
|
||||
(assert-equal test
|
||||
0.0f
|
||||
(sin 0.0f)
|
||||
"sin works as expected"
|
||||
Float.=
|
||||
Float.str)
|
||||
(assert-equal test
|
||||
0.0f
|
||||
(sinh 0.0f)
|
||||
"sinh works as expected"
|
||||
Float.=
|
||||
Float.str)
|
||||
(assert-equal test
|
||||
0.0f
|
||||
(tanh 0.0f)
|
||||
"tanh works as expected"
|
||||
Float.=
|
||||
Float.str)
|
||||
(assert-equal test
|
||||
1.0f
|
||||
(exp 0.0f)
|
||||
"exp works as expected"
|
||||
Float.approx
|
||||
Float.str)
|
||||
(assert-equal test
|
||||
8.0f
|
||||
(ldexp 2.0f 2)
|
||||
"ldexp works as expected"
|
||||
Float.=
|
||||
Float.str)
|
||||
(assert-equal test
|
||||
1.0f
|
||||
(log (Double.to-float Double.e))
|
||||
"log works as expected"
|
||||
Float.approx
|
||||
Float.str)
|
||||
(assert-equal test
|
||||
1.0f
|
||||
(log10 10.0f)
|
||||
"log10 works as expected"
|
||||
Float.=
|
||||
Float.str)
|
||||
(assert-equal test
|
||||
256.0f
|
||||
(pow 2.0f 8.0f)
|
||||
"pow works as expected"
|
||||
Float.=
|
||||
Float.str)
|
||||
(assert-equal test
|
||||
3.0f
|
||||
(sqrt 9.0f)
|
||||
"sqrt works as expected"
|
||||
Float.=
|
||||
Float.str)
|
||||
(assert-equal test
|
||||
2.0f
|
||||
(ceil 1.3f)
|
||||
"ceil works as expected"
|
||||
Float.=
|
||||
Float.str)
|
||||
(assert-equal test
|
||||
2.0f
|
||||
(abs -2.0f)
|
||||
"abs works as expected"
|
||||
Float.=
|
||||
Float.str)
|
||||
(assert-equal test
|
||||
1.0f
|
||||
(floor 1.9f)
|
||||
"floor works as expected"
|
||||
Float.=
|
||||
Float.str)
|
||||
(assert-equal test
|
||||
1.0f
|
||||
(floor 1.9f)
|
||||
"floor works as expected"
|
||||
Float.=
|
||||
Float.str)
|
||||
(assert-equal test
|
||||
0.3f
|
||||
(mod 9.3f 3.0f)
|
||||
"mod works as expected"
|
||||
Float.approx
|
||||
Float.str)
|
||||
(print-test-results test)))
|
Loading…
Reference in New Issue
Block a user