Skip to content
This repository was archived by the owner on Jan 15, 2025. It is now read-only.

Commit e779054

Browse files
committed
Remove inofficial rounding modes
These rounding modes are optional/unsupported in IEEE Decimal128 and thus may not be supported in compiler libraries or hardware.
1 parent fad956a commit e779054

File tree

3 files changed

+1
-311
lines changed

3 files changed

+1
-311
lines changed

src/decimal128.mts

+1-194
Original file line numberDiff line numberDiff line change
@@ -393,41 +393,6 @@ function roundFloor(x: SignedSignificandExponent): SignedSignificandExponent {
393393
};
394394
}
395395

396-
function roundExpand(x: SignedSignificandExponent): SignedSignificandExponent {
397-
let sig = x.significand.toString();
398-
let lastDigit = parseInt(sig.charAt(MAX_SIGNIFICANT_DIGITS)) as Digit;
399-
let cutoff = cutoffAfterSignificantDigits(sig, MAX_SIGNIFICANT_DIGITS - 1);
400-
let penultimateDigit = parseInt(
401-
sig.charAt(MAX_SIGNIFICANT_DIGITS - 1)
402-
) as Digit;
403-
let excessDigits = sig.substring(MAX_SIGNIFICANT_DIGITS);
404-
let numExcessDigits = excessDigits.length;
405-
let exp = x.exponent + numExcessDigits; // we will chop off the excess digits
406-
407-
let finalDigit = roundIt(
408-
x.isNegative,
409-
penultimateDigit,
410-
lastDigit,
411-
ROUNDING_MODE_EXPAND
412-
);
413-
414-
if (finalDigit < 10) {
415-
return {
416-
isNegative: x.isNegative,
417-
significand: BigInt(`${cutoff}${finalDigit}`),
418-
exponent: exp,
419-
};
420-
}
421-
422-
let rounded = propagateCarryFromRight(cutoff);
423-
424-
return {
425-
isNegative: x.isNegative,
426-
significand: BigInt(`${rounded}0`),
427-
exponent: exp,
428-
};
429-
}
430-
431396
function roundTrunc(x: SignedSignificandExponent): SignedSignificandExponent {
432397
let sig = x.significand.toString();
433398
let lastDigit = parseInt(sig.charAt(MAX_SIGNIFICANT_DIGITS)) as Digit;
@@ -453,43 +418,6 @@ function roundTrunc(x: SignedSignificandExponent): SignedSignificandExponent {
453418
};
454419
}
455420

456-
function roundHalfExpand(
457-
x: SignedSignificandExponent
458-
): SignedSignificandExponent {
459-
let sig = x.significand.toString();
460-
let lastDigit = parseInt(sig.charAt(MAX_SIGNIFICANT_DIGITS)) as Digit;
461-
let cutoff = cutoffAfterSignificantDigits(sig, MAX_SIGNIFICANT_DIGITS - 1);
462-
let penultimateDigit = parseInt(
463-
sig.charAt(MAX_SIGNIFICANT_DIGITS - 1)
464-
) as Digit;
465-
let excessDigits = sig.substring(MAX_SIGNIFICANT_DIGITS);
466-
let numExcessDigits = excessDigits.length;
467-
let exp = x.exponent + numExcessDigits; // we will chop off the excess digits
468-
469-
let finalDigit = roundIt(
470-
x.isNegative,
471-
penultimateDigit,
472-
lastDigit,
473-
ROUNDING_MODE_HALF_EXPAND
474-
);
475-
476-
if (finalDigit < 10) {
477-
return {
478-
isNegative: x.isNegative,
479-
significand: BigInt(`${cutoff}${finalDigit}`),
480-
exponent: exp,
481-
};
482-
}
483-
484-
let rounded = propagateCarryFromRight(cutoff);
485-
486-
return {
487-
isNegative: x.isNegative,
488-
significand: BigInt(`${rounded}0`),
489-
exponent: exp,
490-
};
491-
}
492-
493421
function roundHalfCeil(
494422
x: SignedSignificandExponent
495423
): SignedSignificandExponent {
@@ -517,70 +445,6 @@ function roundHalfCeil(
517445
};
518446
}
519447

