@@ -23,7 +23,9 @@ import { Rational } from "./Rational.mjs";
23
23
import { Decimal } from "./Decimal.mjs" ;
24
24
25
25
const EXPONENT_MIN = - 6176 ;
26
+ const NORMAL_EXPONENT_MIN = - 6143 ;
26
27
const EXPONENT_MAX = 6111 ;
28
+ const NORMAL_EXPONENT_MAX = 6144 ;
27
29
const MAX_SIGNIFICANT_DIGITS = 34 ;
28
30
29
31
const bigTen = BigInt ( 10 ) ;
@@ -1046,6 +1048,70 @@ export class Decimal128 {
1046
1048
let q = this . divide ( d ) . round ( 0 , ROUNDING_MODE_TRUNCATE ) ;
1047
1049
return this . subtract ( d . multiply ( q ) ) ;
1048
1050
}
1051
+
1052
+ isNormal ( ) : boolean {
1053
+ if ( this . isNaN ( ) ) {
1054
+ throw new RangeError ( "Cannot determine whether NaN is normal" ) ;
1055
+ }
1056
+
1057
+ if ( ! this . isFinite ( ) ) {
1058
+ throw new RangeError (
1059
+ "Only finite numbers can be said to be normal or not"
1060
+ ) ;
1061
+ }
1062
+
1063
+ if ( this . isZero ( ) ) {
1064
+ throw new RangeError (
1065
+ "Only non-zero numbers can be said to be normal or not"
1066
+ ) ;
1067
+ }
1068
+
1069
+ let exp = this . exponent ( ) ;
1070
+ return exp >= NORMAL_EXPONENT_MIN && exp <= NORMAL_EXPONENT_MAX ;
1071
+ }
1072
+
1073
+ isSubnormal ( ) : boolean {
1074
+ if ( this . isNaN ( ) ) {
1075
+ throw new RangeError ( "Cannot determine whether NaN is subnormal" ) ;
1076
+ }
1077
+
1078
+ if ( ! this . isFinite ( ) ) {
1079
+ throw new RangeError (
1080
+ "Only finite numbers can be said to be subnormal or not"
1081
+ ) ;
1082
+ }
1083
+
1084
+ let exp = this . exponent ( ) ;
1085
+ return exp < NORMAL_EXPONENT_MIN ;
1086
+ }
1087
+
1088
+ truncatedExponent ( ) : number {
1089
+ if ( this . isZero ( ) || this . isSubnormal ( ) ) {
1090
+ return NORMAL_EXPONENT_MIN ;
1091
+ }
1092
+
1093
+ return this . exponent ( ) ;
1094
+ }
1095
+
1096
+ scaledSignificand ( ) : bigint {
1097
+ if ( this . isNaN ( ) ) {
1098
+ throw new RangeError ( "NaN does not have a scaled significand" ) ;
1099
+ }
1100
+
1101
+ if ( ! this . isFinite ( ) ) {
1102
+ throw new RangeError ( "Infinity does not have a scaled significand" ) ;
1103
+ }
1104
+
1105
+ if ( this . isZero ( ) ) {
1106
+ return 0n ;
1107
+ }
1108
+
1109
+ let v = this . cohort ( ) as Rational ;
1110
+ let te = this . truncatedExponent ( ) ;
1111
+ let ss = v . scale10 ( MAX_SIGNIFICANT_DIGITS - 1 - te ) ;
1112
+
1113
+ return ss . numerator ;
1114
+ }
1049
1115
}
1050
1116
1051
1117
Decimal128 . prototype . valueOf = function ( ) {
0 commit comments