It's high time for the second installment of stupid coding tricks! As we learned from the T-SQL Mandelbrot, a stupid coding trick isn't really about obfuscation per se... just, well, stupid awesomeness. Kinda like a quine, except even more useless.

Don Cross was gracious enough to provide today's example, which teeters on the edge of what many consider "code". It's a Win32 batch file. Unlike the analogous *nix shell script, batch "programming" comes with an incredibly limited syntax and a miniscule number of commands. While one can certainly do far more in batch than ever should be done, the technology works well foa shortcut for typing in a bunch of commands.

In fact, the programmability of batch is so limited that discovering the ability to perform "simple arithmetic on 32-bit signed integers" is worthy of a blog post. In fact, when I first read that post from Raymond, I remember thinking, hey sweet, more functionality in batch; so... now I can do... err....what?

Well, now I can finally answer that question. Submitted for your approval: pi.bat, a high-precision pi calculator that uses entirely batch commands.

@if defined talk (echo on) else (echo off)
setlocal EnableDelayedExpansion
echo.pi.bat  -  By Don Cross  -  http://cosinekitty.com
set /a NumQuads = 30
set /a MaxQuadIndex = NumQuads - 1

echo.
echo.%time% - started
echo.

call :PiEngine 48 18 32 57 -20 239
call :PiEngine 16 5 -4 239
goto :EOF

:PiEngine
    call :SetToInteger Pi 0
    set Formula=
    :PiTermLoop
        if "%1" == "" (
            call :Print pi
            echo.
            echo.!time! - finished !Formula!
            echo.
            goto :EOF
        )
        call :ArctanRecip PiTerm %2
        set /a PiEngineFactor=%1
        if !PiEngineFactor! lss 0 (
            set /a PiEngineFactor *= -1
            set Formula=!Formula!
            call :MultiplyByInteger PiTerm !PiEngineFactor!
            call :Subtract Pi PiTerm
            set Operator=-
        ) else (
            call :MultiplyByInteger PiTerm %1
            call :Add Pi PiTerm
            set Operator=+
        )
        if defined Formula (
            set Formula=!Formula! !Operator! !PiEngineFactor!*arctan^(1/%2^)
        ) else (
            set Formula=pi = %1*arctan^(1/%2^)
        )
        shift 
        shift
    goto PiTermLoop
    
:SetToInteger
    for /L %%i in (0, 1, !MaxQuadIndex!) do (
        set /a %1_%%i = 0
    )
    set /a %1_!MaxQuadIndex! = %2
    goto :EOF
    
:Print
    set PrintBuffer=x
    REM  Omit a couple of least significant quads, because they will have roundoff errors.
    if defined PiDebug (
        set /a PrintMinQuadIndex=0
    ) else (
        set /a PrintMinQuadIndex=2
    )
    set /a PrintMaxQuadIndex = MaxQuadIndex - 1
    for /L %%i in (!PrintMinQuadIndex!, 1, !PrintMaxQuadIndex!) do (
        set PrintDigit=!%1_%%i!
        if !PrintDigit! lss 1000 (
            if !PrintDigit! lss 100 (
                if !PrintDigit! lss 10 (
                    set PrintDigit=000!PrintDigit!
                ) else (
                    set PrintDigit=00!PrintDigit!
                )
            ) else (
                set PrintDigit=0!PrintDigit!
            )
        )
        set PrintBuffer=!PrintDigit!!PrintBuffer!
    )
    set PrintBuffer=!%1_%MaxQuadIndex%!.!PrintBuffer:x=!
    echo.%1 = !PrintBuffer!
    goto :EOF
    
:DivideByInteger
    if defined PiDebug echo.DivideByInteger %1 %2
    set /a DBI_Carry = 0
    for /L %%i in (!MaxQuadIndex!, -1, 0) do (
        set /a DBI_Digit = DBI_Carry*10000 + %1_%%i
        set /a DBI_Carry = DBI_Digit %% %2
        set /a %1_%%i = DBI_Digit / %2
    )
    goto :EOF
    
:MultiplyByInteger
    if defined PiDebug echo.MultiplyByInteger %1 %2
    set /a MBI_Carry = 0
    for /L %%i in (0, 1, !MaxQuadIndex!) do (
        set /a MBI_Digit = %1_%%i * %2 + MBI_Carry
        set /a %1_%%i = MBI_Digit %% 10000
        set /a MBI_Carry = MBI_Digit / 10000
    )
    goto :EOF    
    
