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

Number fixes #89

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
15 changes: 12 additions & 3 deletions src/main/java/cc/tweaked/cobalt/internal/string/NumberParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,16 @@ private static double scanLong(int base, byte[] bytes, int start, int end) {
}

if (digit >= base) return Double.NaN;
x = x * base + digit;

if (base == 10) {
try {
x = Math.addExact(Math.multiplyExact(x, base), digit);
} catch (ArithmeticException e) {
return Double.NaN;
}
} else {
x = x * base + digit;
}
}
return x;
}
Expand Down Expand Up @@ -119,7 +128,7 @@ private static double scanDouble(byte[] bytes, int start, int end) {
}

private static double scanHexDouble(byte[] bytes, int index, int end) {
long result = 0; // The mantissa
double result = 0; // The mantissa
int exponent = 0;

int sigDigits = 0, nonSigDigits = 0; // Number of significant digits and non-significant digits (leading 0s).
Expand Down Expand Up @@ -187,6 +196,6 @@ private static double scanHexDouble(byte[] bytes, int index, int end) {
exponent += givenExponent;
}

return Math.scalb((double) result, exponent);
return Math.scalb(result, exponent);
}
}
2 changes: 1 addition & 1 deletion src/main/java/org/squiddev/cobalt/LuaDouble.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public String toString() {
@Override
public LuaString checkLuaString() {
long l = (long) v;
if (l == v) return ValueFactory.valueOf(Long.toString(l));
if (l == v && l != Long.MAX_VALUE) return ValueFactory.valueOf(Long.toString(l));
if (Double.isNaN(v)) return STR_NAN;
if (Double.isInfinite(v)) return v < 0 ? STR_NEGINF : STR_POSINF;

Expand Down
4 changes: 4 additions & 0 deletions src/test/resources/spec/base_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ describe("The base library", function()
expect('' .. 12):eq('12') expect(12.0 .. ''):eq('12')
expect(tostring(-1203 + 0.0)):eq("-1203")
end)

it("correctly handles 2^63", function()
expect(tostring(math.floor(2^63))):not_equals("9223372036854775807")
end)
end)

it("tables", function() expect(tostring {}):str_match('^table:') end)
Expand Down
8 changes: 8 additions & 0 deletions src/test/resources/spec/parser_spec.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
describe("The Lua lexer/parser", function()
describe("numbers", function()
local load = loadstring or load
it("only wraps around hexadecimal numerals with neither an exponent nor radix point", function()
-- See https://github.com/cc-tweaked/Cobalt/issues/88
expect(tonumber(("9"):rep(19))):eq(1e19)
expect(tonumber("0xffffffffffffffff")):eq(-1)
expect(tonumber("0xffffffffffffffff.0")):eq(2^64)
expect(tonumber("0xffffffffffffffffp0")):eq(2^64)
end)

it("rejects numbers ending in 'f' or 'd'", function()
-- Java's Double.parseDouble accepts "2f" and "2d" as a valid number. Make sure we don't.
-- See https://github.com/SquidDev/Cobalt/issues/71
Expand Down
Loading