aboutsummaryrefslogtreecommitdiffstats
path: root/src/stdlib.c
diff options
context:
space:
mode:
authorClyne Sullivan <tullivan99@gmail.com>2018-03-08 12:17:58 -0500
committerClyne Sullivan <tullivan99@gmail.com>2018-03-08 12:17:58 -0500
commitc27af361feeca0e7cbfa51f5589b09d0c110e9c9 (patch)
treebde1d2cb0924f2cbd9153a9a23b3911b0adfc4de /src/stdlib.c
parent94a62e8a7b5fcebf94bb0ae7760b9720e8db2715 (diff)
graphing perfection
Diffstat (limited to 'src/stdlib.c')
-rw-r--r--src/stdlib.c40
1 files changed, 37 insertions, 3 deletions
diff --git a/src/stdlib.c b/src/stdlib.c
index ec67ff3..e34d2a0 100644
--- a/src/stdlib.c
+++ b/src/stdlib.c
@@ -67,15 +67,49 @@ float strtof(const char *s, char **endptr)
{
(void)s;
(void)endptr;
- return 0.0f;
+
+ float res = 0.0f;
+ char neg = 0;
+ unsigned int i = 0;
+
+ if (s[0] == '-') {
+ neg = 1;
+ i++;
+ }
+
+ for (; isdigit(s[i]); i++) {
+ res *= 10.0f;
+ res += (s[i] - '0');
+ }
+
+ if (s[i] != '.')
+ goto end;
+
+ float div = 0.1f;
+ for (i++; isdigit(s[i]); i++) {
+ res += div * (s[i] - '0');
+ div /= 10.0f;
+ }
+
+end:
+ return (neg == 0) ? res : -res;
}
int atoi(const char *s)
{
+ unsigned int i = 0;
+ char neg = 0;
int n = 0;
- for (unsigned int i = 0; isdigit(s[i]); i++) {
+
+ if (s[0] == '-') {
+ neg = 1;
+ i = 1;
+ }
+
+ for (; isdigit(s[i]); i++) {
n *= 10;
n += s[i] - '0';
}
- return n;
+
+ return (neg == 0) ? n : -n;
}