520-
function roundHalfFloor(
521-
x: SignedSignificandExponent
522-
): SignedSignificandExponent {
523-
let sig = x.significand.toString();
524-
let lastDigit = parseInt(sig.charAt(MAX_SIGNIFICANT_DIGITS)) as Digit;
525-
let cutoff = cutoffAfterSignificantDigits(sig, MAX_SIGNIFICANT_DIGITS - 1);
526-
let penultimateDigit = parseInt(
527-
sig.charAt(MAX_SIGNIFICANT_DIGITS - 1)
528-
) as Digit;
529-
let excessDigits = sig.substring(MAX_SIGNIFICANT_DIGITS);
530-
let numExcessDigits = excessDigits.length;
531-
let exp = x.exponent + numExcessDigits; // we will chop off the excess digits
532-
533-
let finalDigit = roundIt(
534-
x.isNegative,
535-
penultimateDigit,
536-
lastDigit,
537-
ROUNDING_MODE_HALF_FLOOR
538-
);
539-
540-
if (finalDigit < 10) {
541-
return {
542-
isNegative: x.isNegative,
543-
significand: BigInt(`${cutoff}${finalDigit}`),
544-
exponent: exp,
545-
};
546-
}
547-
548-
let rounded = propagateCarryFromRight(cutoff);
549-
550-
return {
551-
isNegative: x.isNegative,
552-
significand: BigInt(`${rounded}0`),
553-
exponent: exp,
554-
};
555-
}
556-
557-
function roundHalfTrunc(
558-
x: SignedSignificandExponent
559-
): SignedSignificandExponent {
560-
let sig = x.significand.toString();
561-
let lastDigit = parseInt(sig.charAt(MAX_SIGNIFICANT_DIGITS)) as Digit;
562-
let cutoff = cutoffAfterSignificantDigits(sig, MAX_SIGNIFICANT_DIGITS - 1);
563-
let penultimateDigit = parseInt(
564-
sig.charAt(MAX_SIGNIFICANT_DIGITS - 1)
565-
) as Digit;
566-
let excessDigits = sig.substring(MAX_SIGNIFICANT_DIGITS);
567-
let numExcessDigits = excessDigits.length;
568-
let exp = x.exponent + numExcessDigits; // we will chop off the excess digits
569-
570-
let finalDigit = roundIt(
571-
x.isNegative,
572-
penultimateDigit,
573-
lastDigit,
574-
ROUNDING_MODE_HALF_TRUNCATE
575-
);
576-
577-
return {
578-
isNegative: x.isNegative,
579-
significand: BigInt(`${cutoff}${finalDigit}`),
580-
exponent: exp,
581-
};
582-
}
583-
584448
function adjustNonInteger(
585449
x: SignedSignificandExponent,
586450
options: FullySpecifiedConstructorOptions
@@ -592,18 +456,10 @@ function adjustNonInteger(
592456
return roundCeiling(x);
593457
case ROUNDING_MODE_FLOOR:
594458
return roundFloor(x);
595-
case ROUNDING_MODE_EXPAND:
596-
return roundExpand(x);
597459
case ROUNDING_MODE_TRUNCATE:
598460
return roundTrunc(x);
599-
case ROUNDING_MODE_HALF_EXPAND:
600-
return roundHalfExpand(x);
601461
case ROUNDING_MODE_HALF_CEILING:
602462
return roundHalfCeil(x);
603-
case ROUNDING_MODE_HALF_FLOOR:
604-
return roundHalfFloor(x);
605-
case ROUNDING_MODE_HALF_TRUNCATE:
606-
return roundHalfTrunc(x);
607463
default:
608464
return roundHalfEven(x);
609465
}
@@ -718,13 +574,9 @@ function handleInfinity(s: string): Decimal128Constructor {
718574

719575
export const ROUNDING_MODE_CEILING: RoundingMode = "ceil";
720576
export const ROUNDING_MODE_FLOOR: RoundingMode = "floor";
721-
export const ROUNDING_MODE_EXPAND: RoundingMode = "expand";
722577
export const ROUNDING_MODE_TRUNCATE: RoundingMode = "trunc";
723578
export const ROUNDING_MODE_HALF_EVEN: RoundingMode = "halfEven";
724-
export const ROUNDING_MODE_HALF_EXPAND: RoundingMode = "halfExpand";
725579
export const ROUNDING_MODE_HALF_CEILING: RoundingMode = "halfCeil";
726-
export const ROUNDING_MODE_HALF_FLOOR: RoundingMode = "halfFloor";
727-
export const ROUNDING_MODE_HALF_TRUNCATE: RoundingMode = "halfTrunc";
728580

729581
const ROUNDING_MODE_DEFAULT = ROUNDING_MODE_HALF_EVEN;
730582
const CONSTRUCTOR_SHOULD_NORMALIZE = false;
@@ -756,8 +608,6 @@ function roundIt(
756608
}
757609

758610
return digitToRound;
759-
case ROUNDING_MODE_EXPAND:
760-
return (digitToRound + 1) as DigitOrTen;
761611
case ROUNDING_MODE_TRUNCATE:
762612
return digitToRound;
763613
case ROUNDING_MODE_HALF_CEILING:
@@ -769,36 +619,6 @@ function roundIt(
769619
return (digitToRound + 1) as DigitOrTen;
770620
}
771621

772-
return digitToRound;
773-
case ROUNDING_MODE_HALF_FLOOR:
774-
if (decidingDigit === 5) {
775-
if (isNegative) {
776-
return (digitToRound + 1) as DigitOrTen;
777-
}
778-
779-
return digitToRound;
780-
}
781-
782-
if (decidingDigit > 5) {
783-
return (digitToRound + 1) as DigitOrTen;
784-
}
785-
786-
return digitToRound;
787-
case ROUNDING_MODE_HALF_TRUNCATE:
788-
if (decidingDigit === 5) {
789-
return digitToRound;
790-
}
791-
792-
if (decidingDigit > 5) {
793-
return (digitToRound + 1) as DigitOrTen;
794-
}
795-
796-
return digitToRound;
797-
case ROUNDING_MODE_HALF_EXPAND:
798-
if (decidingDigit >= 5) {
799-
return (digitToRound + 1) as DigitOrTen;
800-
}
801-
802622
return digitToRound;
803623
default: // ROUNDING_MODE_HALF_EVEN:
804624
if (decidingDigit === 5) {
@@ -817,27 +637,14 @@ function roundIt(
817637
}
818638
}
819639

820-
type RoundingMode =
821-
| "ceil"
822-
| "floor"
823-
| "expand"
824-
| "trunc"
825-
| "halfEven"
826-
| "halfExpand"
827-
| "halfCeil"
828-
| "halfFloor"
829-
| "halfTrunc";
640+
type RoundingMode = "ceil" | "floor" | "trunc" | "halfEven" | "halfCeil";
830641

831642
const ROUNDING_MODES: RoundingMode[] = [
832643
"ceil",
833644
"floor",
834-
"expand",
835645
"trunc",
836646
"halfEven",
837-
"halfExpand",
838647
"halfCeil",
839-
"halfFloor",
840-
"halfTrunc",
841648
];
842649

843650
const digitStrRegExp =

tests/constructor.test.js

-8
Original file line numberDiff line numberDiff line change
@@ -464,13 +464,9 @@ describe("rounding options", () => {
464464
let answers = {
465465
ceil: "-1234567890123456789012345678901234",
466466
floor: "-1234567890123456789012345678901235",
467-
expand: "-1234567890123456789012345678901235",
468467
trunc: "-1234567890123456789012345678901234",
469468
halfEven: "-1234567890123456789012345678901234",
470-
halfExpand: "-1234567890123456789012345678901235",
471469
halfCeil: "-1234567890123456789012345678901234",
472-
halfFloor: "-1234567890123456789012345678901235",
473-
halfTrunc: "-1234567890123456789012345678901234",
474470
};
475471
for (const [mode, expected] of Object.entries(answers)) {
476472
test(`constructor with rounding mode "${mode}"`, () => {
@@ -485,13 +481,9 @@ describe("rounding options", () => {
485481
let roundUpAnswers = {
486482
ceil: "-1234567890123456789012345678901239",
487483
floor: "-1234567890123456789012345678901240",
488-
expand: "-1234567890123456789012345678901240",
489484
trunc: "-1234567890123456789012345678901239",
490485
halfEven: "-1234567890123456789012345678901240",
491-
halfExpand: "-1234567890123456789012345678901240",
492486
halfCeil: "-1234567890123456789012345678901239",
493-
halfFloor: "-1234567890123456789012345678901240",
494-
halfTrunc: "-1234567890123456789012345678901239",
495487
};
496488
for (const [mode, expected] of Object.entries(roundUpAnswers)) {
497489
test(`constructor with rounding mode "${mode}"`, () => {

0 commit comments

Comments
 (0)