forked from Oatworm/MathLibrary.cmd
- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMathLibrary.cmd
More file actions
Latest commit
815 lines (721 loc) · 21.6 KB
/
Copy pathMathLibrary.cmd
File metadata and controls
815 lines (721 loc) · 21.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
@ECHOOFF
:: MathLibrary.cmd
:: David Colborne - david@colbornemmx.com
:: https://github.com/Oatworm/MathLibrary.cmd
:: This project is licensed using the Simplified BSD license.
:: For additional details, see the LICENSE file on GitHub.
:: To use MathLibrary.cmd, simply call it:
:: MathLibrary.cmd 1 / 2
:: It will (eventually) return:
:: 0.5
:ExtUI
:: Front-end for MathLibrary.
:: Requires all subroutines listed here.
:: Accepts three arguments, passed when script is called:
:: %1 - First number
:: %2 - Operation to be performed (+, -, *, /)
:: %3 - Second number
:: %4 - Result (optional - passed by reference)
::
:: Example invocation:
:: CALL MathLibrary.cmd 1 + 2 _Result
:: |--> Adds 1+2 and saves result in _Result
SET_Num1=%1
SET_Op=%2
SET_Num2=%3
SET_CmdResult=
IF%_Op%==+ (
CALL :ExtAdd%_Num1%%_Num2% _CmdResult
)
IF%_Op%==- (
CALL :ExtSubtract%_Num1%%_Num2% _CmdResult
)
IF%_Op%==* (
CALL :ExtMultiply%_Num1%%_Num2% _CmdResult
)
IF%_Op%==/ (
CALL :ExtDivision%_Num1%%_Num2% _CmdResult
)
IF /I %_Op%==com (
CALL :ExtCompare%_Num1%%_Num2% _CmdResult
)
IFNOT [%4]==[] (
SET%4=%_CmdResult%
GOTO :EOF
)
ECHO:%_CmdResult%
GOTO :EOF
:ExtDivision
SETLOCALEnableDelayedExpansion
:: Used to divide arbitrary numbers together
:: Requires - :ExtDim, :ExtMatchPad, :ExtUnDecimal, :ExtCompare, :ExtSubtract, :ExtAdd, :ExtPad
:: Accepts three parameters:
:: %1, %2 - The numerator and denominator, respectively (passed by value)
:: %3 - The result (passed by reference)
::
:: Example invocation:
:: CALL :ExtDivision %_Num1% %_Num2% _Result
:: |--> Divides _Num1 by _Num2 and returns result in _Result
SET_DivN=%1
SET_DivD=%2
SET_DivResult=
SET_IntLen=1
SET_DivNRtoL=0
SET_DivDRtoL=0
SET_DivNegFlag=0
IF%_DivN:~0,1%==- (
IF%_DivD:~0,1%==- (
SET_DivN=%_DivN:~1%
SET_DivD=%_DivD:~1%
) ELSE (
SET_DivN=%_DivN:~1%
SET_DivNegFlag=1
)
) ELSE (
IF%_DivD:~0,1%==- (
SET_DivD=%_DivD:~1%
SET_DivNegFlag=1
)
)
CALL :ExtDim%_DivN% _DivNLen _DivNDec
CALL :ExtDim%_DivD% _DivDLen _DivDDec
IF%_DivNDec%LEQ%_DivNLen% (
CALL :ExtUnDecimal _DivN %_DivNLen%%_DivNDec%
SET /A _DivNRtoL=_DivNLen-_DivNDec
SET_DivDecFlag=1
)
IF%_DivDDec%LEQ%_DivDLen% (
CALL :ExtUnDecimal _DivD %_DivDLen%%_DivDDec%
SET /A _DivDRtoL=_DivDLen-_DivDDec
SET /A _DivD_L_1=_DivDLen-1
SET /A _DivD_D_1=_DivD_L_1+1
SET_DivDecFlag=1
)
:: Set max precision here!
IF%_DivNLen%GEQ%_DivDLen% (
SET /A _DivMaxPrec=_DivNLen+10
) ELSE (
SET /A _DivMaxPrec=_DivDLen+10
)
:: Multiplication is expensive - let's do it as little as needed.
:: Meaning NEVER.
SET_DivD_1=%_DivD%
IFNOTDEFINED _DivD_L_1 SET_DivD_L_1=%_DivDLen%
IFNOTDEFINED _DivD_D_1 SET_DivD_D_1=%_DivDDec%
SET_DivD_Prev=%_DivD%
FOR /L %%GIN (2,1,9) DO (
CALL :ExtAdd!_DivD_Prev!%_DivD% _DivD_%%G
SET_DivD_Prev=!_DivD_%%G!
CALL :ExtDim!_DivD_%%G! _DivD_L_%%G _DivD_D_%%G
)
:: Long division time...
SET_DivLoop=0
SET_DivCoef=
SET_DivResDecPos=
SET_DivCoef_L=0
SET_DivCoef_D=1
:ExtDivisionLoop
IF%_DivLoop%GTR%_DivMaxPrec%GOTO ExtEndDivisionLoop
SET_DivResCol=0
CALLSET_DivCol=%%_DivN:~%_DivLoop%,%_IntLen%%%
IFNOTDEFINED _DivCol (
IF [%_DivCoef%]==[0] GOTO ExtEndDivisionLoop
SET_DivCol=0
SET_DivDecFlag=1
IFNOTDEFINED _DivResDecPos SET /A _DivResDecPos=_DivLoop+1
)
IFNOT [%_DivCoef%]==[0] (
SET_DivCoef=%_DivCoef%%_DivCol%
SET /A _DivCoef_L+=1
SET /A _DivCoef_D+=1
) ELSE (
SET_DivCoef=%_DivCol%
SET /A _DivCoef_L=1
SET /A _DivCoef_D=2
)
IF%_DivCoef%EQU0GOTO ExtContinueDivLoop
FOR /L %%HIN (9,-1,0) DO (
CALL :ExtCompare!_DivD_%%H!%_DivCoef% _DivInComp !_DivD_L_%%H!!_DivD_D_%%H!%_DivCoef_L%%_DivCoef_D%
IFNOT!_DivInComp!==GTR (
SET_DivResCol=%%H
CALL :ExtSubtract%_DivCoef%!_DivD_%%H! _DivCoef
CALL :ExtDim!_DivCoef! _DivCoef_L _DivCoef_D
GOTO ExtContinueDivLoop
)
)
:ExtContinueDivLoop
SET_DivResult=%_DivResult%%_DivResCol%
SET /A _DivLoop+=1
GOTO ExtDivisionLoop
:ExtEndDivisionLoop
IFDEFINED _DivDecFlag (
CALL :ExtDim%_DivResult% _DivResLen _DivResTmp
IFNOTDEFINED _DivResDecPos SET /A _DivResDecPos=_DivResTmp
SET /A "_DivResDecPos=_DivResDecPos-_DivNRtoL+_DivDRtoL"
CALL :ExtReDecimal _DivResult _DivResLen !_DivResDecPos!
SET /A _DivRightPad=_DivDRtoL-_DivNRtoL-_DivResLen+_DivDLen
IF!_DivRightPad!GTR0 (
CALL :ExtPad _DivResult !_DivRightPad! R _DivResLen _DivResDecPos
)
) ELSE (
CALL :ExtDim%_DivResult% _DivResLen _DivResTmp
)
:ExtDivStripLeadingZeroes
SET_Zero=0
SET_IntLen=2
CALLSET_DivLInt=%%_DivResult:~%_Zero%,%_IntLen%%%
SET_DivLInt0=%_DivLInt:~0,1%
SET_DivLInt1=%_DivLInt:~1,1%
IFNOT%_DivLInt0%==0GOTO ExtDivDoneStrippingLeadingZeroes
IFNOTDEFINED _DivLInt1 GOTO ExtDivDoneStrippingLeadingZeroes
IF%_DivLInt1%==. GOTO ExtDivDoneStrippingLeadingZeroes
SET /A _DivResLen-=1
CALLSET_DivResult=%%_DivResult:~-%_DivResLen%%%
GOTO ExtDivStripLeadingZeroes
:ExtDivDoneStrippingLeadingZeroes
:ExtDivisionReturnResult
IF%_DivNegFlag%==1SET_DivResult=-%_DivResult%
ENDLOCAL&SET%3=%_DivResult%
GOTO :EOF
:ExtMultiply
SETLOCALEnableDelayedExpansion
:: Used to multiply arbitrary numbers together
:: Requires - :ExtDim, :ExtMatchPad, :ExtAdd, :ExtUnDecimal, :ExtReDecimal
:: Accepts three parameters:
:: %1, %2 - The two numbers being multipled (passed by value)
:: %3 - The result (passed by reference)
::
:: Example invocation:
:: CALL :ExtMultiply %_Num1% %_Num2% _Result
:: |--> Multiplies _Num1 and _Num2 together, saves result in _Result
SET_Mul1=%1
SET_Mul2=%2
SET_MulResult=0
SET_MulCarry=0
SET_IntLen=1
SET_MulNegFlag=0
IF%_Mul1:~0,1%==- (
IF%_Mul2:~0,1%==- (
SET_Mul1=%_Mul1:~1%
SET_Mul2=%_Mul2:~1%
) ELSE (
SET_Mul1=%_Mul1:~1%
SET_MulNegFlag=1
)
) ELSE (
IF%_Mul2:~0,1%==- (
SET_Mul2=%_Mul2:~1%
SET_MulNegFlag=1
)
)
CALL :ExtDim%_Mul1% _MulLen1 _MulDec1
CALL :ExtDim%_Mul2% _MulLen2 _MulDec2
IF%_MulDec1%LEQ%_MulLen1% (
CALL :ExtUnDecimal _Mul1 %_MulLen1%%_MulDec1%
SET /A _MulDecPos1=_MulLen1-_MulDec1
SET_MulDecPos2=0
SET /A _MulLen1-=1
SET_MulDecFlag=1
)
IF%_MulDec2%LEQ%_MulLen2% (
CALL :ExtUnDecimal _Mul2 %_MulLen2%%_MulDec2%
SET /A _MulDecPos2=_MulLen2-_MulDec2
IFNOTDEFINED _MulDecPos1 SET_MulDecPos1=0
SET /A _MulLen2-=1
SET_MulDecFlag=1
)
FOR /L %%GIN (%_MulLen1%,-1,1) DO (
SET /A _BMulPos=%%G-1
SET_MulColRes=
SET_MulCarry=0
SET /A _MulCol=%%G+1
FOR /L %%HIN (!_MulCol!,1,%_MulLen1%) DOSET_MulColRes=0!_MulColRes!
FOR /L %%IIN (%_MulLen2%,-1,1) DO (
SET /A _TMulPos=%%I-1
CALLSET_MulInt1=%%_Mul1:~!_BMulPos!,%_IntLen%%%
CALLSET_MulInt2=%%_Mul2:~!_TMulPos!,%_IntLen%%%
SET /A _MulByPos=_MulInt1*_MulInt2+_MulCarry
IF!_MulByPos!GEQ10 (
SET_MulCarry=!_MulByPos:~0,1!
SET_MulByPos=!_MulByPos:~1,1!
) ELSE (
SET_MulCarry=0
)
SET_MulColRes=!_MulByPos!!_MulColRes!
)
IF!_MulCarry!GTR0SET_MulColRes=!_MulCarry!!_MulColRes!
CALL :ExtAdd!_MulColRes!!_MulResult! _MulResult
)
IFDEFINED _MulDecFlag (
SET /A _MulDecPos=_MulDecPos1+_MulDecPos2
CALL :ExtDim%_MulResult% _MulResLen _MulResDec
SET /A _MulResDec=_MulResLen-_MulDecPos+1
CALL :ExtReDecimal _MulResult _MulResLen !_MulResDec!
)
:ExtMultiplyReturnResult
IF%_MulNegFlag%==1SET_MulResult=-%_MulResult%
ENDLOCAL&SET%3=%_MulResult%
GOTO :EOF
:ExtSubtract
SETLOCALEnableDelayedExpansion
:: Used to subtract an arbitrary number from another
:: Requires - :ExtMatchPad, :ExtDim, :ExtCompare
:: Accepts three parameters:
:: %1 - Minuend (passed by value)
:: %2 - Subtrahend (passed by value)
:: %3 - The result (passed by reference)
::
:: Example invocation:
:: CALL :ExtSubtract %_Num1% %_Num2% _Result
:: |--> Subtracts _Num2 from _Num1 and saves result in _Result
SET_SubtractResult=
SET_SubtractCarry=0
SET_IntLen=1
SET_SubNegChk1=%1
SET_SubNegChk2=%2
SET_SubtractNegSwap=0
SET_Zero=0
SET_One=1
IF%_SubNegChk1:~0,1%==- (
IFNOT%_SubNegChk2:~0,1%==- (
SET_SubtractNegSwap=1
SET_SubtractM=%_SubNegChk1:~1%
SET_SubtractS=%_SubNegChk2%
CALL :ExtAdd!_SubtractM!!_SubtractS! _SubtractResult
GOTO ExtSubtractReturnResult
) ELSE (
CALL :ExtCompare%1%2 _SubComResult
IF!_SubComResult!==GTR (
SET_SubtractM=%_SubNegChk2:~1%
SET_SubtractS=%_SubNegChk1:~1%
) ELSE (
SET_SubtractM=%_SubNegChk1:~1%
SET_SubtractS=%_SubNegChk2:~1%
SET_SubtractNegSwap=1
)
)
) ELSE (
IF%_SubNegChk2:~0,1%==- (
SET_SubtractM=%_SubNegChk1%
SET_SubtractS=%_SubNegChk2:~1%
CALL :ExtAdd!_SubtractM!!_SubtractS! _SubtractResult
GOTO ExtSubtractReturnResult
) ELSE (
CALL :ExtCompare%1%2 _SubComResult
IF!_SubComResult!==LSS (
SET_SubtractM=%2
SET_SubtractS=%1
SET_SubtractNegSwap=1
) ELSE (
SET_SubtractM=%1
SET_SubtractS=%2
)
)
)
CALL :ExtDim%_SubtractM% _SubtractMLen _SubtractMDec
CALL :ExtDim%_SubtractS% _SubtractSLen _SubtractSDec
CALL :ExtMatchPad _SubtractM _SubtractS _SubtractLen _SubtractDec %_SubtractMLen%%_SubtractMDec%%_SubtractSLen%%_SubtractSDec%
FOR /L %%GIN (%_SubtractLen%,-1,1) DO (
SET /A _SubtractPos=%%G-1
CALLSET_SubtractIntM=%%_SubtractM:~!_SubtractPos!,%_IntLen%%%
CALLSET_SubtractIntS=%%_SubtractS:~!_SubtractPos!,%_IntLen%%%
IF!_SubtractIntM!==. (
SET_SubtractResult=.!_SubtractResult!
) ELSE (
SET /A _SubtractByPos=_SubtractIntM-_SubtractIntS-_SubtractCarry
IF!_SubtractByPos!LSS0 (
SET_SubtractCarry=1
SET /A _SubtractByPos+=10
) ELSE (
SET_SubtractCarry=0
)
SET_SubtractResult=!_SubtractByPos!!_SubtractResult!
)
)
CALL :ExtDim%_SubtractResult% _SubtractResLen _SubtractResDec
IF%_SubtractResDec%GTR%_SubtractResLen%GOTO ExtSubtractDoneStrippingTrailingZeroes
:ExtSubtractStripTrailingZeroes
SET /A _SubStripMax=_SubtractResLen-_SubtractResDec+1
SET_SubStripOff=0
FOR /L %%GIN (1,1,%_SubStripMax%) DO (
SET_SubStripPos=%%G
CALLSET_SubZChk=%%_SubtractResult:~-!_SubStripPos!,%_One%%%
IFNOT!_SubZChk!==0 (
GOTO ExtSubtractFoundTrailingZeroes
) ELSESET_SubStripOff=%%G
)
:ExtSubtractFoundTrailingZeroes
IF%_SubZChk%==. SET /A _SubStripOff+=1
CALLSET_SubtractResult=%%_SubtractResult:~%_Zero%,-%_SubStripOff%%%
:ExtSubtractDoneStrippingTrailingZeroes
:ExtSubtractStripLeadingZeroes
CALL :ExtDim%_SubtractResult% _SubtractResLen _SubtractResDec
IF%_SubtractResLen%GTR%_SubtractResDec% (
SET /A _SubStripLoopLen=%_SubtractResDec%-2
) ELSESET /A _SubStripLoopLen=%_SubtractResLen%-1
FOR /L %%GIN (0,1,%_SubStripLoopLen%) DO (
SET_SubStripPos=%%G
SET_SubStripOff=%%G
CALLSET_SubZChk=%%_SubtractResult:~!_SubStripPos!,%_One%%%
IFNOT!_SubZChk!==0GOTO ExtSubtractFoundLeadingZeroes
)
:ExtSubtractFoundLeadingZeroes
CALLSET_SubtractResult=%%_SubtractResult:~%_SubStripOff%%%
:ExtSubtractDoneStrippingLeadingZeroes
:ExtSubtractReturnResult
IF%_SubtractNegSwap%EQU1SET_SubtractResult=-%_SubtractResult%
ENDLOCAL&SET%3=%_SubtractResult%
GOTO :EOF
:ExtAdd
SETLOCALEnableDelayedExpansion
:: Used to add two arbitrary numbers together.
:: Requires - :ExtMatchPad, :ExtDim
:: Accepts three parameters:
:: %1, %2 - The numbers to be added together (passed by value)
:: %3 - The result (passed by reference)
::
:: Example invocation:
:: CALL :ExtAdd %_Num1% %_Num2% _Result
:: |--> Sums _Num1 and _Num2 and saves result in _Result
SET_Add1=%1
SET_Add2=%2
SET_AddResult=
SET_AddCarry=0
SET_IntLen=1
SET_AddNegFlag=0
IF%_Add1:~0,1%==- (
IF%_Add2:~0,1%==- (
SET_AddNegFlag=1
SET_Add1=%_Add1:~1%
SET_Add2=%_Add2:~1%
GOTO ExtAddNums
) ELSE (
SET_Add1=%_Add1:~1%
CALL :ExtSubtract%_Add2%!_Add1! _AddResult
GOTO ExtAddReturnResult
)
) ELSE (
IF%_Add2:~0,1%==- (
SET_Add2=%_Add2:~1%
CALL :ExtSubtract%_Add1%!_Add2! _AddResult
GOTO ExtAddReturnResult
)
)
:ExtAddNums
CALL :ExtDim%_Add1% _Add1Len _Add1Dec
CALL :ExtDim%_Add2% _Add2Len _Add2Dec
CALL :ExtMatchPad _Add1 _Add2 _AddLen _AddDec %_Add1Len%%_Add1Dec%%_Add2Len%%_Add2Dec%
FOR /L %%GIN (%_AddLen%,-1,1) DO (
SET /A _AddPos=%%G-1
CALLSET_AddInt1=%%_Add1:~!_AddPos!,%_IntLen%%%
CALLSET_AddInt2=%%_Add2:~!_AddPos!,%_IntLen%%%
IF!_AddInt1!==. (
SET_AddResult=.!_AddResult!
) ELSE (
SET /A _AddByPos=_AddInt1+_AddInt2+_AddCarry
IF!_AddByPos!GEQ10 (
SET_AddCarry=1
SET /A _AddByPos-=10
) ELSE (
SET_AddCarry=0
)
SET_AddResult=!_AddByPos!!_AddResult!
)
)
IF%_AddCarry%EQU1SET_AddResult=1%_AddResult%
IF%_AddNegFlag%EQU1SET_AddResult=-%_AddResult%
:ExtAddReturnResult
ENDLOCAL&SET%3=%_AddResult%
GOTO :EOF
:ExtCompare
SETLOCALEnableDelayedExpansion
:: Used to compare two numbers.
:: Requires - :ExtDim, :ExtMatchPad
:: Accepts the following parameters:
:: %1, %2 - The numbers to compare, evaluated in order (passed by value)
:: %3 - The result, returned as GTR, LSS, or EQU
::
:: Optional arguments:
:: %4, %5 - Length and decimal position of %1
:: %6, %7 - Length and decimal position of %2
::
:: Example invocation:
:: CALL :ExtCompare %_Num1% %_Num2% _Result
:: |--> If _Num1 is 1 and _Num2 is 3, returns LSS.
:: |--> If _Num1 is 3 and _Num2 is 1, returns GTR
:: |--> If _Num1 and _Num2 are both 3, returns EQU
SET_Com1=%1
SET_Com2=%2
SET_ComLen=
SET_IntLen=1
SET_ComResult=EQU
SET_ComNegFlag=0
IF%_Com1:~0,1%==- (
IFNOT%_Com2:~0,1%==- (
SET_ComResult=LSS
GOTO ExtCompareReturnResult
) ELSE (
SET_ComNegFlag=1
SET_Com1=%_Com1:~1%
SET_Com2=%_Com2:~1%
GOTO ExtCompareCheck
)
)
IF%_Com2:~0,1%==- (
SET_ComResult=GTR
GOTO ExtCompareReturnResult
)
:ExtCompareCheck
IFNOT [%7]==[] (
SET_Com2Len=%6
SET_Com2Dec=%7
) ELSECALL :ExtDim%_Com2% _Com2Len _Com2Dec
IFNOT [%5]==[] (
SET_Com1Len=%4
SET_Com1Dec=%5
) ELSECALL :ExtDim%_Com1% _Com1Len _Com1Dec
SET /A _Com1IntLen=_Com1Len-_Com1Dec
IF%_Com1IntLen%==-1SET_Com1IntLen=%_Com1Len%
SET /A _Com2IntLen=_Com2Len-_Com2Dec
IF%_Com2IntLen%==-1SET_Com2IntLen=%_Com2Len%
IF%_Com1IntLen%GTR%_Com2IntLen% (
SET_ComResult=GTR
GOTO ExtCompareReturnResult
)
IF%_Com2IntLen%GTR%_Com1IntLen% (
SET_ComResult=LSS
GOTO ExtCompareReturnResult
)
CALL :ExtMatchPad _Com1 _Com2 _ComLen _ComDec %_Com1Len%%_Com1Dec%%_Com2Len%%_Com2Dec%
FOR /L %%GIN (0,1,%_ComLen%) DO (
SET /A _ComPos=%%G
CALLSET_ComLInt=%%_Com1:~!_ComPos!,%_IntLen%%%
CALLSET_ComRInt=%%_Com2:~!_ComPos!,%_IntLen%%%
IFNOT!_ComLInt!==. (
IF!_ComLInt!LSS!_ComRInt! (
SET_ComResult=LSS
GOTO ExtCompareCheckNeg
)
IF!_ComLInt!GTR!_ComRInt! (
SET_ComResult=GTR
GOTO ExtCompareCheckNeg
)
)
)
:ExtCompareCheckNeg
IF%_ComNegFlag%EQU1 (
IF%_ComResult%==GTR SET_ComResult=LSS
IF%_ComResult%==LSS SET_ComResult=GTR
)
:ExtCompareReturnResult
ENDLOCAL&SET%3=%_ComResult%
GOTO :EOF
:ExtUnDecimal
SETLOCALEnableDelayedExpansion
:: Used to strip decimal points out of numbers.
:: Requires - :ExtDim
:: Necessary for division and multiplication. See :ExtReDecimal.
:: Accepts the following parameters:
:: %1 - The number to strip (passed by reference)
:: %2, %3 (Optional) - Length and Decimal position of the stripped number
::
:: Example invocation:
:: CALL :ExtUnDecimal _Num1
:: |--> If _Num1=123.45, returns 12345
:: Note that you should store the decimal position for use in :ExtReDecimal
:: elsewhere.
SET_UnDNum=!%1!
SET_UnDResult=
SET_UnDLeft=
SET_UnDRight=
IFNOT [%3]==[] (
SET_UnDLen=%2
SET_UnDDec=%3
) ELSECALL :ExtDim%_UnDNum% _UnDLen _UnDDec
IF%_UnDDec%LEQ%_UnDLen% (
SET_UnD0=0
SET /A _UnDLLen=_UnDDec-1
SET /A _UnDRLen=_UnDLen-_UnDDec
CALLSET_UnDLeft=%%_UnDNum:~!_UnD0!,!_UnDLLen!%%
CALLSET_UnDRight=%%_UnDNum:~%_UnDDec%,!_UnDRLen!%%
SET_UnDResult=!_UnDLeft!!_UnDRight!
) ELSE (
SET_UnDResult=!_UnDNum!
)
ENDLOCAL&SET%1=%_UnDResult%
GOTO :EOF
:ExtReDecimal
SETLOCALEnableDelayedExpansion
:: Used to place a decimal point back into a number.
:: Useful for proper decimal placement when multiplying or dividing.
:: Accepts the following parameters:
:: %1 - The number to place the decimal into (passed by reference)
:: %2 - The length of the number string (passed by reference)
:: %3 - The decimal position (passed by value, counting from the left)
::
:: Example invocation:
:: CALL :ExtReDecimal _Num1 _NumLen 6
:: |--> If _Num1=1234567, returns 12345.67 with a new length of 8.
::
:: Note that this subroutine assumes that the number being changed is
:: an integer - there's no error checking.
SET_ReDNum=!%1!
SET_ReDLen=!%2!
SET /A _ReDPos=%3
SET_ReDLeft=
SET_ReDRight=
SET_ReDResult=
IF%_ReDPos%LEQ%_ReDLen% (
SET_ReD0=0
SET /A _ReDLLen=_ReDPos-1
SET /A _ReDLen+=1
CALLSET_ReDLeft=%%_ReDNum:~!_ReD0!,!_ReDLLen!%%
CALLSET_ReDRight=%%_ReDNum:~!_ReDLLen!,!_ReDLen!%%
SET_ReDResult=!_ReDLeft!.!_ReDRight!
) ELSE (
SET_ReDResult=!_ReDNum!
)
ENDLOCAL&SET%1=%_ReDResult%& SET %2=%_ReDLen%
GOTO :EOF
:ExtMatchPad
SETLOCALEnableDelayedExpansion
:: Used to match length and decimal position of two number strings
:: Requires - :ExtPad, :ExtDim
:: Accepts the following parameters (passed by reference):
:: %1, %2 - Two numbers to match
:: %3, %4 - Length and decimal position of padded numbers.
::
:: Optionally accepts the following (passed by value)
:: %5, %6 - Length and decimal position of %1
:: %7, %8 - Length and decimal position of %2
::
:: Example invocation:
:: CALL :ExtMatchPad _Num1 _Num2
:: |--> Makes _Num1 and _Num2 have the same length
:: and decimal position
:: So, 1234.5 and 1.2345 become 1234.5000 and 0001.2345.
SET_MatchPad1=!%1!
SET_MatchPad2=!%2!
SET_MatchPadL=
SET_MatchPadD=
IFNOT [%8]==[] (
SET_MatchPadL2=%7
SET_MatchPadD2=%8
) ELSECALL :ExtDim%_MatchPad2% _MatchPadL2 _MatchPadD2
IFNOT [%6]==[] (
SET_MatchPadL1=%5
SET_MatchPadD1=%6
) ELSECALL :ExtDim%_MatchPad1% _MatchPadL1 _MatchPadD1
:: Add a decimal if one number has a decimal and the other doesn't.
IF%_MatchPadD1%GTR%_MatchPadL1% (
IF%_MatchPadD2%LEQ%_MatchPadL2% (
CALL :ExtMakeFloat _MatchPad1 _MatchPadL1
)
)
IF%_MatchPadD2%GTR%_MatchPadL2% (
IF%_MatchPadD1%LEQ%_MatchPadL1% (
CALL :ExtMakeFloat _MatchPad2 _MatchPadL2
)
)
:: Pad number strings so they both line up.
IF%_MatchPadD1%GTR%_MatchPadD2% (
SET /A "_DPadLen=_MatchPadD1-_MatchPadD2"
CALL :ExtPad _MatchPad2 !_DPadLen! L _MatchPadL2 _MatchPadD2
SET_MatchPadD=_MatchPadD1
)
IF%_MatchPadD2%GTR%_MatchPadD1% (
SET /A "_DPadLen=_MatchPadD2-_MatchPadD1"
CALL :ExtPad _MatchPad1 !_DPadLen! L _MatchPadL1 _MatchPadD1
SET_MatchPadD=_MatchPadD2
)
IF%_MatchPadL1%GTR%_MatchPadL2% (
SET /A _LPadLen=_MatchPadL1-_MatchPadL2
CALL :ExtPad _MatchPad2 !_LPadLen! R _MatchPadL2 _MatchPadD2
SET_MatchPadL=_MatchPadL1
)
IF%_MatchPadL2%GTR%_MatchPadL1% (
SET /A _LPadLen=_MatchPadL2-_MatchPadL1
CALL :ExtPad _MatchPad1 !_LPadLen! R _MatchPadL1 _MatchPadD1
SET_MatchPadL=_MatchPadL2
)
IFNOTDEFINED _MatchPadL SET_MatchPadL=%_MatchPadL1%
IFNOTDEFINED _MatchPadD SET_MatchPadD=%_MatchPadD1%
ENDLOCAL&SET%1=%_MatchPad1%& SET %2=%_MatchPad2%&SET%3=%_MatchPadL%& SET %4=%_MatchPadD%
GOTO :EOF
:ExtMakeFloat
SETLOCALEnableDelayedExpansion
:: Used to add a decimal point to a number string
:: Accepts the following parameters:
:: %1 - The variable that needs a decimal point
::
:: Example invocation:
:: CALL :ExtMakeFloat _Num _Len
:: |--> Returns _Num with a . at the end (1234 -> 1234.)
:: |--> Automatically increments length by one.
SET_FloatNum=!%1!
SET /A _FloatLength=%2
:: Only do this if we really need to...
CALL :ExtDim%_FloatNum% _FloatLen _FloatDec
IF%_FloatDec%GTR%_FloatLen% (
SET_FloatNum=!_FloatNum!.
SET /A _FloatLength+=1
)
ENDLOCAL&SET%1=%_FloatNum%& SET /A %2=%_FloatLength%
GOTO :EOF
:ExtPad
SETLOCALEnableDelayedExpansion
:: Used to pad a number to facilitate mathematical operations
:: Accepts the following parameters:
:: %1 - The variable that needs to be padded
:: %2 - The number of positions to pad
:: %3 - The direction the pad needs to be applied (L or R)
:: %4 - Length variable - Returned with padded length
:: %5 - Decimal variable - Returned with adjusted position
::
:: Example invocation:
:: CALL :ExtPad _Num 5 L _Len _Dec
:: |--> Returns padded length and decimal position in _Len & _Dec.
:: |--> Returns padded number back in _Num
SET_PadNum=!%1!
SET /A _PadLength=%4
SET /A _PadDecimal=%5
IF%3==L (
FOR /L %%GIN (1,1,%2) DO (
SET_PadNum=0!_PadNum!
SET /A _PadLength+=1
SET /A _PadDecimal+=1
)
)
IF%3==R (
FOR /L %%GIN (1,1,%2) DO (
SET_PadNum=!_PadNum!0
SET /A _PadLength+=1
)
)
ENDLOCAL&SET%1=%_PadNum%& SET /A %4=%_PadLength%&SET /A %5=%_PadDecimal%
GOTO :EOF
:ExtDim
SETLOCALEnableDelayedExpansion
:: Used to get the string length and decimal position of a number.
:: Accepts three parameters:
:: %1 - The number that needs its size and precision
:: %2 - A variable in which the size is returned
:: %3 - A variable in which the position of the decimal is returned
:: Returns length and decimal position by reference.
::
:: Example invocation:
:: CALL :ExtDim %_Num% _Len _Dec
SET_DimLength=0
SET_DimDecimal=-1
SET_DimArg=%1
SET_DimPos=1
:DimLoop
CALLSET_DimLCheck=%%_DimArg:~%_DimLength%,%_DimPos%%%
IFNOTDEFINED _DimLCheck GOTO ExitDimLoop
SET /A _DimLength+=1
IF%_DimLCheck%==. SET_DimDecimal=%_DimLength%
GOTO DimLoop
:ExitDimLoop
IF%_DimDecimal%EQU-1SET /A "_DimDecimal=_DimLength+1"
ENDLOCAL&SET /A %2=%_DimLength% &SET /A %3=%_DimDecimal%
GOTO :EOF