-
-
Notifications
You must be signed in to change notification settings - Fork 165
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
BigInt support #1461
Comments
For LuaJIT target (big int overflow example):
For Lua 5.0/5.1/5.2 this could only be supported by using/ It is a thing for 5.3+, just make sure to not mix things:
|
Lua 5.3+ lets you compare an int and a number (double), the issue with your example is that the literal "9223372036854775807.0" does not correspond to a representable double, the nearest double is 9223372036854775808.0:
But if you try with a integer that is exactly representable in a double (e.g., 2^52) then the comparison returns true:
|
@Cheatoid any Lua target would require a software implementation. Lua 5.3 introduced 64-bit integers, but not big integers, which are the subject of this proposal. Big integers are of an arbitrary size, operations with big integers do not overflow. |
That's what I thought you are proposing at first, but that didn't sound right since Lua by-standard doesn't have bigint support, except LuaJIT where you could theoretically implement that using a script. So for that reason, I thought this was about 64-bit integers. |
That is not a problem – Lua also lacks support for other TypeScript features, such as classes. The purpose of the aforementioned lualib is to polyfill this functionality with a software implementation that doesn't rely on a straightforward mapping between TypeScript and Lua language features. See the I'm not sure what you mean by "except LuaJIT where you could theoretically implement that using a script." A software implementation of big integers is possible to implement for all Lua dialects TSTL currently supports, including PUC Lua 5.0-5.4.
I'm not sure what you mean by "native extension." As I suggested in the proposal above, you can implement arithmetic on big integer objects via metatables. Similar arithmetic overloading is a well-known Lua feature, see e.g. Programming in Lua: Arithmetic Metamethods:
|
@viluon Sure... But scripted/software implementation is going to be slow. I could port it over to TSTL and publish it to npm so everyone could use it easier, but kinda busy right now, if you want this so badly, just drop the declare interface BigInt {
clone(this: BigInt): BigInt;
equals(this: BigInt, other: object): boolean;
toString(this: BigInt): string;
negate(this: BigInt): BigInt;
add(this: BigInt, other: BigInt): BigInt;
subtract(this: BigInt, other: BigInt): BigInt;
multiply(this: BigInt, other: BigInt): BigInt;
divide(this: BigInt, other: BigInt): BigInt;
}
export = {
(this: void, value: string): BigInt;
}; |
Currently, TSTL doesn't support JavaScript big integers, available since at least ECMAScript 2020. TypeScript supports the
bigint
type, big integer literals, and related features since version 3.2.Supporting big integers in TSTL would require implementing them in the lualib, using metatables to support arithmetic operations in generic code*. The compiler would need to turn big integer literals into invocations of the appropriate constructor.
*: Mixing
number
s andbigint
s in arithmetic is an error, but a function which works both onbigint
s andnumber
s, like the one below, could not be compiled to a single Lua function without big integer metatables (or costly dispatch viatype()
).The text was updated successfully, but these errors were encountered: