commit
message
Reorganizing formulas
author
Ben Vogt <[email protected]>
date
2017-02-18 18:26:42
stats
3 file(s) changed,
344 insertions(+),
396 deletions(-)
files
src/RawFormulas/Math.ts
src/RawFormulas/Misc.ts
src/RawFormulas/RawFormulas.ts
1diff --git a/src/RawFormulas/Math.ts b/src/RawFormulas/Math.ts
2index 91e4ca3..0ed50d9 100644
3--- a/src/RawFormulas/Math.ts
4+++ b/src/RawFormulas/Math.ts
5@@ -500,6 +500,21 @@ var SQRT = function (...values) : number {
6 return Math.sqrt(x);
7 };
8
9+/**
10+ * Returns the positive square root of the product of Pi and the given positive number.
11+ * @param values[0] value - The number which will be multiplied by Pi and have the product's square root returned
12+ * @returns {number} the positive square root of the product of Pi and the given positive number.
13+ * @constructor
14+ */
15+var SQRTPI = function (...values) : number{
16+ ArgsChecker.checkLength(values, 1);
17+ var n = TypeCaster.firstValueAsNumber(values[0]);
18+ if (n < 0) {
19+ throw new CellError(ERRORS.NUM_ERROR, "Function SQRTPI parameter 1 value is " + n + ". It should be greater than or equal to 0.");
20+ }
21+ return Math.sqrt(n * Math.PI);
22+};
23+
24 /**
25 * Returns the cosine of an angle provided in radians.
26 * @param values[0] The angle to find the cosine of, in radians.
27@@ -1082,6 +1097,31 @@ var TRUNC = function (...values) : number {
28 };
29
30
31+/**
32+ * Converts an angle value in degrees to radians.
33+ * @param values[0] angle - The angle to convert from degrees to radians.
34+ * @returns {number} radians
35+ * @constructor
36+ */
37+var RADIANS = function (...values) {
38+ ArgsChecker.checkLength(values, 1);
39+ var d = TypeCaster.firstValueAsNumber(values[0]);
40+ return d * Math.PI / 180;
41+};
42+
43+/**
44+ * Converts an angle value in radians to degrees.
45+ * @param values[0] angle - The angle to convert from radians to degrees.
46+ * @returns {number} degrees
47+ * @constructor
48+ */
49+var DEGREES = function (...values) {
50+ ArgsChecker.checkLength(values, 1);
51+ var r = TypeCaster.firstValueAsNumber(values[0]);
52+ return r * 180 / Math.PI;
53+};
54+
55+
56
57 export {
58 ABS,
59@@ -1116,6 +1156,7 @@ export {
60 SINH,
61 SUM,
62 SQRT,
63+ SQRTPI,
64 PI,
65 POWER,
66 LOG,
67@@ -1137,5 +1178,7 @@ export {
68 COUNTIFS,
69 CEILING,
70 SUMSQ,
71- TRUNC
72+ TRUNC,
73+ RADIANS,
74+ DEGREES
75 }
76\ No newline at end of file
77diff --git a/src/RawFormulas/Misc.ts b/src/RawFormulas/Misc.ts
78index 3bbe308..7742e1b 100644
79--- a/src/RawFormulas/Misc.ts
80+++ b/src/RawFormulas/Misc.ts
81@@ -93,9 +93,298 @@ var CONCATENATE = function (...values) : string {
82 return string;
83 };
84
85+/**
86+ * Converts a signed binary number to decimal format.
87+ * @param values[0] signed_binary_number - The signed 10-bit binary value to be converted to decimal, provided as a
88+ * string. The most significant bit of signed_binary_number is the sign bit; that is, negative numbers are represented
89+ * in two's complement format.
90+ * @returns {number}
91+ * @constructor
92+ */
93+var BIN2DEC = function (...values) : number {
94+ ArgsChecker.checkLength(values, 1);
95+ if (typeof TypeCaster.firstValue(values[0]) === "boolean") {
96+ throw new CellError(ERRORS.VALUE_ERROR, "Function BIN2DEC parameter 1 expects text values. But '" + values[0] + "' is a boolean and cannot be coerced to a text.");
97+ }
98+ var n = TypeCaster.firstValueAsString(values[0]);
99+ if (!(/^[01]{1,10}$/).test(n)) {
100+ throw new CellError(ERRORS.NUM_ERROR, "Input for BIN2DEC ('"+n+"') is not a valid binary representation.");
101+ }
102+
103+ if (n.length === 10 && n.substring(0, 1) === '1') {
104+ return parseInt(n.substring(1), 2) - 512;
105+ }
106+ return parseInt(n, 2);
107+};
108+
109+
110+/**
111+ * Converts a signed binary number to signed hexadecimal format.
112+ * @param values[0] signed_binary_number - The signed 10-bit binary value to be converted to signed hexadecimal,
113+ * provided as a string. The most significant bit of signed_binary_number is the sign bit; that is, negative numbers are
114+ * represented in two's complement format.
115+ * @param values[1] significant_digits - [ OPTIONAL ] - The number of significant digits to ensure in the result.
116+ * @returns {string} string representation of a signed hexadecimal
117+ * @constructor
118+ */
119+var BIN2HEX = function (...values) : string {
120+ ArgsChecker.checkLengthWithin(values, 1, 2);
121+ if (typeof TypeCaster.firstValue(values[0]) === "boolean") {
122+ throw new CellError(ERRORS.VALUE_ERROR, "Function BIN2HEX parameter 1 expects text values. But '" + values[0] + "' is a boolean and cannot be coerced to a text.");
123+ }
124+ var n = TypeCaster.firstValueAsString(values[0]);
125+ var p = 10;
126+ if (values.length === 2) {
127+ p = TypeCaster.firstValueAsNumber(values[1]);
128+ }
129+ if (!(/^[01]{1,10}$/).test(n)) {
130+ throw new CellError(ERRORS.NUM_ERROR, "Input for BIN2HEX ('"+n+"') is not a valid binary representation.");
131+ }
132+
133+ if (n.length === 10 && n.substring(0, 1) === '1') {
134+ return (1099511627264 + parseInt(n.substring(1), 2)).toString(16).toUpperCase();
135+ }
136+
137+ if (p < 1 || p > 10) {
138+ throw new CellError(ERRORS.NUM_ERROR, "Function BIN2HEX parameter 2 value is " + p + ". Valid values are between 1 and 10 inclusive.");
139+ }
140+ p = Math.floor(p);
141+ // Convert decimal number to hexadecimal
142+ var result = parseInt(n.toString(), 2).toString(16).toUpperCase();
143+ if (p === 10) {
144+ return result;
145+ }
146+ var str = "";
147+ for (var i = 0; i < p - result.length; i++) {
148+ str += "0";
149+ }
150+ return str + result;
151+};
152+
153+
154+/**
155+ * Converts a signed binary number to signed octal format.
156+ * @param values[0] signed_binary_number - The signed 10-bit binary value to be converted to signed octal, provided as a
157+ * string. The most significant bit of signed_binary_number is the sign bit; that is, negative numbers are represented
158+ * in two's complement format.
159+ * @param values[1] significant_digits - [ OPTIONAL ] - The number of significant digits to ensure in the result. If
160+ * this is greater than the number of significant digits in the result, the result is left-padded with zeros until the
161+ * total number of digits reaches significant_digits.
162+ * @returns {string} number in octal format
163+ * @constructor
164+ */
165+var BIN2OCT = function (...values) : string {
166+ ArgsChecker.checkLengthWithin(values, 1, 2);
167+ if (typeof TypeCaster.firstValue(values[0]) === "boolean") {
168+ throw new CellError(ERRORS.VALUE_ERROR, "Function BIN2OCT parameter 1 expects text values. But '" + values[0] + "' is a boolean and cannot be coerced to a text.");
169+ }
170+ var n = TypeCaster.firstValueAsString(values[0]);
171+ var p = 10;
172+ if (values.length === 2) {
173+ p = TypeCaster.firstValueAsNumber(values[1]);
174+ }
175+ if (!(/^[01]{1,10}$/).test(n)) {
176+ throw new CellError(ERRORS.NUM_ERROR, "Input for BIN2OCT ('"+n+"') is not a valid binary representation.");
177+ }
178+
179+ if (n.length === 10 && n.substring(0, 1) === '1') {
180+ return (1073741312 + parseInt(n.substring(1), 2)).toString(8);
181+ }
182+
183+ if (p < 1 || p > 10) {
184+ throw new CellError(ERRORS.NUM_ERROR, "Function BIN2OCT parameter 2 value is " + p + ". Valid values are between 1 and 10 inclusive.");
185+ }
186+ p = Math.floor(p);
187+ var result = parseInt(n.toString(), 2).toString(8);
188+ if (p === 10) {
189+ return result;
190+ }
191+ if (p >= result.length) {
192+ var str = "";
193+ for (var i = 0; i < p - result.length - 1; i++) {
194+ str += "0";
195+ }
196+ return str + result;
197+ }
198+};
199+
200+/**
201+ * Converts a decimal number to signed octal format.
202+ * @param values[0] decimal_number - The decimal value to be converted to signed octal,provided as a string. For this
203+ * function, this value has a maximum of 536870911 if positive, and a minimum of -53687092 if negative.
204+ * @param values[1] significant_digits - [ OPTIONAL ] The number of significant digits to ensure in the result. If this
205+ * is greater than the number of significant digits in the result, the result is left-padded with zeros until the total
206+ * number of digits reaches significant_digits.
207+ * @returns {string} octal string representation of the decimal number
208+ * @constructor
209+ */
210+var DEC2OCT = function (...values) : string {
211+ ArgsChecker.checkLengthWithin(values, 1, 2);
212+ var n = TypeCaster.firstValueAsNumber(values[0]);
213+ if (n < 0) {
214+ n = Math.ceil(n);
215+ }
216+ if (n > 0) {
217+ n = Math.floor(n);
218+ }
219+ var p = 10;
220+ var placesPresent = false;
221+ if (values.length === 2) {
222+ p = Math.floor(TypeCaster.firstValueAsNumber(values[1]));
223+ placesPresent = true;
224+ }
225+ if (n < -53687092 || n > 536870911) {
226+ throw new CellError(ERRORS.NUM_ERROR, "Function DEC2OCT parameter 1 value is " + n + ". Valid values are between -53687092 and 536870911 inclusive.");
227+ }
228+ if (p < 1 || p > 10) {
229+ throw new CellError(ERRORS.NUM_ERROR, "Function DEC2OCT parameter 2 value is " + p + ". Valid values are between 1 and 10 inclusive.");
230+ }
231+ if (n < 0) {
232+ return (1073741824 + n).toString(8).toUpperCase();
233+ }
234+
235+ // Convert decimal number to hexadecimal
236+ var result = parseInt(n.toString(), 10).toString(8).toUpperCase();
237+ if (!placesPresent) {
238+ return result;
239+ }
240+ var str = "";
241+ for (var i = 0; i < p - result.length; i++) {
242+ str += "0";
243+ }
244+ return str + result.toUpperCase();
245+};
246+
247+
248+/**
249+ * Converts a decimal number to signed hexadecimal format.
250+ * @param values[0] decimal_number - The decimal value to be converted to signed hexadecimal, provided as a string. This
251+ * value has a maximum of 549755813887 if positive, and a minimum of -549755814888 if negative.
252+ * @param values[1] significant_digits - [ OPTIONAL ] - The number of significant digits to ensure in the result. If
253+ * this is greater than the number of significant digits in the result, the result is left-padded with zeros until the
254+ * total number of digits reaches significant_digits. This value is ignored if decimal_number is negative.
255+ * @returns {string} hexadecimal string representation of the decimal number
256+ * @constructor
257+ */
258+var DEC2HEX = function (...values) : string {
259+ ArgsChecker.checkLengthWithin(values, 1, 2);
260+ var n = TypeCaster.firstValueAsNumber(values[0]);
261+ if (n < 0) {
262+ n = Math.ceil(n);
263+ }
264+ if (n > 0) {
265+ n = Math.floor(n);
266+ }
267+ var p = 10;
268+ var placesPresent = false;
269+ if (values.length === 2) {
270+ p = Math.floor(TypeCaster.firstValueAsNumber(values[1]));
271+ placesPresent = true;
272+ }
273+ if (n < -549755813888 || n > 549755813887) {
274+ throw new CellError(ERRORS.NUM_ERROR, "Function DEC2HEX parameter 1 value is " + n + ". Valid values are between -549755813888 and 549755813887 inclusive.");
275+ }
276+ if (p < 1 || p > 10) {
277+ throw new CellError(ERRORS.NUM_ERROR, "Function DEC2HEX parameter 2 value is " + p + ". Valid values are between 1 and 10 inclusive.");
278+ }
279+ // Ignore places and return a 10-character hexadecimal number if number is negative
280+ if (n < 0) {
281+ return (1099511627776 + n).toString(16).toUpperCase();
282+ }
283+
284+ // Convert decimal number to hexadecimal
285+ var result = parseInt(n.toString(), 10).toString(16).toUpperCase();
286+ if (!placesPresent) {
287+ return result;
288+ }
289+ var str = "";
290+ for (var i = 0; i < p - result.length; i++) {
291+ str += "0";
292+ }
293+ return str + result;
294+};
295+
296+/**
297+ * Converts a decimal number to signed binary format.
298+ * @param values[0] decimal_number - The decimal value to be converted to signed binary, provided as a string. For this
299+ * function, this value has a maximum of 511 if positive, and a minimum of -512 if negative.
300+ * @param values[1] significant_digits - [ OPTIONAL ] The number of significant digits to ensure in the result. If this
301+ * is greater than the number of significant digits in the result, the result is left-padded with zeros until the total
302+ * number of digits reaches significant_digits.
303+ * @returns {string} signed binary string representation of the input decimal number.
304+ * @constructor
305+ */
306+var DEC2BIN = function (...values) : string {
307+ ArgsChecker.checkLengthWithin(values, 1, 2);
308+ var n = TypeCaster.firstValueAsNumber(values[0]);
309+ if (n < 0) {
310+ n = Math.ceil(n);
311+ }
312+ if (n > 0) {
313+ n = Math.floor(n);
314+ }
315+ if (n === 0 || n === 1) {
316+ return n.toString();
317+ }
318+ var p = 10;
319+ var placesPresent = false;
320+ if (values.length === 2) {
321+ p = Math.floor(TypeCaster.firstValueAsNumber(values[1]));
322+ placesPresent = true;
323+ }
324+
325+ if (n < -512 || n > 511) {
326+ throw new CellError(ERRORS.NUM_ERROR, "Function DEC2BIN parameter 1 value is " + n + ". Valid values are between -512 and 511 inclusive.");
327+ }
328+ if (p < 1 || p > 10) {
329+ throw new CellError(ERRORS.NUM_ERROR, "Function DEC2BIN parameter 2 value is " + p + ". Valid values are between 1 and 10 inclusive.");
330+ }
331+
332+ // Ignore places and return a 10-character binary number if number is negative
333+ if (n < 0) {
334+ var count = (9 - (512 + n).toString(2).length);
335+ var st = "";
336+ for (var i = 0; i < count; i++) {
337+ st += "0";
338+ }
339+ return "1" + st + (512 + n).toString(2);
340+ }
341+
342+ // Convert decimal number to binary
343+ var result = parseInt(n.toString(), 10).toString(2);
344+
345+ // Pad return value with leading 0s (zeros) if necessary
346+ if (p >= result.length) {
347+ var str = "";
348+ for (var i = 0; i < (p - result.length); i++) {
349+ str += "0";
350+ }
351+ var workingString = str + result;
352+ if (!placesPresent) {
353+ var returnString = "";
354+ for (var i = 0; i < workingString.length; i++) {
355+ var char = workingString[i];
356+ if (char === "1") {
357+ break;
358+ }
359+ returnString = workingString.slice(i+1);
360+ }
361+ return returnString;
362+ }
363+ return workingString;
364+ }
365+};
366+
367+
368 export {
369 CHAR,
370 CODE,
371 SPLIT,
372- CONCATENATE
373+ CONCATENATE,
374+ BIN2DEC,
375+ BIN2HEX,
376+ BIN2OCT,
377+ DEC2BIN,
378+ DEC2HEX,
379+ DEC2OCT
380 }
381\ No newline at end of file
382diff --git a/src/RawFormulas/RawFormulas.ts b/src/RawFormulas/RawFormulas.ts
383index 87c377d..2bdd90e 100644
384--- a/src/RawFormulas/RawFormulas.ts
385+++ b/src/RawFormulas/RawFormulas.ts
386@@ -34,6 +34,7 @@ import {
387 SINH,
388 SUM,
389 SQRT,
390+ SQRTPI,
391 PI,
392 POWER,
393 LOG,
394@@ -55,7 +56,9 @@ import {
395 COUNTIFS,
396 CEILING,
397 SUMSQ,
398- TRUNC
399+ TRUNC,
400+ RADIANS,
401+ DEGREES
402 } from "./Math";
403 import {
404 AND,
405@@ -70,7 +73,13 @@ import {
406 CHAR,
407 CODE,
408 SPLIT,
409- CONCATENATE
410+ CONCATENATE,
411+ BIN2DEC,
412+ BIN2HEX,
413+ BIN2OCT,
414+ DEC2BIN,
415+ DEC2HEX,
416+ DEC2OCT
417 } from "./Misc";
418 import {
419 CriteriaFunctionFactory,
420@@ -126,334 +135,6 @@ var SUMX2MY2 = Formula["SUMX2MY2"];
421 var SUMX2PY2 = Formula["SUMX2PY2"];
422 var YEARFRAC = Formula["YEARFRAC"];
423
424-
425-/**
426- * Returns the positive square root of the product of Pi and the given positive number.
427- * @param values[0] value - The number which will be multiplied by Pi and have the product's square root returned
428- * @returns {number} the positive square root of the product of Pi and the given positive number.
429- * @constructor
430- */
431-var SQRTPI = function (...values) : number{
432- ArgsChecker.checkLength(values, 1);
433- var n = TypeCaster.firstValueAsNumber(values[0]);
434- if (n < 0) {
435- throw new CellError(ERRORS.NUM_ERROR, "Function SQRTPI parameter 1 value is " + n + ". It should be greater than or equal to 0.");
436- }
437- return Math.sqrt(n * Math.PI);
438-};
439-
440-
441-/**
442- * Converts a signed binary number to decimal format.
443- * @param values[0] signed_binary_number - The signed 10-bit binary value to be converted to decimal, provided as a
444- * string. The most significant bit of signed_binary_number is the sign bit; that is, negative numbers are represented
445- * in two's complement format.
446- * @returns {number}
447- * @constructor
448- */
449-var BIN2DEC = function (...values) : number {
450- ArgsChecker.checkLength(values, 1);
451- if (typeof TypeCaster.firstValue(values[0]) === "boolean") {
452- throw new CellError(ERRORS.VALUE_ERROR, "Function BIN2DEC parameter 1 expects text values. But '" + values[0] + "' is a boolean and cannot be coerced to a text.");
453- }
454- var n = TypeCaster.firstValueAsString(values[0]);
455- if (!(/^[01]{1,10}$/).test(n)) {
456- throw new CellError(ERRORS.NUM_ERROR, "Input for BIN2DEC ('"+n+"') is not a valid binary representation.");
457- }
458-
459- if (n.length === 10 && n.substring(0, 1) === '1') {
460- return parseInt(n.substring(1), 2) - 512;
461- }
462- return parseInt(n, 2);
463-};
464-
465-
466-/**
467- * Converts a signed binary number to signed hexadecimal format.
468- * @param values[0] signed_binary_number - The signed 10-bit binary value to be converted to signed hexadecimal,
469- * provided as a string. The most significant bit of signed_binary_number is the sign bit; that is, negative numbers are
470- * represented in two's complement format.
471- * @param values[1] significant_digits - [ OPTIONAL ] - The number of significant digits to ensure in the result.
472- * @returns {string} string representation of a signed hexadecimal
473- * @constructor
474- */
475-var BIN2HEX = function (...values) : string {
476- ArgsChecker.checkLengthWithin(values, 1, 2);
477- if (typeof TypeCaster.firstValue(values[0]) === "boolean") {
478- throw new CellError(ERRORS.VALUE_ERROR, "Function BIN2HEX parameter 1 expects text values. But '" + values[0] + "' is a boolean and cannot be coerced to a text.");
479- }
480- var n = TypeCaster.firstValueAsString(values[0]);
481- var p = 10;
482- if (values.length === 2) {
483- p = TypeCaster.firstValueAsNumber(values[1]);
484- }
485- if (!(/^[01]{1,10}$/).test(n)) {
486- throw new CellError(ERRORS.NUM_ERROR, "Input for BIN2HEX ('"+n+"') is not a valid binary representation.");
487- }
488-
489- if (n.length === 10 && n.substring(0, 1) === '1') {
490- return (1099511627264 + parseInt(n.substring(1), 2)).toString(16).toUpperCase();
491- }
492-
493- if (p < 1 || p > 10) {
494- throw new CellError(ERRORS.NUM_ERROR, "Function BIN2HEX parameter 2 value is " + p + ". Valid values are between 1 and 10 inclusive.");
495- }
496- p = Math.floor(p);
497- // Convert decimal number to hexadecimal
498- var result = parseInt(n.toString(), 2).toString(16).toUpperCase();
499- if (p === 10) {
500- return result;
501- }
502- var str = "";
503- for (var i = 0; i < p - result.length; i++) {
504- str += "0";
505- }
506- return str + result;
507-};
508-
509-
510-/**
511- * Converts a signed binary number to signed octal format.
512- * @param values[0] signed_binary_number - The signed 10-bit binary value to be converted to signed octal, provided as a
513- * string. The most significant bit of signed_binary_number is the sign bit; that is, negative numbers are represented
514- * in two's complement format.
515- * @param values[1] significant_digits - [ OPTIONAL ] - The number of significant digits to ensure in the result. If
516- * this is greater than the number of significant digits in the result, the result is left-padded with zeros until the
517- * total number of digits reaches significant_digits.
518- * @returns {string} number in octal format
519- * @constructor
520- */
521-var BIN2OCT = function (...values) : string {
522- ArgsChecker.checkLengthWithin(values, 1, 2);
523- if (typeof TypeCaster.firstValue(values[0]) === "boolean") {
524- throw new CellError(ERRORS.VALUE_ERROR, "Function BIN2OCT parameter 1 expects text values. But '" + values[0] + "' is a boolean and cannot be coerced to a text.");
525- }
526- var n = TypeCaster.firstValueAsString(values[0]);
527- var p = 10;
528- if (values.length === 2) {
529- p = TypeCaster.firstValueAsNumber(values[1]);
530- }
531- if (!(/^[01]{1,10}$/).test(n)) {
532- throw new CellError(ERRORS.NUM_ERROR, "Input for BIN2OCT ('"+n+"') is not a valid binary representation.");
533- }
534-
535- if (n.length === 10 && n.substring(0, 1) === '1') {
536- return (1073741312 + parseInt(n.substring(1), 2)).toString(8);
537- }
538-
539- if (p < 1 || p > 10) {
540- throw new CellError(ERRORS.NUM_ERROR, "Function BIN2OCT parameter 2 value is " + p + ". Valid values are between 1 and 10 inclusive.");
541- }
542- p = Math.floor(p);
543- var result = parseInt(n.toString(), 2).toString(8);
544- if (p === 10) {
545- return result;
546- }
547- if (p >= result.length) {
548- var str = "";
549- for (var i = 0; i < p - result.length - 1; i++) {
550- str += "0";
551- }
552- return str + result;
553- }
554-};
555-
556-
557-
558-/**
559- * Converts an angle value in degrees to radians.
560- * @param values[0] angle - The angle to convert from degrees to radians.
561- * @returns {number} radians
562- * @constructor
563- */
564-var RADIANS = function (...values) {
565- ArgsChecker.checkLength(values, 1);
566- var d = TypeCaster.firstValueAsNumber(values[0]);
567- return d * Math.PI / 180;
568-};
569-
570-/**
571- * Converts an angle value in radians to degrees.
572- * @param values[0] angle - The angle to convert from radians to degrees.
573- * @returns {number} degrees
574- * @constructor
575- */
576-var DEGREES = function (...values) {
577- ArgsChecker.checkLength(values, 1);
578- var r = TypeCaster.firstValueAsNumber(values[0]);
579- return r * 180 / Math.PI;
580-};
581-
582-
583-/**
584- * Converts a decimal number to signed octal format.
585- * @param values[0] decimal_number - The decimal value to be converted to signed octal,provided as a string. For this
586- * function, this value has a maximum of 536870911 if positive, and a minimum of -53687092 if negative.
587- * @param values[1] significant_digits - [ OPTIONAL ] The number of significant digits to ensure in the result. If this
588- * is greater than the number of significant digits in the result, the result is left-padded with zeros until the total
589- * number of digits reaches significant_digits.
590- * @returns {string} octal string representation of the decimal number
591- * @constructor
592- */
593-var DEC2OCT = function (...values) : string {
594- ArgsChecker.checkLengthWithin(values, 1, 2);
595- var n = TypeCaster.firstValueAsNumber(values[0]);
596- if (n < 0) {
597- n = Math.ceil(n);
598- }
599- if (n > 0) {
600- n = Math.floor(n);
601- }
602- var p = 10;
603- var placesPresent = false;
604- if (values.length === 2) {
605- p = Math.floor(TypeCaster.firstValueAsNumber(values[1]));
606- placesPresent = true;
607- }
608- if (n < -53687092 || n > 536870911) {
609- throw new CellError(ERRORS.NUM_ERROR, "Function DEC2OCT parameter 1 value is " + n + ". Valid values are between -53687092 and 536870911 inclusive.");
610- }
611- if (p < 1 || p > 10) {
612- throw new CellError(ERRORS.NUM_ERROR, "Function DEC2OCT parameter 2 value is " + p + ". Valid values are between 1 and 10 inclusive.");
613- }
614- if (n < 0) {
615- return (1073741824 + n).toString(8).toUpperCase();
616- }
617-
618- // Convert decimal number to hexadecimal
619- var result = parseInt(n.toString(), 10).toString(8).toUpperCase();
620- if (!placesPresent) {
621- return result;
622- }
623- var str = "";
624- for (var i = 0; i < p - result.length; i++) {
625- str += "0";
626- }
627- return str + result.toUpperCase();
628-};
629-
630-
631-/**
632- * Converts a decimal number to signed hexadecimal format.
633- * @param values[0] decimal_number - The decimal value to be converted to signed hexadecimal, provided as a string. This
634- * value has a maximum of 549755813887 if positive, and a minimum of -549755814888 if negative.
635- * @param values[1] significant_digits - [ OPTIONAL ] - The number of significant digits to ensure in the result. If
636- * this is greater than the number of significant digits in the result, the result is left-padded with zeros until the
637- * total number of digits reaches significant_digits. This value is ignored if decimal_number is negative.
638- * @returns {string} hexadecimal string representation of the decimal number
639- * @constructor
640- */
641-var DEC2HEX = function (...values) : string {
642- ArgsChecker.checkLengthWithin(values, 1, 2);
643- var n = TypeCaster.firstValueAsNumber(values[0]);
644- if (n < 0) {
645- n = Math.ceil(n);
646- }
647- if (n > 0) {
648- n = Math.floor(n);
649- }
650- var p = 10;
651- var placesPresent = false;
652- if (values.length === 2) {
653- p = Math.floor(TypeCaster.firstValueAsNumber(values[1]));
654- placesPresent = true;
655- }
656- if (n < -549755813888 || n > 549755813887) {
657- throw new CellError(ERRORS.NUM_ERROR, "Function DEC2HEX parameter 1 value is " + n + ". Valid values are between -549755813888 and 549755813887 inclusive.");
658- }
659- if (p < 1 || p > 10) {
660- throw new CellError(ERRORS.NUM_ERROR, "Function DEC2HEX parameter 2 value is " + p + ". Valid values are between 1 and 10 inclusive.");
661- }
662- // Ignore places and return a 10-character hexadecimal number if number is negative
663- if (n < 0) {
664- return (1099511627776 + n).toString(16).toUpperCase();
665- }
666-
667- // Convert decimal number to hexadecimal
668- var result = parseInt(n.toString(), 10).toString(16).toUpperCase();
669- if (!placesPresent) {
670- return result;
671- }
672- var str = "";
673- for (var i = 0; i < p - result.length; i++) {
674- str += "0";
675- }
676- return str + result;
677-};
678-
679-/**
680- * Converts a decimal number to signed binary format.
681- * @param values[0] decimal_number - The decimal value to be converted to signed binary, provided as a string. For this
682- * function, this value has a maximum of 511 if positive, and a minimum of -512 if negative.
683- * @param values[1] significant_digits - [ OPTIONAL ] The number of significant digits to ensure in the result. If this
684- * is greater than the number of significant digits in the result, the result is left-padded with zeros until the total
685- * number of digits reaches significant_digits.
686- * @returns {string} signed binary string representation of the input decimal number.
687- * @constructor
688- */
689-var DEC2BIN = function (...values) : string {
690- ArgsChecker.checkLengthWithin(values, 1, 2);
691- var n = TypeCaster.firstValueAsNumber(values[0]);
692- if (n < 0) {
693- n = Math.ceil(n);
694- }
695- if (n > 0) {
696- n = Math.floor(n);
697- }
698- if (n === 0 || n === 1) {
699- return n.toString();
700- }
701- var p = 10;
702- var placesPresent = false;
703- if (values.length === 2) {
704- p = Math.floor(TypeCaster.firstValueAsNumber(values[1]));
705- placesPresent = true;
706- }
707-
708- if (n < -512 || n > 511) {
709- throw new CellError(ERRORS.NUM_ERROR, "Function DEC2BIN parameter 1 value is " + n + ". Valid values are between -512 and 511 inclusive.");
710- }
711- if (p < 1 || p > 10) {
712- throw new CellError(ERRORS.NUM_ERROR, "Function DEC2BIN parameter 2 value is " + p + ". Valid values are between 1 and 10 inclusive.");
713- }
714-
715- // Ignore places and return a 10-character binary number if number is negative
716- if (n < 0) {
717- var count = (9 - (512 + n).toString(2).length);
718- var st = "";
719- for (var i = 0; i < count; i++) {
720- st += "0";
721- }
722- return "1" + st + (512 + n).toString(2);
723- }
724-
725- // Convert decimal number to binary
726- var result = parseInt(n.toString(), 10).toString(2);
727-
728- // Pad return value with leading 0s (zeros) if necessary
729- if (p >= result.length) {
730- var str = "";
731- for (var i = 0; i < (p - result.length); i++) {
732- str += "0";
733- }
734- var workingString = str + result;
735- if (!placesPresent) {
736- var returnString = "";
737- for (var i = 0; i < workingString.length; i++) {
738- var char = workingString[i];
739- if (char === "1") {
740- break;
741- }
742- returnString = workingString.slice(i+1);
743- }
744- return returnString;
745- }
746- return workingString;
747- }
748-};
749-
750-
751-
752 export {
753 __COMPLEX,
754