:ArctanRecip
    if defined PiDebug echo.ArctanRecip %1 %2
    call :SetToInteger %1 1
    call :DivideByInteger %1 %2
    call :CopyValue AR_Recip %1
    set /a AR_Toggle = -1    
    set /a AR_K = 3
    :ArctanLoop
        if defined PiDebug (
            echo.
            echo.ArctanRecip  AR_K=!AR_K!    ---------------------------------------------------------
        )
        call :DivideByInteger AR_Recip %2
        call :DivideByInteger AR_Recip %2
        call :CopyValue AR_Term AR_Recip
        call :DivideByInteger AR_Term !AR_K!
        call :CopyValue AR_PrevSum %1
        if !AR_Toggle! lss 0 (
            call :Subtract %1 AR_Term
        ) else (
            call :Add %1 AR_Term
        )
        call :Compare AR_EqualFlag %1 AR_PrevSum
        if !AR_EqualFlag! == true goto :EOF
        set /a AR_K += 2
        set /a AR_Toggle *= -1
    goto ArctanLoop
    
:CopyValue
    if defined PiDebug echo.CopyValue %1 %2
    for /L %%i in (0, 1, !MaxQuadIndex!) do (
        set /a %1_%%i = %2_%%i
    )
    goto :EOF
    
:Add 
    if defined PiDebug echo.Add %1 %2
    if defined PiDebug call :Print %1
    if defined PiDebug call :Print %2
    set /a Add_Carry = 0
    for /L %%i in (0, 1, !MaxQuadIndex!) do (
        set /a Add_Digit = Add_Carry + %1_%%i + %2_%%i
        set /a %1_%%i = Add_Digit %% 10000
        set /a Add_Carry = Add_Digit / 10000
    )
    goto :EOF    

:Subtract 
    if defined PiDebug echo.Subtract %1 %2
    if defined PiDebug call :Print %1
    if defined PiDebug call :Print %2
    set /a Subtract_Borrow = 0
    for /L %%i in (0, 1, !MaxQuadIndex!) do (
        set /a Subtract_Digit = %1_%%i - %2_%%i - Subtract_Borrow
        if !Subtract_Digit! lss 0 (
            set /a Subtract_Digit += 10000
            set /a Subtract_Borrow = 1
        ) else (
            set /a Subtract_Borrow = 0
        )
        set /a %1_%%i = Subtract_Digit
    )
    goto :EOF    
    
:Compare
    if defined PiDebug echo.Compare %1 %2 %3
    if defined PiDebug call :Print %2
    if defined PiDebug call :Print %3
    set /a Compare_Index = 0
    set %1=true
    :CompareLoop
        if not !%2_%Compare_Index%! == !%3_%Compare_Index%! (
            if defined PiDebug echo.!%2_%Compare_Index%! neq !%3_%Compare_Index%!
            set %1=false
            goto :EOF
        )
        set /a Compare_Index += 1
        if !Compare_Index! gtr !MaxQuadIndex! (
            if defined PiDebug echo.Compare equal
            goto :EOF
        )
    goto CompareLoop    
    
REM    $Log: pi.bat,v $
REM    Revision 1.2  2007/09/06 21:49:15  Don.Cross
REM    Added time stamps and display of formula.
REM
REM    Revision 1.1  2007/09/06 21:12:36  Don.Cross
REM    Batch file for calculating pi
REM

As for the output, here's what came up when I ran it on my machine.

> pi

pi.bat  -  By Don Cross  -  http://cosinekitty.com

 1:42:47.79 - started

pi = 3.1415926535897932384626433832795028841971693993751058209749445923078164062
86208998628034825342117067982148086

 1:43:07.36 - finished pi = 48*arctan(1/18) + 32*arctan(1/57) - 20*arctan(1/239)


pi = 3.1415926535897932384626433832795028841971693993751058209749445923078164062
86208998628034825342117067982148086

 1:43:27.26 - finished pi = 16*arctan(1/5) - 4*arctan(1/239)

Got a stupid coding trick of your own? Well then send it on in!

[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!