-
Notifications
You must be signed in to change notification settings - Fork 639
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's #347
Comments
Really interesting feature. I'd enjoy working on this. |
But maybe we can make it support |
I tried to implement it with function split(numeric: string): string[] {
const [_, integer, float] = /(\d+)\.?(\d+)?/.exec(numeric);
return [integer || '0', float || '0'];
}
export type Action = 'plus' | 'minus';
export class Big {
constructor(private value: string) {}
public calc(a: string, b: string, type: Action): string {
let [integer1, float1] = split(a);
let [integer2, float2] = split(b);
const maxIntegerLen = Math.max(integer1.length, integer2.length);
const maxFloatLen = Math.max(float1.length, float2.length);
integer1 = integer1.padStart(maxIntegerLen, '0');
float1 = float1.padEnd(maxFloatLen, '0');
integer2 = integer2.padStart(maxIntegerLen, '0');
float2 = float2.padEnd(maxFloatLen, '0');
const srcBigInt = BigInt(integer1 + float1);
const destBitInt = BigInt(integer2 + float2);
const maxLength = Math.max(
(integer1 + float1).length,
(integer2 + float2).length
);
let result = '';
switch (type) {
// a + b
case 'plus':
result = (srcBigInt + destBitInt).toString();
break;
// a - b
case 'minus':
result = (srcBigInt - destBitInt).toString();
break;
}
result = result.padStart(maxLength, '0');
const integer = result.substr(0, maxIntegerLen);
const float = result.substr(maxIntegerLen);
this.value = integer + (+float ? '.' + float : '').replace(/0$/, '');
return result;
}
public plus(numeric: string) {
this.calc(this.value, numeric, 'plus');
return this;
}
public minus(numeric: string) {
this.calc(this.value, numeric, 'minus');
return this;
}
public toString() {
return this.value;
}
}
// calculate float
console.log(new Big('1.2').plus('0.2')); // Big { value: "1.4" }
console.log(new Big('1.234').plus('0.2')); // Big { value: "1.434" }
console.log(new Big('0.3').minus('0.1')); // Big { value: "0.2" }
console.log(new Big('100.1').plus('1000.123')); // Big { value: "1100.223" }
// calculate normal integer
console.log(new Big('1').plus('2')); // Big { value: "3" }
console.log(new Big('15').plus('21')); // Big { value: "36" }
console.log(new Big('1001').minus('1')); // Big { value: "1000" }
console.log(new Big('1001').plus('999')); // Big { value: "2000" }
// calculate big integer
const maxInteger = Number.MAX_SAFE_INTEGER; // 9007199254741091
console.log(new Big(maxInteger + '').plus('1.1')); // Big { value: "9007199254741092.1" } There is a problem with this, do we need to write a library of high-precision calculations in std? I think there should be. High-precision calculations make deno more widely used and can be applied to If @ry agree with this, I am happy to work on it. Updatethis approach is not feasible. Ignore it. |
@axetroy what about make |
If you only need integer calculations, why not use |
Is this available in v8? |
@hayd the V8 version Deno using already comes with |
True. |
Using import { Big } from 'https://deno.land/x/math/mod.ts';
new Big(0.1).plus(0.2).toString(); // '0.3'
console.log(0.1 + 0.2); // 0.30000000000000004 |
Description
I'm working in the blockchain world as a developer and we have to handle security-sensitive and FinTech related data. This is the reason why Deno could be interesting for the usage with DApp's.
We have currently a discussion over several projects because of the coming BigInt and the current solutions to represent BigIntegers in JS.
Feature Request
My idea would be that Deno could provide more real types with their provided std lib.
The types could be implemented with WASM or JS.
Possible types
References
The text was updated successfully, but these errors were encountered: