How do I re-write this piece of BASIC programming to PHP?

129 Views Asked by At

Pardon my lack of expertise but I really do need to pick your brain on the proper formulation of this PHP code.

I have these two lines of BASIC:

100 PITCHHOR=(LSLATHOR/(NRSLATVER+1)):LSLATHOR=PITCHHOR*(NRSLATVER+1)
110 IF PITCHHOR>72 THEN NRSLATVER=NRSLATVER+1:GOTO 100

I simply need to put it to PHP code, I understand it is out of context but I simply need the logic. What is happening it makes an adjustment to PITCHHOR on line 100, and an adjustment to LSLATHOR. Then if PITCHHOR > 72 it adjusts NRSLATVER and goes back to line 100, and it seems to loop until a condition is met where PITCHHOR is less than a value of 72. Am I right in this?

What would PHP look like for these two lines, would I use a while loop?

My attempt (Probably making mistakes)

<?php
    $pitchhor = (floatval($lslathor) / (floatval($nrslatver) + 1));
    $lslathor = (floatval($pitchhor) * (floatval($nrslatver) + 1));
        while ($pitchhor > 72) {                    
            $nrslatver += 1;
            $pitchhor = (floatval($lslathor) / (floatval($nrslatver) + 1));
            $lslathor = (floatval($pitchhor) * (floatval($nrslatver) + 1));               
        }
?>

I'd appreciate your help ty vm.

EDIT 1:

I made a slight change, trying to use goto since it's been introduced in PHP 5.3.0.

Does this logic reflect the BASIC code?

recalc:

$pitchhor = (floatval($lslathor) / (floatval($nrslatver) + 1));
$lslathor = (floatval($pitchhor) * (floatval($nrslatver) + 1));

if ($pitchhor > 72) {
    $nrslatver += 1;
    goto recalc;
}

EDIT 2:

I ended up using this, and it worked thanks to Richard Chambers (I added a dynamic tolerance instead of a fixed pitch tolerance of 72, and this pertains to the specifications laid out in the old BASIC program):

do {
        $pitch = ($length / ($slat_qty + 1));
        $length = $pitch * ($slat_qty + 1); 
        if($pitch > $tolerance) { 
            ++$slat_qty;
        }
} while($pitch > $tolerance); 
1

There are 1 best solutions below

0
Richard Chambers On BEST ANSWER

The BASIC source can be broken out as follows. This assumes that the multiple statements on the same line of the THEN clause are part of a block which seems to be standard for old style BASIC.

100 PITCHHOR=(LSLATHOR/(NRSLATVER+1))
102 LSLATHOR=PITCHHOR*(NRSLATVER+1)
110 IF PITCHHOR>72 THEN
112 NRSLATVER=NRSLATVER+1
114 GOTO 100
116 END

I think that the following is close to the BASIC source. Have it in a loop that continues so long as the if statement condition is met. Nothing really wrong with goto statement just can be a source of errors. And frankly the following could also be an error if $pitchhor does not converge to a value less than 72 therefor the thread ends up in an infinite loop.

<?php
    do {
        $pitchhor = (floatval($lslathor) / (floatval($nrslatver) + 1));
        $lslathor = (floatval($pitchhor) * (floatval($nrslatver) + 1));
        if($pitchhor > 72) {                    
            $nrslatver += 1;
        }
    } while ($pitchhor > 72);
?>