Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] fix misbehavior with trailing decimals #182

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ryu/s2d.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ enum Status s2d_n(const char * buffer, const int len, double * result) {
if ((c < '0') || (c > '9')) {
break;
}
if (m10digits >= 17) {
if (m10digits - (dotIndex != len ? dotIndex : 0) >= 17) {
return INPUT_TOO_LONG;
}
m10 = 10 * m10 + (c - '0');
Expand Down
2 changes: 1 addition & 1 deletion ryu/s2f.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ enum Status s2f_n(const char * buffer, const int len, float * result) {
if ((c < '0') || (c > '9')) {
break;
}
if (m10digits >= 9) {
if (m10digits - (dotIndex != len ? dotIndex : 0) >= 9) {
return INPUT_TOO_LONG;
}
m10 = 10 * m10 + (c - '0');
Expand Down
16 changes: 14 additions & 2 deletions ryu/tests/s2d_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include "ryu/ryu_parse.h"
#include "third_party/gtest/gtest.h"

#define EXPECT_S2D(a, b) { double value; EXPECT_EQ(SUCCESS, s2d(b, &value)); EXPECT_EQ(a, value); } while (0);
#define EXPECT_S2D(a, b) { double value; EXPECT_EQ(SUCCESS, s2d(b, &value)) << "str=" << b; EXPECT_EQ(a, value) << "str=" << b; } while (0);

TEST(S2dTest, BadInput) {
double value;
Expand Down Expand Up @@ -96,4 +96,16 @@ TEST(S2dTest, Issue173) {
EXPECT_S2D(2.2250738585072012e-308, "2.2250738585072012e-308");
EXPECT_S2D(2.2250738585072013e-308, "2.2250738585072013e-308");
EXPECT_S2D(2.2250738585072014e-308, "2.2250738585072014e-308");
}
}

TEST(S2dTest, TrailingDecimalZeros) {
EXPECT_S2D(1. , "1");
EXPECT_S2D(1. , "1.000");
EXPECT_S2D(1. , "1.0000000000000000");
EXPECT_S2D(1. , "1.00000000000000000");
EXPECT_S2D(1. , "1.000000000000000000"); // fail: INPUT_TOO_LONG
EXPECT_S2D(8388605., "8388605");
EXPECT_S2D(8388605., "8388605.000");
EXPECT_S2D(8388605., "8388605.0000000000");
EXPECT_S2D(8388605., "8388605.00000000000"); // Assertion failed: dist < 64, file C:\users\jpmag\_bazel_jpmag\lajl3dfu\execroot\__main__\ryu/d2s_intrinsics.h, line 58
}
12 changes: 11 additions & 1 deletion ryu/tests/s2f_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include "ryu/ryu_parse.h"
#include "third_party/gtest/gtest.h"

#define EXPECT_S2F(a, b) do { float value; EXPECT_EQ(SUCCESS, s2f(b, &value)); EXPECT_EQ(a, value); } while (0);
#define EXPECT_S2F(a, b) do { float value; EXPECT_EQ(SUCCESS, s2f(b, &value)) << "str=" << b; EXPECT_EQ(a, value) << "str=" << b; } while (0);

TEST(S2fTest, Basic) {
EXPECT_S2F(0.0f, "0");
Expand Down Expand Up @@ -53,3 +53,13 @@ TEST(S2fTest, TrailingZeros) {
EXPECT_S2F(50000004.0f, "50000002.5");
EXPECT_S2F(99999992.0f, "99999989.5");
}

TEST(S2fTest, TrailingDecimalZeros) {
EXPECT_S2F(1.f, "1");
EXPECT_S2F(1.f, "1.000");
EXPECT_S2F(1.f, "1.000000000");
EXPECT_S2F(1.f, "1.0000000000"); // SUCCESS, but wrong value: 1.4013e-45
EXPECT_S2F(8388605.f, "8388605");
EXPECT_S2F(8388605.f, "8388605.00");
EXPECT_S2F(8388605.f, "8388605.000"); // SUCCESS, but wrong value: 4.09364e+06
}