' Conditional math: compile-time algorithm selection via #IF / #ELSEIF
#COMPILE EXE
#DIM ALL
#INCLUDE "win32api.inc"

%USE_FAST_MATH = 1
%USE_APPROX = 0

FUNCTION PBMAIN() AS LONG
    LOCAL i  AS LONG
    LOCAL n  AS LONG
    LOCAL result  AS LONG
    LOCAL values(20) AS LONG
    LOCAL sum  AS LONG
    LOCAL minVal  AS LONG
    LOCAL maxVal  AS LONG
    LOCAL avg AS DOUBLE

    PRINT "=== Conditional Math ==="
    PRINT

    n = 8
    values(0) = 45
    values(1) = 23
    values(2) = 78
    values(3) = 12
    values(4) = 56
    values(5) = 34
    values(6) = 90
    values(7) = 67

    PRINT "Input values:"
    FOR i = 0 TO n - 1
        PRINT "  " & FORMAT$(values(i))
    NEXT i
    PRINT

    sum = 0
    minVal = values(0)
    maxVal = values(0)

    FOR i = 0 TO n - 1
        sum = sum + values(i)
        IF values(i) < minVal THEN minVal = values(i)
        IF values(i) > maxVal THEN maxVal = values(i)
    NEXT i

    avg = sum / n

    PRINT "Results (compiled with %USE_FAST_MATH=" & FORMAT$(%USE_FAST_MATH) & "):"
    PRINT "  Sum: " & FORMAT$(sum)
    PRINT "  Min: " & FORMAT$(minVal)
    PRINT "  Max: " & FORMAT$(maxVal)
    PRINT "  Avg: " & FORMAT$(avg)
    PRINT

    PRINT "--- Algorithm Selection ---"
    PRINT "Selected: Linear scan (O(n))"
    PRINT "  Alternative: Divide-and-conquer (not used)"
    PRINT "  Alternative: SSE vectorized (requires CPU support)"
    PRINT

    PRINT "--- Compile-time branch ---"
    PRINT "Using FAST_MATH path (no division, no sqrt)"
    PRINT "Computed sum: " & FORMAT$(sum)
    PRINT

    PRINT "--- Available algorithms ---"
    PRINT "  1. Linear scan (current)"
    PRINT "  2. Divide & conquer"
    PRINT "  3. Parallel reduce"
    PRINT "Select by changing %USE_FAST_MATH in source"

    PRINT
    PRINT "Conditional math complete."

    FUNCTION = 0
END FUNCTION
