core: add Float.from-string and Double.from-string

This commit is contained in:
hellerve 2020-01-29 11:41:06 +01:00
parent 1cc751afbb
commit c0c31b6ad1
4 changed files with 24 additions and 2 deletions

View File

@ -189,6 +189,7 @@
(defmodule Float
(register str (Fn [Float] String))
(register format (Fn [&String Float] String))
(register from-string (λ [&String] Float))
)
(defmodule Long
@ -200,6 +201,7 @@
(defmodule Double
(register str (Fn [Double] String))
(register format (Fn [&String Double] String))
(register from-string (λ [&String] Double))
)
(defmodule Char

View File

@ -199,6 +199,10 @@ String Double_format(const String *s, double x) {
return buffer;
}
double Double_from_MINUS_string(const String *s) {
return strtod(*s, NULL);
}
String Float_str(float x) {
int size = snprintf(NULL, 0, "%gf", x) + 1;
String buffer = CARP_MALLOC(size);
@ -213,6 +217,10 @@ String Float_format(const String *str, float x) {
return buffer;
}
float Float_from_MINUS_string(const String *s) {
return strtof(*s, NULL);
}
String Int_str(int x) {
int size = snprintf(NULL, 0, "%d", x) + 1;
String buffer = CARP_MALLOC(size);

View File

@ -101,4 +101,10 @@
0l
(to-bytes 0.0)
"to-bytes works as expected II"
))
)
(assert-equal test
10.3
(from-string "10.3")
"from-string works as expected"
)
)

View File

@ -107,4 +107,10 @@
0
(to-bytes 0.0f)
"to-bytes works as expected II"
))
)
(assert-equal test
10.3f
(from-string "10.3")
"from-string works as expected"
)
)