name:
dist/Formulas/Engineering.js
-rw-r--r--
12732
1"use strict";
2exports.__esModule = true;
3var ArgsChecker_1 = require("../Utilities/ArgsChecker");
4var TypeConverter_1 = require("../Utilities/TypeConverter");
5var Errors_1 = require("../Errors");
6/**
7 * Converts a signed binary number to decimal format.
8 * @param signedBinaryNumber - The signed 10-bit binary value to be converted to decimal, provided as a
9 * string. The most significant bit of signed_binary_number is the sign bit; that is, negative numbers are represented
10 * in two's complement format.
11 * @returns {number}
12 * @constructor
13 */
14var BIN2DEC = function (signedBinaryNumber) {
15 ArgsChecker_1.ArgsChecker.checkLength(arguments, 1, "BIN2DEC");
16 if (typeof TypeConverter_1.TypeConverter.firstValue(signedBinaryNumber) === "boolean") {
17 throw new Errors_1.ValueError("Function BIN2DEC parameter 1 expects text values. But '" + signedBinaryNumber + "' is a boolean and cannot be coerced to a text.");
18 }
19 var n = TypeConverter_1.TypeConverter.firstValueAsString(signedBinaryNumber);
20 if (!(/^[01]{1,10}$/).test(n)) {
21 throw new Errors_1.NumError("Input for BIN2DEC ('" + n + "') is not a valid binary representation.");
22 }
23 if (n.length === 10 && n.substring(0, 1) === '1') {
24 return parseInt(n.substring(1), 2) - 512;
25 }
26 return parseInt(n, 2);
27};
28exports.BIN2DEC = BIN2DEC;
29/**
30 * Converts a signed binary number to signed hexadecimal format.
31 * @param signedBinaryNumber - The signed 10-bit binary value to be converted to signed hexadecimal,
32 * provided as a string. The most significant bit of signed_binary_number is the sign bit; that is, negative numbers are
33 * represented in two's complement format.
34 * @param significantDigits - [ OPTIONAL ] - The number of significant digits to ensure in the result.
35 * @returns {string} string representation of a signed hexadecimal
36 * @constructor
37 */
38var BIN2HEX = function (signedBinaryNumber, significantDigits) {
39 ArgsChecker_1.ArgsChecker.checkLengthWithin(arguments, 1, 2, "BIN2HEX");
40 if (typeof TypeConverter_1.TypeConverter.firstValue(signedBinaryNumber) === "boolean") {
41 throw new Errors_1.ValueError("Function BIN2HEX parameter 1 expects text values. But '" + signedBinaryNumber + "' is a boolean and cannot be coerced to a text.");
42 }
43 var n = TypeConverter_1.TypeConverter.firstValueAsString(signedBinaryNumber);
44 var p = 10;
45 if (significantDigits !== undefined) {
46 p = TypeConverter_1.TypeConverter.firstValueAsNumber(significantDigits);
47 }
48 if (!(/^[01]{1,10}$/).test(n)) {
49 throw new Errors_1.NumError("Input for BIN2HEX ('" + n + "') is not a valid binary representation.");
50 }
51 if (n.length === 10 && n.substring(0, 1) === '1') {
52 return (1099511627264 + parseInt(n.substring(1), 2)).toString(16).toUpperCase();
53 }
54 if (p < 1 || p > 10) {
55 throw new Errors_1.NumError("Function BIN2HEX parameter 2 value is " + p + ". Valid values are between 1 and 10 inclusive.");
56 }
57 p = Math.floor(p);
58 // Convert decimal number to hexadecimal
59 var result = parseInt(n.toString(), 2).toString(16).toUpperCase();
60 if (p === 10) {
61 return result;
62 }
63 var str = "";
64 for (var i = 0; i < p - result.length; i++) {
65 str += "0";
66 }
67 return str + result;
68};
69exports.BIN2HEX = BIN2HEX;
70/**
71 * Converts a signed binary number to signed octal format.
72 * @param signedBinaryNumber - The signed 10-bit binary value to be converted to signed octal, provided as a
73 * string. The most significant bit of signed_binary_number is the sign bit; that is, negative numbers are represented
74 * in two's complement format.
75 * @param significantDigits - [ OPTIONAL ] - The number of significant digits to ensure in the result. If
76 * this is greater than the number of significant digits in the result, the result is left-padded with zeros until the
77 * total number of digits reaches significant_digits.
78 * @returns {string} number in octal format
79 * @constructor
80 */
81var BIN2OCT = function (signedBinaryNumber, significantDigits) {
82 ArgsChecker_1.ArgsChecker.checkLengthWithin(arguments, 1, 2, "BIN2OCT");
83 if (typeof TypeConverter_1.TypeConverter.firstValue(signedBinaryNumber) === "boolean") {
84 throw new Errors_1.ValueError("Function BIN2OCT parameter 1 expects text values. But '" + signedBinaryNumber + "' is a boolean and cannot be coerced to a text.");
85 }
86 var n = TypeConverter_1.TypeConverter.firstValueAsString(signedBinaryNumber);
87 var p = 10;
88 if (significantDigits !== undefined) {
89 p = TypeConverter_1.TypeConverter.firstValueAsNumber(significantDigits);
90 }
91 if (!(/^[01]{1,10}$/).test(n)) {
92 throw new Errors_1.NumError("Input for BIN2OCT ('" + n + "') is not a valid binary representation.");
93 }
94 if (n.length === 10 && n.substring(0, 1) === '1') {
95 return (1073741312 + parseInt(n.substring(1), 2)).toString(8);
96 }
97 if (p < 1 || p > 10) {
98 throw new Errors_1.NumError("Function BIN2OCT parameter 2 value is " + p + ". Valid values are between 1 and 10 inclusive.");
99 }
100 p = Math.floor(p);
101 var result = parseInt(n.toString(), 2).toString(8);
102 if (p === 10) {
103 return result;
104 }
105 if (p >= result.length) {
106 var str = "";
107 for (var i = 0; i < p - result.length - 1; i++) {
108 str += "0";
109 }
110 return str + result;
111 }
112};
113exports.BIN2OCT = BIN2OCT;
114/**
115 * Converts a decimal number to signed octal format.
116 * @param decimalDumber - The decimal value to be converted to signed octal,provided as a string. For this
117 * function, this value has a maximum of 536870911 if positive, and a minimum of -53687092 if negative.
118 * @param significantDigits - [ OPTIONAL ] The number of significant digits to ensure in the result. If this
119 * is greater than the number of significant digits in the result, the result is left-padded with zeros until the total
120 * number of digits reaches significant_digits.
121 * @returns {string} octal string representation of the decimal number
122 * @constructor
123 */
124var DEC2OCT = function (decimalDumber, significantDigits) {
125 ArgsChecker_1.ArgsChecker.checkLengthWithin(arguments, 1, 2, "DEC2OCT");
126 var n = TypeConverter_1.TypeConverter.firstValueAsNumber(decimalDumber);
127 if (n < 0) {
128 n = Math.ceil(n);
129 }
130 if (n > 0) {
131 n = Math.floor(n);
132 }
133 var p = 10;
134 var placesPresent = false;
135 if (significantDigits !== undefined) {
136 p = Math.floor(TypeConverter_1.TypeConverter.firstValueAsNumber(significantDigits));
137 placesPresent = true;
138 }
139 if (n < -53687092 || n > 536870911) {
140 throw new Errors_1.NumError("Function DEC2OCT parameter 1 value is " + n + ". Valid values are between -53687092 and 536870911 inclusive.");
141 }
142 if (p < 1 || p > 10) {
143 throw new Errors_1.NumError("Function DEC2OCT parameter 2 value is " + p + ". Valid values are between 1 and 10 inclusive.");
144 }
145 if (n < 0) {
146 return (1073741824 + n).toString(8).toUpperCase();
147 }
148 // Convert decimal number to hexadecimal
149 var result = parseInt(n.toString(), 10).toString(8).toUpperCase();
150 if (!placesPresent) {
151 return result;
152 }
153 var str = "";
154 for (var i = 0; i < p - result.length; i++) {
155 str += "0";
156 }
157 return str + result.toUpperCase();
158};
159exports.DEC2OCT = DEC2OCT;
160/**
161 * Converts a decimal number to signed hexadecimal format.
162 * @param decimalDumber - The decimal value to be converted to signed hexadecimal, provided as a string. This
163 * value has a maximum of 549755813887 if positive, and a minimum of -549755814888 if negative.
164 * @param significantDigits - [ OPTIONAL ] - The number of significant digits to ensure in the result. If
165 * this is greater than the number of significant digits in the result, the result is left-padded with zeros until the
166 * total number of digits reaches significant_digits. This value is ignored if decimal_number is negative.
167 * @returns {string} hexadecimal string representation of the decimal number
168 * @constructor
169 */
170var DEC2HEX = function (decimalDumber, significantDigits) {
171 ArgsChecker_1.ArgsChecker.checkLengthWithin(arguments, 1, 2, "DEC2HEX");
172 var n = TypeConverter_1.TypeConverter.firstValueAsNumber(decimalDumber);
173 if (n < 0) {
174 n = Math.ceil(n);
175 }
176 if (n > 0) {
177 n = Math.floor(n);
178 }
179 var p = 10;
180 var placesPresent = false;
181 if (significantDigits !== undefined) {
182 p = Math.floor(TypeConverter_1.TypeConverter.firstValueAsNumber(significantDigits));
183 placesPresent = true;
184 }
185 if (n < -549755813888 || n > 549755813887) {
186 throw new Errors_1.NumError("Function DEC2HEX parameter 1 value is " + n + ". Valid values are between -549755813888 and 549755813887 inclusive.");
187 }
188 if (p < 1 || p > 10) {
189 throw new Errors_1.NumError("Function DEC2HEX parameter 2 value is " + p + ". Valid values are between 1 and 10 inclusive.");
190 }
191 // Ignore places and return a 10-character hexadecimal number if number is negative
192 if (n < 0) {
193 return (1099511627776 + n).toString(16).toUpperCase();
194 }
195 // Convert decimal number to hexadecimal
196 var result = parseInt(n.toString(), 10).toString(16).toUpperCase();
197 if (!placesPresent) {
198 return result;
199 }
200 var str = "";
201 for (var i = 0; i < p - result.length; i++) {
202 str += "0";
203 }
204 return str + result;
205};
206exports.DEC2HEX = DEC2HEX;
207/**
208 * Converts a decimal number to signed binary format.
209 * @param decimalDumber - The decimal value to be converted to signed binary, provided as a string. For this
210 * function, this value has a maximum of 511 if positive, and a minimum of -512 if negative.
211 * @param significantDigits - [ OPTIONAL ] The number of significant digits to ensure in the result. If this
212 * is greater than the number of significant digits in the result, the result is left-padded with zeros until the total
213 * number of digits reaches significant_digits.
214 * @returns {string} signed binary string representation of the input decimal number.
215 * @constructor
216 */
217var DEC2BIN = function (decimalDumber, significantDigits) {
218 ArgsChecker_1.ArgsChecker.checkLengthWithin(arguments, 1, 2, "DEC2BIN");
219 var n = TypeConverter_1.TypeConverter.firstValueAsNumber(decimalDumber);
220 if (n < 0) {
221 n = Math.ceil(n);
222 }
223 if (n > 0) {
224 n = Math.floor(n);
225 }
226 if (n === 0 || n === 1) {
227 return n.toString();
228 }
229 var p = 10;
230 var placesPresent = false;
231 if (significantDigits !== undefined) {
232 p = Math.floor(TypeConverter_1.TypeConverter.firstValueAsNumber(significantDigits));
233 placesPresent = true;
234 }
235 if (n < -512 || n > 511) {
236 throw new Errors_1.NumError("Function DEC2BIN parameter 1 value is " + n + ". Valid values are between -512 and 511 inclusive.");
237 }
238 if (p < 1 || p > 10) {
239 throw new Errors_1.NumError("Function DEC2BIN parameter 2 value is " + p + ". Valid values are between 1 and 10 inclusive.");
240 }
241 // Ignore places and return a 10-character binary number if number is negative
242 if (n < 0) {
243 var count = (9 - (512 + n).toString(2).length);
244 var st = "";
245 for (var i = 0; i < count; i++) {
246 st += "0";
247 }
248 return "1" + st + (512 + n).toString(2);
249 }
250 // Convert decimal number to binary
251 var result = parseInt(n.toString(), 10).toString(2);
252 // Pad return value with leading 0s (zeros) if necessary
253 if (p >= result.length) {
254 var str = "";
255 for (var i = 0; i < (p - result.length); i++) {
256 str += "0";
257 }
258 var workingString = str + result;
259 if (!placesPresent) {
260 var returnString = "";
261 for (var i = 0; i < workingString.length; i++) {
262 var char = workingString[i];
263 if (char === "1") {
264 break;
265 }
266 returnString = workingString.slice(i + 1);
267 }
268 return returnString;
269 }
270 return workingString;
271 }
272};
273exports.DEC2BIN = DEC2BIN;
274/**
275 * Compare two numeric values, returning 1 if they're equal.
276 * @param one - The first number to compare.
277 * @param two - The second number to compare.
278 * @returns {number} 1 if they're equal, 0 if they're not equal.
279 * @constructor
280 */
281var DELTA = function (one, two) {
282 ArgsChecker_1.ArgsChecker.checkLengthWithin(arguments, 1, 2, "DELTA");
283 if (two === undefined) {
284 return TypeConverter_1.TypeConverter.valueToNumber(one) === 0 ? 1 : 0;
285 }
286 return TypeConverter_1.TypeConverter.valueToNumber(one) === TypeConverter_1.TypeConverter.valueToNumber(two) ? 1 : 0;
287};
288exports.DELTA = DELTA;