@@ -135,8 +135,7 @@ public static void cmdAverage(StackObj calcStack, String arg) {
135
135
BigDecimal mean = Math .mean (calcStack );
136
136
137
137
// If we are not going to keep the stack (the default) clear it
138
- if (!keepFlag )
139
- calcStack .clear ();
138
+ if (!keepFlag ) calcStack .clear ();
140
139
141
140
// Add the average to the stack
142
141
calcStack .push (mean );
@@ -182,14 +181,26 @@ public static void cmdCopy(StackObj calcStack, String arg) {
182
181
return ;
183
182
}
184
183
185
- // Determine line number to copy
184
+ // If Arg is blank, set arg to the top of the stack number so we copy the last value
185
+ if (arg .isBlank ()) arg = "1" ;
186
+
187
+ // Determine if the provided argument is not a number
188
+ if (!Math .isNumeric (arg )) {
189
+ Output .printColorln (Ansi .Color .RED , "ERROR: '" + arg + "' is not a valid line number" );
190
+ return ;
191
+ }
192
+
186
193
try {
194
+ // Grab the value from the argument
187
195
lineNumToCopy = Integer .parseInt (arg );
188
- } catch (NumberFormatException ex ) {
189
- if (!arg .isBlank ()) {
190
- Output .printColorln (Ansi .Color .RED , "ERROR: '" + arg + "' is not a valid line number" );
191
- return ;
196
+
197
+ // If a relative number is provided (i.e. `-1`) then set the value to copy back from the top of the stack
198
+ if (lineNumToCopy <= -1 ) {
199
+ lineNumToCopy = java .lang .Math .abs (lineNumToCopy ); // Remove the negative sign
200
+ lineNumToCopy = 1 + lineNumToCopy ;
192
201
}
202
+ } catch (Exception ex ) {
203
+ Output .printColorln (Ansi .Color .RED , "ERROR: '" + arg + "' is not a valid relative value" );
193
204
}
194
205
195
206
// Save current calcStack to the undoStack
@@ -200,14 +211,14 @@ public static void cmdCopy(StackObj calcStack, String arg) {
200
211
// Copy the provided number if it's valid
201
212
try {
202
213
// Ensure the number entered is valid
203
- if (lineNumToCopy < 1 || lineNumToCopy > calcStack .size ()) {
204
- Output .printColorln (Ansi .Color .RED , "Invalid line number entered: " + lineNumToCopy );
214
+ if (lineNumToCopy > calcStack .size ()) {
215
+ Output .printColorln (Ansi .Color .RED , "Invalid line number entered" );
205
216
} else {
206
217
// Perform the copy
207
218
calcStack .push (calcStack .get (calcStack .size () - lineNumToCopy ));
208
219
}
209
220
} catch (Exception e ) {
210
- Output .printColorln (Ansi .Color .RED , "Error parsing line number for element copy: '" + lineNumToCopy + "'" );
221
+ Output .printColorln (Ansi .Color .RED , "Error parsing line number for copy: '" + lineNumToCopy + "'" );
211
222
Output .debugPrintln (e .getMessage ());
212
223
}
213
224
}
@@ -479,13 +490,12 @@ public static void cmdLinearRegression(StackObj calcStack, String args) {
479
490
sumY2 = sumY2 .add (new BigDecimal (String .valueOf (y )).pow (2 ));
480
491
481
492
// Line by line debug output
482
- Output .debugPrintln (
483
- "#" + i + ":\t x:" + x + "\t y:" + y + "\t XY:" + y .multiply (new BigDecimal (String .valueOf (x ))) + "\t X2:" + (x * x ) + "\t Y2:" + y .pow (2 ));
493
+ Output .debugPrintln ("#" + i + ":\t x:" + x + "\t y:" + y + "\t XY:" + y .multiply (new BigDecimal (String .valueOf (x ))) + "\t X2:" + (x * x ) + "\t Y2:" + y .pow (2 ));
484
494
}
485
495
486
- // Calculate the remaining values
487
- // Reference Formula: a = ((sumY * sumX2) - (sumX * sumXY)) / ((n * sumX2) - (sumX * sumX));
488
- // Reference Formula: b = ((n * sumXY) - (sumX * sumY)) / ((n * sumX2) - (sumX * sumX));
496
+ // Calculate the remaining values
497
+ // Reference Formula: a = ((sumY * sumX2) - (sumX * sumXY)) / ((n * sumX2) - (sumX * sumX));
498
+ // Reference Formula: b = ((n * sumXY) - (sumX * sumY)) / ((n * sumX2) - (sumX * sumX));
489
499
490
500
BigDecimal a_top = sumY .multiply (sumX2 ).subtract (sumX .multiply (sumXY ));
491
501
BigDecimal a_bottom = n .multiply (sumX2 ).subtract (sumX .pow (2 ));
@@ -583,8 +593,7 @@ public static boolean cmdMaximum(StackObj calcStack) {
583
593
584
594
// Loop through the stack and look for the largest value
585
595
for (int i = 0 ; i < calcStack .size (); i ++) {
586
- if (calcStack .get (i ).compareTo (largestValue ) > 0 )
587
- largestValue = calcStack .get (i );
596
+ if (calcStack .get (i ).compareTo (largestValue ) > 0 ) largestValue = calcStack .get (i );
588
597
}
589
598
590
599
// Add the lowest value to the stack
@@ -626,8 +635,7 @@ public static void cmdMedian(StackObj calcStack, String arg) {
626
635
BigDecimal median = Math .median (calcStack );
627
636
628
637
// If we are not going to keep the stack (the default) clear it
629
- if (!keepFlag )
630
- calcStack .clear ();
638
+ if (!keepFlag ) calcStack .clear ();
631
639
632
640
// Add the average to the stack
633
641
calcStack .push (median );
@@ -654,8 +662,7 @@ public static boolean cmdMinimum(StackObj calcStack) {
654
662
655
663
// Loop through the stack and look for the lowest value
656
664
for (int i = 0 ; i < calcStack .size (); i ++) {
657
- if (calcStack .get (i ).compareTo (lowestValue ) < 1 )
658
- lowestValue = calcStack .get (i );
665
+ if (calcStack .get (i ).compareTo (lowestValue ) < 1 ) lowestValue = calcStack .get (i );
659
666
}
660
667
661
668
// Add the lowest value to the stack
0 commit comments