bson
Advanced tools
Comparing version 5.4.0 to 5.5.0
@@ -395,2 +395,22 @@ /** | ||
static fromString(representation: string): Decimal128; | ||
/** | ||
* Create a Decimal128 instance from a string representation, allowing for rounding to 34 | ||
* significant digits | ||
* | ||
* @example Example of a number that will be rounded | ||
* ```ts | ||
* > let d = Decimal128.fromString('37.499999999999999196428571428571375') | ||
* Uncaught: | ||
* BSONError: "37.499999999999999196428571428571375" is not a valid Decimal128 string - inexact rounding | ||
* at invalidErr (/home/wajames/js-bson/lib/bson.cjs:1402:11) | ||
* at Decimal128.fromStringInternal (/home/wajames/js-bson/lib/bson.cjs:1633:25) | ||
* at Decimal128.fromString (/home/wajames/js-bson/lib/bson.cjs:1424:27) | ||
* | ||
* > d = Decimal128.fromStringWithRounding('37.499999999999999196428571428571375') | ||
* new Decimal128("37.49999999999999919642857142857138") | ||
* ``` | ||
* @param representation - a numeric string representation. | ||
*/ | ||
static fromStringWithRounding(representation: string): Decimal128; | ||
private static _fromString; | ||
/** Create a string representation of the raw Decimal128 value */ | ||
@@ -397,0 +417,0 @@ toString(): string; |
@@ -17,3 +17,3 @@ { | ||
"types": "bson.d.ts", | ||
"version": "5.4.0", | ||
"version": "5.5.0", | ||
"author": { | ||
@@ -20,0 +20,0 @@ "name": "The MongoDB NodeJS Team", |
@@ -161,4 +161,31 @@ import { BSONValue } from './bson_value'; | ||
static fromString(representation: string): Decimal128 { | ||
return Decimal128._fromString(representation, { allowRounding: false }); | ||
} | ||
/** | ||
* Create a Decimal128 instance from a string representation, allowing for rounding to 34 | ||
* significant digits | ||
* | ||
* @example Example of a number that will be rounded | ||
* ```ts | ||
* > let d = Decimal128.fromString('37.499999999999999196428571428571375') | ||
* Uncaught: | ||
* BSONError: "37.499999999999999196428571428571375" is not a valid Decimal128 string - inexact rounding | ||
* at invalidErr (/home/wajames/js-bson/lib/bson.cjs:1402:11) | ||
* at Decimal128.fromStringInternal (/home/wajames/js-bson/lib/bson.cjs:1633:25) | ||
* at Decimal128.fromString (/home/wajames/js-bson/lib/bson.cjs:1424:27) | ||
* | ||
* > d = Decimal128.fromStringWithRounding('37.499999999999999196428571428571375') | ||
* new Decimal128("37.49999999999999919642857142857138") | ||
* ``` | ||
* @param representation - a numeric string representation. | ||
*/ | ||
static fromStringWithRounding(representation: string): Decimal128 { | ||
return Decimal128._fromString(representation, { allowRounding: true }); | ||
} | ||
private static _fromString(representation: string, options: { allowRounding: boolean }) { | ||
// Parse state tracking | ||
let isNegative = false; | ||
let sawSign = false; | ||
let sawRadix = false; | ||
@@ -184,4 +211,2 @@ let foundNonZero = false; | ||
let digitsInsert = 0; | ||
// The index of the first non-zero digit | ||
let firstDigit = 0; | ||
// The index of the last digit | ||
@@ -192,4 +217,2 @@ let lastDigit = 0; | ||
let exponent = 0; | ||
// loop index over array | ||
let i = 0; | ||
// The high 17 digits of the significand | ||
@@ -247,2 +270,3 @@ let significandHigh = new Long(0, 0); | ||
if (representation[index] === '+' || representation[index] === '-') { | ||
sawSign = true; | ||
isNegative = representation[index++] === '-'; | ||
@@ -270,3 +294,3 @@ } | ||
if (nDigitsStored < 34) { | ||
if (nDigitsStored < MAX_DIGITS) { | ||
if (representation[index] !== '0' || foundNonZero) { | ||
@@ -315,7 +339,3 @@ if (!foundNonZero) { | ||
// Find first non-zero digit in digits | ||
firstDigit = 0; | ||
if (!nDigitsStored) { | ||
firstDigit = 0; | ||
lastDigit = 0; | ||
digits[0] = 0; | ||
@@ -329,3 +349,7 @@ nDigits = 1; | ||
if (significantDigits !== 1) { | ||
while (digits[firstNonZero + significantDigits - 1] === 0) { | ||
while ( | ||
representation[ | ||
firstNonZero + significantDigits - 1 + Number(sawSign) + Number(sawRadix) | ||
] === '0' | ||
) { | ||
significantDigits = significantDigits - 1; | ||
@@ -341,3 +365,3 @@ } | ||
// Overflow prevention | ||
if (exponent <= radixPosition && radixPosition - exponent > 1 << 14) { | ||
if (exponent <= radixPosition && radixPosition > exponent + (1 << 14)) { | ||
exponent = EXPONENT_MIN; | ||
@@ -352,7 +376,5 @@ } else { | ||
lastDigit = lastDigit + 1; | ||
if (lastDigit - firstDigit > MAX_DIGITS) { | ||
if (lastDigit >= MAX_DIGITS) { | ||
// Check if we have a zero then just hard clamp, otherwise fail | ||
const digitsString = digits.join(''); | ||
if (digitsString.match(/^0+$/)) { | ||
if (significantDigits === 0) { | ||
exponent = EXPONENT_MAX; | ||
@@ -367,80 +389,84 @@ break; | ||
while (exponent < EXPONENT_MIN || nDigitsStored < nDigits) { | ||
// Shift last digit. can only do this if < significant digits than # stored. | ||
if (lastDigit === 0 && significantDigits < nDigitsStored) { | ||
exponent = EXPONENT_MIN; | ||
significantDigits = 0; | ||
break; | ||
} | ||
if (options.allowRounding) { | ||
while (exponent < EXPONENT_MIN || nDigitsStored < nDigits) { | ||
// Shift last digit. can only do this if < significant digits than # stored. | ||
if (lastDigit === 0 && significantDigits < nDigitsStored) { | ||
exponent = EXPONENT_MIN; | ||
significantDigits = 0; | ||
break; | ||
} | ||
if (nDigitsStored < nDigits) { | ||
// adjust to match digits not stored | ||
nDigits = nDigits - 1; | ||
} else { | ||
// adjust to round | ||
lastDigit = lastDigit - 1; | ||
} | ||
if (nDigitsStored < nDigits) { | ||
// adjust to match digits not stored | ||
nDigits = nDigits - 1; | ||
} else { | ||
// adjust to round | ||
lastDigit = lastDigit - 1; | ||
} | ||
if (exponent < EXPONENT_MAX) { | ||
exponent = exponent + 1; | ||
} else { | ||
// Check if we have a zero then just hard clamp, otherwise fail | ||
const digitsString = digits.join(''); | ||
if (digitsString.match(/^0+$/)) { | ||
exponent = EXPONENT_MAX; | ||
break; | ||
if (exponent < EXPONENT_MAX) { | ||
exponent = exponent + 1; | ||
} else { | ||
// Check if we have a zero then just hard clamp, otherwise fail | ||
const digitsString = digits.join(''); | ||
if (digitsString.match(/^0+$/)) { | ||
exponent = EXPONENT_MAX; | ||
break; | ||
} | ||
invalidErr(representation, 'overflow'); | ||
} | ||
invalidErr(representation, 'overflow'); | ||
} | ||
} | ||
// Round | ||
// We've normalized the exponent, but might still need to round. | ||
if (lastDigit - firstDigit + 1 < significantDigits) { | ||
let endOfString = nDigitsRead; | ||
// Round | ||
// We've normalized the exponent, but might still need to round. | ||
if (lastDigit + 1 < significantDigits) { | ||
let endOfString = nDigitsRead; | ||
// If we have seen a radix point, 'string' is 1 longer than we have | ||
// documented with ndigits_read, so inc the position of the first nonzero | ||
// digit and the position that digits are read to. | ||
if (sawRadix) { | ||
firstNonZero = firstNonZero + 1; | ||
endOfString = endOfString + 1; | ||
} | ||
// if negative, we need to increment again to account for - sign at start. | ||
if (isNegative) { | ||
firstNonZero = firstNonZero + 1; | ||
endOfString = endOfString + 1; | ||
} | ||
// If we have seen a radix point, 'string' is 1 longer than we have | ||
// documented with ndigits_read, so inc the position of the first nonzero | ||
// digit and the position that digits are read to. | ||
if (sawRadix) { | ||
firstNonZero = firstNonZero + 1; | ||
endOfString = endOfString + 1; | ||
} | ||
// if negative, we need to increment again to account for - sign at start. | ||
if (sawSign) { | ||
firstNonZero = firstNonZero + 1; | ||
endOfString = endOfString + 1; | ||
} | ||
const roundDigit = parseInt(representation[firstNonZero + lastDigit + 1], 10); | ||
let roundBit = 0; | ||
const roundDigit = parseInt(representation[firstNonZero + lastDigit + 1], 10); | ||
let roundBit = 0; | ||
if (roundDigit >= 5) { | ||
roundBit = 1; | ||
if (roundDigit === 5) { | ||
roundBit = digits[lastDigit] % 2 === 1 ? 1 : 0; | ||
for (i = firstNonZero + lastDigit + 2; i < endOfString; i++) { | ||
if (parseInt(representation[i], 10)) { | ||
roundBit = 1; | ||
break; | ||
if (roundDigit >= 5) { | ||
roundBit = 1; | ||
if (roundDigit === 5) { | ||
roundBit = digits[lastDigit] % 2 === 1 ? 1 : 0; | ||
for (let i = firstNonZero + lastDigit + 2; i < endOfString; i++) { | ||
if (parseInt(representation[i], 10)) { | ||
roundBit = 1; | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
if (roundBit) { | ||
let dIdx = lastDigit; | ||
if (roundBit) { | ||
let dIdx = lastDigit; | ||
for (; dIdx >= 0; dIdx--) { | ||
if (++digits[dIdx] > 9) { | ||
digits[dIdx] = 0; | ||
for (; dIdx >= 0; dIdx--) { | ||
if (++digits[dIdx] > 9) { | ||
digits[dIdx] = 0; | ||
// overflowed most significant digit | ||
if (dIdx === 0) { | ||
if (exponent < EXPONENT_MAX) { | ||
exponent = exponent + 1; | ||
digits[dIdx] = 1; | ||
} else { | ||
return new Decimal128(isNegative ? INF_NEGATIVE_BUFFER : INF_POSITIVE_BUFFER); | ||
// overflowed most significant digit | ||
if (dIdx === 0) { | ||
if (exponent < EXPONENT_MAX) { | ||
exponent = exponent + 1; | ||
digits[dIdx] = 1; | ||
} else { | ||
return new Decimal128(isNegative ? INF_NEGATIVE_BUFFER : INF_POSITIVE_BUFFER); | ||
} | ||
} | ||
} else { | ||
break; | ||
} | ||
@@ -450,2 +476,58 @@ } | ||
} | ||
} else { | ||
while (exponent < EXPONENT_MIN || nDigitsStored < nDigits) { | ||
// Shift last digit. can only do this if < significant digits than # stored. | ||
if (lastDigit === 0) { | ||
if (significantDigits === 0) { | ||
exponent = EXPONENT_MIN; | ||
break; | ||
} | ||
invalidErr(representation, 'exponent underflow'); | ||
} | ||
if (nDigitsStored < nDigits) { | ||
if ( | ||
representation[nDigits - 1 + Number(sawSign) + Number(sawRadix)] !== '0' && | ||
significantDigits !== 0 | ||
) { | ||
invalidErr(representation, 'inexact rounding'); | ||
} | ||
// adjust to match digits not stored | ||
nDigits = nDigits - 1; | ||
} else { | ||
if (digits[lastDigit] !== 0) { | ||
invalidErr(representation, 'inexact rounding'); | ||
} | ||
// adjust to round | ||
lastDigit = lastDigit - 1; | ||
} | ||
if (exponent < EXPONENT_MAX) { | ||
exponent = exponent + 1; | ||
} else { | ||
invalidErr(representation, 'overflow'); | ||
} | ||
} | ||
// Round | ||
// We've normalized the exponent, but might still need to round. | ||
if (lastDigit + 1 < significantDigits) { | ||
// If we have seen a radix point, 'string' is 1 longer than we have | ||
// documented with ndigits_read, so inc the position of the first nonzero | ||
// digit and the position that digits are read to. | ||
if (sawRadix) { | ||
firstNonZero = firstNonZero + 1; | ||
} | ||
// if saw sign, we need to increment again to account for - or + sign at start. | ||
if (sawSign) { | ||
firstNonZero = firstNonZero + 1; | ||
} | ||
const roundDigit = parseInt(representation[firstNonZero + lastDigit + 1], 10); | ||
if (roundDigit !== 0) { | ||
invalidErr(representation, 'inexact rounding'); | ||
} | ||
} | ||
} | ||
@@ -463,4 +545,4 @@ | ||
significandLow = Long.fromNumber(0); | ||
} else if (lastDigit - firstDigit < 17) { | ||
let dIdx = firstDigit; | ||
} else if (lastDigit < 17) { | ||
let dIdx = 0; | ||
significandLow = Long.fromNumber(digits[dIdx++]); | ||
@@ -474,3 +556,3 @@ significandHigh = new Long(0, 0); | ||
} else { | ||
let dIdx = firstDigit; | ||
let dIdx = 0; | ||
significandHigh = Long.fromNumber(digits[dIdx++]); | ||
@@ -555,3 +637,2 @@ | ||
} | ||
/** Create a string representation of the raw Decimal128 value */ | ||
@@ -558,0 +639,0 @@ toString(): string { |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
2283328
29007