From c5d83cb079e9ceae34c5d854ba4eefef28d2091e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ingo=20B=C3=BCrk?= Date: Mon, 21 Apr 2014 00:32:05 +0200 Subject: [PATCH 1/3] #1659: Added BigInteger.js (flattened commit) --- CONTRIBUTORS.md | 1 + bigInteger/bigInteger-tests.ts | 95 +++++++++++++++++++ bigInteger/bigInteger.d.ts | 161 +++++++++++++++++++++++++++++++++ 3 files changed, 257 insertions(+) create mode 100644 bigInteger/bigInteger-tests.ts create mode 100644 bigInteger/bigInteger.d.ts diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 54ae0cdc8..d68e3061c 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -22,6 +22,7 @@ All definitions files include a header with the author and editors, so at some p * [Atom](https://atom.io/) (by [vvakame](https://github.com/vvakame)) * [Backbone.js](http://backbonejs.org/) (by [Boris Yankov](https://github.com/borisyankov)) * [Backbone Relational](http://backbonerelational.org/) (by [Eirik Hoem](https://github.com/eirikhm)) +* [BigInteger](https://github.com/peterolson/BigInteger.js) (by [Ingo Bürk](https://github.com/Airblader)) * [BigScreen](http://brad.is/coding/BigScreen/) (by [Douglas Eichelberger](https://github.com/dduugg)) * [Bluebird](https://github.com/petkaantonov/bluebird) (by [Bart van der Schoor](https://github.com/Bartvds)) * [Bootbox](https://github.com/makeusabrew/bootbox) (by [Vincent Bortone](https://github.com/vbortone/)) diff --git a/bigInteger/bigInteger-tests.ts b/bigInteger/bigInteger-tests.ts new file mode 100644 index 000000000..7940b8629 --- /dev/null +++ b/bigInteger/bigInteger-tests.ts @@ -0,0 +1,95 @@ +/// + +// constructor tests +var noArgument = bigInt(), + numberArgument = bigInt( 93 ), + stringArgument = bigInt( "75643564363473453456342378564387956906736546456235345" ), + bigIntArgument = bigInt( noArgument ); + +// method tests +var x = bigInt(), + isBigInteger: BigInteger, + isNumber: number, + isBoolean: boolean, + isString: string, + isDivmod: { + quotient: BigInteger; + remainder: BigInteger; + }; + +isBigInteger = x.abs(); + +isBigInteger = x.add( 0 ); +isBigInteger = x.add( x ); + +isBigInteger = x.compare( 0 ); +isBigInteger = x.compare( x ); + +isBigInteger = x.compareAbs( 0 ); +isBigInteger = x.compareAbs( x ); + +isBigInteger = x.divide( 0 ); +isBigInteger = x.divide( x ); + +isDivmod = x.divmod( 0 ); +isDivmod = x.divmod( x ); + +isBoolean = x.equals( 0 ); +isBoolean = x.equals( x ); + +isBoolean = x.greater( 0 ); +isBoolean = x.greater( x ); + +isBoolean = x.greaterOrEquals( 0 ); +isBoolean = x.greaterOrEquals( x ); + +isBoolean = x.isEven(); + +isBoolean = x.isNegative(); + +isBoolean = x.isOdd(); + +isBoolean = x.isPositive(); + +isBoolean = x.lesser( 0 ); +isBoolean = x.lesser( x ); + +isBoolean = x.lesserOrEquals( 0 ); +isBoolean = x.lesserOrEquals( x ); + +isBigInteger = x.minus( 0 ); +isBigInteger = x.minus( x ); + +isBigInteger = x.mod( 0 ); +isBigInteger = x.mod( x ); + +isBigInteger = x.multiply( 0 ); +isBigInteger = x.multiply( x ); + +isBigInteger = x.next(); + +isBoolean = x.notEquals( 0 ); +isBoolean = x.notEquals( x ); + +isBigInteger = x.over( 0 ); +isBigInteger = x.over( x ); + +isBigInteger = x.plus( 0 ); +isBigInteger = x.plus( x ); + +isBigInteger = x.pow( 0 ); +isBigInteger = x.pow( x ); + +isBigInteger = x.prev(); + +isBigInteger = x.subtract( 0 ); +isBigInteger = x.subtract( x ); + +isBigInteger = x.times( 0 ); +isBigInteger = x.times( x ); + +isNumber = x.toJSNumber(); + +isString = x.toString(); + +isNumber = x.valueOf(); \ No newline at end of file diff --git a/bigInteger/bigInteger.d.ts b/bigInteger/bigInteger.d.ts new file mode 100644 index 000000000..ed03fc5ea --- /dev/null +++ b/bigInteger/bigInteger.d.ts @@ -0,0 +1,161 @@ +// Type definitions for BigInteger.js +// Project: https://github.com/peterolson/BigInteger.js +// Definitions by: Ingo Bürk +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +interface BigInteger { + /** Returns the absolute value of a bigInt. */ + abs(): BigInteger; + + /** Performs addition */ + add( number: number ): BigInteger; + /** Performs addition */ + add( number: BigInteger ): BigInteger; + + /** Alias for the add method. */ + plus( number: number ): BigInteger; + /** Alias for the add method. */ + plus( number: BigInteger ): BigInteger; + + /** Alias for the subtract method. */ + minus( number: number ): BigInteger; + /** Alias for the subtract method. */ + minus( number: BigInteger ): BigInteger; + + /** Performs subtraction. */ + subtract( number: number ): BigInteger; + /** Performs subtraction. */ + subtract( number: BigInteger ): BigInteger; + + /** Performs multiplication. */ + multiply( number: number ): BigInteger; + /** Performs multiplication. */ + multiply( number: BigInteger ): BigInteger; + + /** Alias for the multiply method. */ + times( number: number ): BigInteger; + /** Alias for the multiply method. */ + times( number: BigInteger ): BigInteger; + + /** Performs integer division, disregarding the remainder. */ + divide( number: number ): BigInteger; + /** Performs integer division, disregarding the remainder. */ + divide( number: BigInteger ): BigInteger; + + /** Alias for the divide method. */ + over( number: number ): BigInteger; + /** Alias for the divide method. */ + over( number: BigInteger ): BigInteger; + + /** Performs exponentiation. If the exponent is less than 0, pow returns 0. bigInt.zero.pow(0) returns 1. */ + pow( number: number ): BigInteger; + /** Performs exponentiation. If the exponent is less than 0, pow returns 0. bigInt.zero.pow(0) returns 1. */ + pow( number: BigInteger ): BigInteger; + + /** Adds one to the number. */ + next(): BigInteger; + + /** Subtracts one from the number. */ + prev(): BigInteger; + + /** Performs division and returns the remainder, disregarding the quotient. The sign of the remainder will match the sign of the dividend. */ + mod( number: number ): BigInteger; + /** Performs division and returns the remainder, disregarding the quotient. The sign of the remainder will match the sign of the dividend. */ + mod( number: BigInteger ): BigInteger; + + /** Performs division and returns an object with two properties: quotient and remainder. The sign of the remainder will match the sign of the dividend. */ + divmod( number: number ): { quotient: BigInteger; remainder: BigInteger }; + /** Performs division and returns an object with two properties: quotient and remainder. The sign of the remainder will match the sign of the dividend. */ + divmod( number: BigInteger ): { quotient: BigInteger; remainder: BigInteger }; + + /** Checks if the first number is greater than the second. */ + greater( number: number ): boolean; + /** Checks if the first number is greater than the second. */ + greater( number: BigInteger ): boolean; + + /** Checks if the first number is greater than or equal to the second. */ + greaterOrEquals( number: number ): boolean; + /** Checks if the first number is greater than or equal to the second. */ + greaterOrEquals( number: BigInteger ): boolean; + + /** Checks if the first number is lesser than the second. */ + lesser( number: number ): boolean; + /** Checks if the first number is lesser than the second. */ + lesser( number: BigInteger ): boolean; + + /** Checks if the first number is less than or equal to the second. */ + lesserOrEquals( number: number ): boolean; + /** Checks if the first number is less than or equal to the second. */ + lesserOrEquals( number: BigInteger ): boolean; + + /** Returns true if the number is even, false otherwise. */ + isEven(): boolean; + + /** Returns true if the number is odd, false otherwise. */ + isOdd(): boolean; + + /** Return true if the number is positive, false otherwise. Returns true for 0 and false for -0. */ + isPositive(): boolean; + + /** Returns true if the number is negative, false otherwise. Returns false for 0 and true for -0. */ + isNegative(): boolean; + + /** + * Performs a comparison between two numbers. If the numbers are equal, it returns 0. + * If the first number is greater, it returns 1. If the first number is lesser, it returns -1. + */ + compare( number: number ): BigInteger; + /** + * Performs a comparison between two numbers. If the numbers are equal, it returns 0. + * If the first number is greater, it returns 1. If the first number is lesser, it returns -1. + */ + compare( number: BigInteger ): BigInteger; + + /** Performs a comparison between the absolute value of two numbers. */ + compareAbs( number: number ): BigInteger; + /** Performs a comparison between the absolute value of two numbers. */ + compareAbs( number: BigInteger ): BigInteger; + + /** Checks if two numbers are equal. */ + equals( number: number ): boolean; + /** Checks if two numbers are equal. */ + equals( number: BigInteger ): boolean; + + /** Checks if two numbers are not equal. */ + notEquals( number: number ): boolean; + /** Checks if two numbers are not equal. */ + notEquals( number: BigInteger ): boolean; + + /** Converts a bigInt into a native Javascript number. Loses precision for numbers outside the range. */ + toJSNumber(): number; + + /** Converts a bigInt to a string. */ + toString(): string; + + /** Converts a bigInt to a native Javascript number. This override allows you to use native arithmetic operators without explicit conversion. */ + valueOf(): number; +} + +interface BigIntegerStatic { + /** Equivalent to bigInt(1) */ + one: BigInteger; + /** Equivalent to bigInt(0) */ + zero: BigInteger; + /** Equivalent to bigInt(-1) */ + minusOne: BigInteger; + + /** Equivalent to bigInt(0) */ + (): BigInteger; + /** Parse a Javascript number into a bigInt */ + ( number: number ): BigInteger; + /** Parse a string into a bigInt */ + ( string: string ): BigInteger; + /** no-op */ + ( bigInt: BigInteger ): BigInteger; +} + +declare var bigInt: BigIntegerStatic; + +declare module "BigInteger" { + export = bigInt; +} \ No newline at end of file From 6416531210d6a22e582214a5f683334469c0970d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ingo=20B=C3=BCrk?= Date: Mon, 21 Apr 2014 00:42:58 +0200 Subject: [PATCH 2/3] #2073 BigInteger.js: added string signatures for all methods --- bigInteger/bigInteger-tests.ts | 19 ++++++++++++++++ bigInteger/bigInteger.d.ts | 41 ++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/bigInteger/bigInteger-tests.ts b/bigInteger/bigInteger-tests.ts index 7940b8629..225b2118d 100644 --- a/bigInteger/bigInteger-tests.ts +++ b/bigInteger/bigInteger-tests.ts @@ -21,27 +21,35 @@ isBigInteger = x.abs(); isBigInteger = x.add( 0 ); isBigInteger = x.add( x ); +isBigInteger = x.add( "100" ); isBigInteger = x.compare( 0 ); isBigInteger = x.compare( x ); +isBigInteger = x.compare( "100" ); isBigInteger = x.compareAbs( 0 ); isBigInteger = x.compareAbs( x ); +isBigInteger = x.compareAbs( "100" ); isBigInteger = x.divide( 0 ); isBigInteger = x.divide( x ); +isBigInteger = x.divide( "100" ); isDivmod = x.divmod( 0 ); isDivmod = x.divmod( x ); +isDivmod = x.divmod( "100" ); isBoolean = x.equals( 0 ); isBoolean = x.equals( x ); +isBoolean = x.equals( "100" ); isBoolean = x.greater( 0 ); isBoolean = x.greater( x ); +isBoolean = x.greater( "100" ); isBoolean = x.greaterOrEquals( 0 ); isBoolean = x.greaterOrEquals( x ); +isBoolean = x.greaterOrEquals( "100" ); isBoolean = x.isEven(); @@ -53,40 +61,51 @@ isBoolean = x.isPositive(); isBoolean = x.lesser( 0 ); isBoolean = x.lesser( x ); +isBoolean = x.lesser( "100" ); isBoolean = x.lesserOrEquals( 0 ); isBoolean = x.lesserOrEquals( x ); +isBoolean = x.lesserOrEquals( "100" ); isBigInteger = x.minus( 0 ); isBigInteger = x.minus( x ); +isBigInteger = x.minus( "100" ); isBigInteger = x.mod( 0 ); isBigInteger = x.mod( x ); +isBigInteger = x.mod( "100" ); isBigInteger = x.multiply( 0 ); isBigInteger = x.multiply( x ); +isBigInteger = x.multiply( "100" ); isBigInteger = x.next(); isBoolean = x.notEquals( 0 ); isBoolean = x.notEquals( x ); +isBoolean = x.notEquals( "100" ); isBigInteger = x.over( 0 ); isBigInteger = x.over( x ); +isBigInteger = x.over( "100" ); isBigInteger = x.plus( 0 ); isBigInteger = x.plus( x ); +isBigInteger = x.plus( "100" ); isBigInteger = x.pow( 0 ); isBigInteger = x.pow( x ); +isBigInteger = x.pow( "100" ); isBigInteger = x.prev(); isBigInteger = x.subtract( 0 ); isBigInteger = x.subtract( x ); +isBigInteger = x.subtract( "100" ); isBigInteger = x.times( 0 ); isBigInteger = x.times( x ); +isBigInteger = x.times( "100" ); isNumber = x.toJSNumber(); diff --git a/bigInteger/bigInteger.d.ts b/bigInteger/bigInteger.d.ts index ed03fc5ea..a98fad7e7 100644 --- a/bigInteger/bigInteger.d.ts +++ b/bigInteger/bigInteger.d.ts @@ -11,46 +11,64 @@ interface BigInteger { add( number: number ): BigInteger; /** Performs addition */ add( number: BigInteger ): BigInteger; + /** Performs addition */ + add( number: string ): BigInteger; /** Alias for the add method. */ plus( number: number ): BigInteger; /** Alias for the add method. */ plus( number: BigInteger ): BigInteger; + /** Alias for the add method. */ + plus( number: string ): BigInteger; /** Alias for the subtract method. */ minus( number: number ): BigInteger; /** Alias for the subtract method. */ minus( number: BigInteger ): BigInteger; + /** Alias for the subtract method. */ + minus( number: string ): BigInteger; /** Performs subtraction. */ subtract( number: number ): BigInteger; /** Performs subtraction. */ subtract( number: BigInteger ): BigInteger; + /** Performs subtraction. */ + subtract( number: string ): BigInteger; /** Performs multiplication. */ multiply( number: number ): BigInteger; /** Performs multiplication. */ multiply( number: BigInteger ): BigInteger; + /** Performs multiplication. */ + multiply( number: string ): BigInteger; /** Alias for the multiply method. */ times( number: number ): BigInteger; /** Alias for the multiply method. */ times( number: BigInteger ): BigInteger; + /** Alias for the multiply method. */ + times( number: string ): BigInteger; /** Performs integer division, disregarding the remainder. */ divide( number: number ): BigInteger; /** Performs integer division, disregarding the remainder. */ divide( number: BigInteger ): BigInteger; + /** Performs integer division, disregarding the remainder. */ + divide( number: string ): BigInteger; /** Alias for the divide method. */ over( number: number ): BigInteger; /** Alias for the divide method. */ over( number: BigInteger ): BigInteger; + /** Alias for the divide method. */ + over( number: string ): BigInteger; /** Performs exponentiation. If the exponent is less than 0, pow returns 0. bigInt.zero.pow(0) returns 1. */ pow( number: number ): BigInteger; /** Performs exponentiation. If the exponent is less than 0, pow returns 0. bigInt.zero.pow(0) returns 1. */ pow( number: BigInteger ): BigInteger; + /** Performs exponentiation. If the exponent is less than 0, pow returns 0. bigInt.zero.pow(0) returns 1. */ + pow( number: string ): BigInteger; /** Adds one to the number. */ next(): BigInteger; @@ -62,31 +80,43 @@ interface BigInteger { mod( number: number ): BigInteger; /** Performs division and returns the remainder, disregarding the quotient. The sign of the remainder will match the sign of the dividend. */ mod( number: BigInteger ): BigInteger; + /** Performs division and returns the remainder, disregarding the quotient. The sign of the remainder will match the sign of the dividend. */ + mod( number: string ): BigInteger; /** Performs division and returns an object with two properties: quotient and remainder. The sign of the remainder will match the sign of the dividend. */ divmod( number: number ): { quotient: BigInteger; remainder: BigInteger }; /** Performs division and returns an object with two properties: quotient and remainder. The sign of the remainder will match the sign of the dividend. */ divmod( number: BigInteger ): { quotient: BigInteger; remainder: BigInteger }; + /** Performs division and returns an object with two properties: quotient and remainder. The sign of the remainder will match the sign of the dividend. */ + divmod( number: string ): { quotient: BigInteger; remainder: BigInteger }; /** Checks if the first number is greater than the second. */ greater( number: number ): boolean; /** Checks if the first number is greater than the second. */ greater( number: BigInteger ): boolean; + /** Checks if the first number is greater than the second. */ + greater( number: string ): boolean; /** Checks if the first number is greater than or equal to the second. */ greaterOrEquals( number: number ): boolean; /** Checks if the first number is greater than or equal to the second. */ greaterOrEquals( number: BigInteger ): boolean; + /** Checks if the first number is greater than or equal to the second. */ + greaterOrEquals( number: string ): boolean; /** Checks if the first number is lesser than the second. */ lesser( number: number ): boolean; /** Checks if the first number is lesser than the second. */ lesser( number: BigInteger ): boolean; + /** Checks if the first number is lesser than the second. */ + lesser( number: string ): boolean; /** Checks if the first number is less than or equal to the second. */ lesserOrEquals( number: number ): boolean; /** Checks if the first number is less than or equal to the second. */ lesserOrEquals( number: BigInteger ): boolean; + /** Checks if the first number is less than or equal to the second. */ + lesserOrEquals( number: string ): boolean; /** Returns true if the number is even, false otherwise. */ isEven(): boolean; @@ -110,21 +140,32 @@ interface BigInteger { * If the first number is greater, it returns 1. If the first number is lesser, it returns -1. */ compare( number: BigInteger ): BigInteger; + /** + * Performs a comparison between two numbers. If the numbers are equal, it returns 0. + * If the first number is greater, it returns 1. If the first number is lesser, it returns -1. + */ + compare( number: string ): BigInteger; /** Performs a comparison between the absolute value of two numbers. */ compareAbs( number: number ): BigInteger; /** Performs a comparison between the absolute value of two numbers. */ compareAbs( number: BigInteger ): BigInteger; + /** Performs a comparison between the absolute value of two numbers. */ + compareAbs( number: string ): BigInteger; /** Checks if two numbers are equal. */ equals( number: number ): boolean; /** Checks if two numbers are equal. */ equals( number: BigInteger ): boolean; + /** Checks if two numbers are equal. */ + equals( number: string ): boolean; /** Checks if two numbers are not equal. */ notEquals( number: number ): boolean; /** Checks if two numbers are not equal. */ notEquals( number: BigInteger ): boolean; + /** Checks if two numbers are not equal. */ + notEquals( number: string ): boolean; /** Converts a bigInt into a native Javascript number. Loses precision for numbers outside the range. */ toJSNumber(): number; From f723f78dc43ee468aa9c3d69975a6e511b3efb3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ingo=20B=C3=BCrk?= Date: Mon, 21 Apr 2014 01:52:28 +0200 Subject: [PATCH 3/3] #2073: fix spelling --- .../bigInteger-tests.ts => big-integer/big-integer-tests.ts | 2 +- bigInteger/bigInteger.d.ts => big-integer/big-integer.d.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename bigInteger/bigInteger-tests.ts => big-integer/big-integer-tests.ts (98%) rename bigInteger/bigInteger.d.ts => big-integer/big-integer.d.ts (99%) diff --git a/bigInteger/bigInteger-tests.ts b/big-integer/big-integer-tests.ts similarity index 98% rename from bigInteger/bigInteger-tests.ts rename to big-integer/big-integer-tests.ts index 225b2118d..3b9fb8745 100644 --- a/bigInteger/bigInteger-tests.ts +++ b/big-integer/big-integer-tests.ts @@ -1,4 +1,4 @@ -/// +/// // constructor tests var noArgument = bigInt(), diff --git a/bigInteger/bigInteger.d.ts b/big-integer/big-integer.d.ts similarity index 99% rename from bigInteger/bigInteger.d.ts rename to big-integer/big-integer.d.ts index a98fad7e7..dfadc4962 100644 --- a/bigInteger/bigInteger.d.ts +++ b/big-integer/big-integer.d.ts @@ -197,6 +197,6 @@ interface BigIntegerStatic { declare var bigInt: BigIntegerStatic; -declare module "BigInteger" { +declare module "big-integer" { export = bigInt; } \ No newline at end of file