Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
triazo
/
kernel
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
3
Merge Requests
0
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
97402ab3
authored
Nov 10, 2015
by
malcolm
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
Added atof
parent
bb652938
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
49 additions
and
0 deletions
src/string.c
src/string.h
src/string.c
View file @
97402ab3
...
...
@@ -108,3 +108,51 @@ int stoi(char *str)
return
sign
*
res
;
}
double
atof
(
char
*
num
)
{
if
(
!
num
||
!*
num
)
return
0
;
double
integerPart
=
0
;
double
fractionPart
=
0
;
int
divisorForFraction
=
1
;
int
sign
=
1
;
bool
inFraction
=
false
;
if
(
*
num
==
'-'
)
{
++
num
;
sign
=
-
1
;
}
else
if
(
*
num
==
'+'
)
{
++
num
;
}
while
(
*
num
!=
'\0'
)
{
if
(
*
num
>=
'0'
&&
*
num
<=
'9'
)
{
if
(
inFraction
)
{
fractionPart
=
fractionPart
*
10
+
(
*
num
-
'0'
);
divisorForFraction
*=
10
;
}
else
{
integerPart
=
integerPart
*
10
+
(
*
num
-
'0'
);
}
}
else
if
(
*
num
==
'.'
)
{
if
(
inFraction
)
return
sign
*
(
integerPart
+
fractionPart
/
divisorForFraction
);
else
inFraction
=
true
;
}
else
{
return
sign
*
(
integerPart
+
fractionPart
/
divisorForFraction
);
}
++
num
;
}
return
sign
*
(
integerPart
+
fractionPart
/
divisorForFraction
);
}
src/string.h
View file @
97402ab3
...
...
@@ -9,4 +9,5 @@ int strcmp(const char* s1, const char* s2);
int
strncmp
(
const
char
*
s1
,
const
char
*
s2
,
size_t
n
);
int
atoi
(
char
*
p
);
int
stoi
(
char
*
str
);
double
atof
(
char
*
num
);
#endif
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment