Skip to content

Commit

Permalink
karm-io: Added support for "-." in atof.
Browse files Browse the repository at this point in the history
  • Loading branch information
sleepy-monax committed Dec 18, 2024
1 parent 1eda808 commit 4202ec6
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/libs/karm-io/aton.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,15 @@ static inline Opt<f64> atof(_SScan<E> &s, AtoxOptions const &options = {}) {
f64 fpart = 0.0;
i64 exp = 0;

if (s.peek(0) != '.' or not _parseDigit(s.peek(1), options)) {
ipart = try$(atoi(s, options));
bool negDot = s.skip("-.");

if (not negDot) {
if (s.peek(0) != '.' or not _parseDigit(s.peek(1), options)) {
ipart = try$(atoi(s, options));
}
}

if (s.skip('.')) {
if (negDot or s.skip('.')) {
f64 multiplier = (1.0 / options.base);
while (not s.ended()) {
auto maybeDigit = _nextDigit(s, options);
Expand All @@ -115,6 +119,8 @@ static inline Opt<f64> atof(_SScan<E> &s, AtoxOptions const &options = {}) {
exp = maybeExp.unwrap();
}

if (negDot)
return -fpart * pow(options.base, exp);
if (ipart < 0)
return ipart - fpart * pow(options.base, exp);
return ipart + fpart * pow(options.base, exp);
Expand All @@ -139,7 +145,7 @@ static inline Opt<isize> atoi(_Str<E> str, AtoxOptions const &options = {}) {
template <StaticEncoding E>
static inline Opt<f64> atof(_Str<E> str, AtoxOptions const &options = {}) {
auto s = _SScan<E>(str);
return stof(s, options);
return atof(s, options);
}

#endif
Expand Down
52 changes: 52 additions & 0 deletions src/libs/karm-io/tests/test-aton.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <karm-io/aton.h>
#include <karm-math/funcs.h>
#include <karm-test/macros.h>

namespace Karm::Io::Tests {

test$("atoi") {
expectEq$(Io::atoi("0"s), 0);
expectEq$(Io::atoi("1"s), 1);
expectEq$(Io::atoi("10"s), 10);
expectEq$(Io::atoi("100"s), 100);

expectEq$(Io::atoi("-1"s), -1);
expectEq$(Io::atoi("-10"s), -10);
expectEq$(Io::atoi("-100"s), -100);

return Ok();
}

test$("atof") {
expect$(Math::epsilonEq(try$(Io::atof("0.0"s)), 0.0));
expect$(Math::epsilonEq(try$(Io::atof("0.1"s)), 0.1));
expect$(Math::epsilonEq(try$(Io::atof("0.5"s)), 0.5));

expect$(Math::epsilonEq(try$(Io::atof(".0"s)), 0.0));
expect$(Math::epsilonEq(try$(Io::atof(".1"s)), 0.1));
expect$(Math::epsilonEq(try$(Io::atof(".5"s)), 0.5));

expect$(Math::epsilonEq(try$(Io::atof("0"s)), 0.));
expect$(Math::epsilonEq(try$(Io::atof("1"s)), 1.));
expect$(Math::epsilonEq(try$(Io::atof("5"s)), 5.));

expect$(Math::epsilonEq(try$(Io::atof("0."s)), 0.));
expect$(Math::epsilonEq(try$(Io::atof("1."s)), 1.));
expect$(Math::epsilonEq(try$(Io::atof("5."s)), 5.));

expect$(Math::epsilonEq(try$(Io::atof("-0"s)), -0.));
expect$(Math::epsilonEq(try$(Io::atof("-1"s)), -1.));
expect$(Math::epsilonEq(try$(Io::atof("-5"s)), -5.));

expect$(Math::epsilonEq(try$(Io::atof("-0."s)), -0.));
expect$(Math::epsilonEq(try$(Io::atof("-1."s)), -1.));
expect$(Math::epsilonEq(try$(Io::atof("-5."s)), -5.));

expect$(Math::epsilonEq(try$(Io::atof("-.0"s)), -.0));
expect$(Math::epsilonEq(try$(Io::atof("-.1"s)), -.1));
expect$(Math::epsilonEq(try$(Io::atof("-.5"s)), -.5));

return Ok();
}

} // namespace Karm::Io::Tests

0 comments on commit 4202ec6

Please sign in to comment.