commit df4d15d5c3cbdb898c2c2829b5b76328aee5ae60 Author: Wayne.MJ <150761502+wayne-mj@users.noreply.github.com> Date: Mon Sep 8 13:41:14 2025 +1000 First commit diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..da80fa6 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) [2025] [Wayne Jackson] + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..a567afe --- /dev/null +++ b/README.md @@ -0,0 +1,13 @@ +# sqrtnewton + +This is a COBOL rewrite of a Fortran program that is a rewrite of a REXX script that I came across similar to the one on Wikipedia [Rexx](https://en.wikipedia.org/wiki/Rexx) to find the square root of a positive number. + +I have modified the code to include: +- Correct for negative numbers +- Set a limit for iterations +- Set tolerances for 32 bit REAL numbers +- It also sanitises the input in case the user types something other than a number, also JES/JCL seems to not like numbers all that much being passed by SYSIN + +This all came about as I am doing some courses on using the z/OS and writing code in REXX is one of the course modules. + +I got bored. \ No newline at end of file diff --git a/sqrnewt.cbl b/sqrnewt.cbl new file mode 100644 index 0000000..a02704d --- /dev/null +++ b/sqrnewt.cbl @@ -0,0 +1,68 @@ + IDENTIFICATION DIVISION. + * A rewrite of a fortran program that is rewrite of a REXX script + * to find the square root of a number + + PROGRAM-ID. SQRNEWT. + AUTHOR. Wayne. + + DATA DIVISION. + WORKING-STORAGE SECTION. + 01 WORK-AREA. + 05 MAX-ITER PIC 9(3) VALUE 100. + 05 ITER PIC 9(3). + 05 TOL PIC 9(3)V9(12) VALUE 0.0000000001. + 05 RR PIC S9(3)V9(12). + 05 R PIC S9(3)V9(12). + 05 N PIC S9(3)V9(12). + 05 N-TEXT PIC X(15). + 05 DIS-RESULTS PIC Z(3).9(12). + + PROCEDURE DIVISION. + DISPLAY "Enter a 3 digit value <> 0:" + * ACCEPT N FROM PARMS + * MOVE 4 TO N + ACCEPT N-TEXT + + PERFORM VALID-INPUT + STOP RUN. + + * Validate the input + * If less than 0 ie negative, make it positive + * If 0 then drop out + VALID-INPUT. + INSPECT N-TEXT REPLACING ALL "," BY "." + COMPUTE N = FUNCTION NUMVAL(N-TEXT) + * DISPLAY N + + IF N LESS THAN ZERO THEN + COMPUTE N = N * (- 1) + DISPLAY "CORRECTED FOR NEGATIVE VALUE" + END-IF. + + IF N NOT EQUAL ZERO THEN + PERFORM FIND-SQRROOT + ELSE + DISPLAY "CANNOT BE ZERO" + END-IF. + END-VALID-INPUT. + + * Find the SQR Root of a number + FIND-SQRROOT. + MOVE 0 to ITER + MOVE 1 to R + + PERFORM UNTIL ITER GREATER OR EQUAL MAX-ITER + COMPUTE ITER = ITER + 1 + COMPUTE RR = (N / R + R) / 2 + + IF (FUNCTION ABS(RR - R) <= TOL) THEN + EXIT PERFORM + END-IF + + MOVE RR TO R + END-PERFORM + + MOVE RR TO DIS-RESULTS + DISPLAY DIS-RESULTS. + END-FIND-SQRROOT. + diff --git a/sqrnewt.jcl b/sqrnewt.jcl new file mode 100644 index 0000000..57b3bf4 --- /dev/null +++ b/sqrnewt.jcl @@ -0,0 +1,17 @@ +//SQRNEWT JOB 1,NOTIFY=&SYSUID +//***************************************************/ +//COBRUN EXEC IGYWCL +//COBOL.SYSIN DD DSN=&SYSUID..CBL(SQRNEWT),DISP=SHR +//LKED.SYSLMOD DD DSN=&SYSUID..LOAD(SQRNEWT),DISP=SHR +//***************************************************/ +// IF (RC = 0) THEN +//***************************************************/ +//RUN EXEC PGM=SQRNEWT +//STEPLIB DD DSN=&SYSUID..LOAD,DISP=SHR +//SYSOUT DD SYSOUT=* +//SYSIN DD * +15 +/* +//SYSUDUMP DD DUMMY +//***************************************************/ +// ENDIF