Closed Mister7F closed 4 years ago
JavaScript does not distinguish between int and float; everything is a float, some just happen to be rounded. In Python there are two types, but ints resolve to float automatically on operations like division. It will be very hard to make Python ints be represented as BigInt, and will have all sorts of consequences, like Math
functions won't work as you point out.
Therefore, I don't think this is a problem that PScript should (try to) solve. Instead, if you need integer numbers that do not fit in float64, just use BigInt in that usecase.
@almarklein Sorry for the late response, maybe can you use something like this to emulate python number inside pscript...
I think this project is really awesome, but it would be even better if it felt like a real python :)
(And can develop it on my free time and make a pull request if you are interested)
class PyNumber {
constructor(value) {
this.isInt = !(Number(value) % 1);
this.value = this.isInt ? BigInt(value) : value;
}
add(number) {
if (number.isInt && this.isInt) {
return new PyNumber(this.value + number.value);
} else {
return new PyNumber(Number(this.value) + Number(number.value));
}
}
pow (number) {
if (number.isInt && this.isInt) {
return new PyNumber(this.value ** number.value);
} else {
return new PyNumber(Math.pow(Number(this.value), Number(number.value)));
}
}
}
a = new PyNumber(1);
b = new PyNumber(2);
console.log(a.add(b));
a = new PyNumber(1.3);
b = new PyNumber(2);
console.log(a.add(b));
a = new PyNumber(2);
b = new PyNumber(2.4);
console.log(a.add(b));
a = new PyNumber(2);
b = new PyNumber(3);
console.log(a.pow(b));
a = new PyNumber(2.3);
b = new PyNumber(2);
console.log(a.pow(b));
a = new PyNumber(2);
b = new PyNumber(2.4);
console.log(a.pow(b));
This could be a solution for your use-case, and if it works for you please go ahead :) But it's not suitable as a general solution because it generates a lot of overhead, making all operations with numbers slower.
I think this project is really awesome, but it would be even better if it felt like a real python :)
Yeah ... the problem is that PScript quite feels like Python, but it's not. We can make it feel more like Python, but it still won't be, so there will always be surprises like this ...
Ok, thank you for your answer :)
Hello, There's a problem with big integer. In Javascript, when an integer become to large, it is transformed to a float. But in python the integers have no limit.
Maybe can you use big int in JS instead of simple integer ? (But division, power (and other
Math
functions) will be broken)