' c_math.pb — C math library functions simulation
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL doubleVals(20) AS DOUBLE
    LOCAL numVals AS LONG
    LOCAL i AS LONG
    LOCAL j AS LONG
    LOCAL sinVal AS DOUBLE
    LOCAL cosVal AS DOUBLE
    LOCAL sqrtVal AS DOUBLE
    LOCAL logVal AS DOUBLE
    LOCAL powVal AS DOUBLE
    LOCAL absVal AS DOUBLE
    LOCAL x AS DOUBLE
    LOCAL y AS DOUBLE
    LOCAL z AS DOUBLE
    LOCAL sum AS DOUBLE
    LOCAL mean AS DOUBLE
    LOCAL variance AS DOUBLE
    LOCAL stddev AS DOUBLE
    LOCAL diff AS DOUBLE
    LOCAL iter AS LONG

    numVals = 8

    PRINT "=== C Math Library Simulation ==="

    PRINT "Square root approximation:"
    FOR i = 1 TO 10
        x = i * 1.0
        sqrtVal = x / 2.0
        sqrtVal = sqrtVal + x / sqrtVal
        sqrtVal = sqrtVal / 2.0
        sqrtVal = sqrtVal + x / sqrtVal
        sqrtVal = sqrtVal / 2.0
        PRINT "  sqrt(" + STR$(x) + ") ~ " + STR$(sqrtVal)
    NEXT i

    PRINT "Power approximation (x^2):"
    FOR i = 1 TO 8
        x = i * 1.0
        powVal = x * x
        PRINT "  " + STR$(x) + "^2 = " + STR$(powVal)
    NEXT i

    PRINT "Absolute value:"
    x = 0 - 3.5
    IF x < 0 THEN
        absVal = 0 - x
    ELSE
        absVal = x
    END IF
    PRINT "  abs(-3.5) = " + STR$(absVal)

    x = 4.2
    IF x < 0 THEN
        absVal = 0 - x
    ELSE
        absVal = x
    END IF
    PRINT "  abs(4.2) = " + STR$(absVal)

    PRINT "Sine approximation (degrees):"
    FOR i = 0 TO 8
        x = i * 45
        IF x = 0 THEN PRINT "  sin(0) = 0"
        IF x = 45 THEN PRINT "  sin(45) = 0.707"
        IF x = 90 THEN PRINT "  sin(90) = 1"
        IF x = 135 THEN PRINT "  sin(135) = 0.707"
        IF x = 180 THEN PRINT "  sin(180) = 0"
        IF x = 225 THEN PRINT "  sin(225) = -0.707"
        IF x = 270 THEN PRINT "  sin(270) = -1"
        IF x = 315 THEN PRINT "  sin(315) = -0.707"
        IF x = 360 THEN PRINT "  sin(360) = 0"
    NEXT i

    PRINT "Mean and StdDev:"
    FOR i = 0 TO numVals - 1
        doubleVals(i) = (i + 1) * 1.5
        sum = sum + doubleVals(i)
    NEXT i
    mean = sum / numVals
    variance = 0
    FOR i = 0 TO numVals - 1
        diff = doubleVals(i) - mean
        IF diff < 0 THEN diff = 0 - diff
        variance = variance + diff * diff
    NEXT i
    variance = variance / numVals
    stddev = variance / 2.0
    stddev = stddev + variance / stddev
    stddev = stddev / 2.0

    PRINT "  Mean: " + STR$(mean)
    PRINT "  Variance: " + STR$(variance)
    PRINT "  StdDev: " + STR$(stddev)

    PRINT "=== End cmath ==="
    FUNCTION = 0
END FUNCTION
