Long Multiplication

  • This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.
#1
I am using the following batch file, written for CMD.EXE;
Code:
::Long Multiplication Task from Rosetta Code
::Batch File Implementation
@echo off
call :longmul 18446744073709551616 18446744073709551616 answer
echo(%answer%
exit /b 0

rem The Hellish Procedure
rem Syntax: call :longmul <n1> <n2> <variable to store product>
:longmul
    setlocal enabledelayedexpansion

    rem Define variables
    set "num1=%1"
    set "num2=%2"
    set "limit1=-1"
    set "limit2=-1"
    set "length=0"
    set "prod="

    rem Reverse the digits of each factor
    for %%A in (1,2) do (
        for /l %%B in (0,1,9) do set "num%%A=!num%%A:%%B=%%B !"
        for %%C in (!num%%A!) do ( set /a limit%%A+=1 & set "rev%%A=%%C!rev%%A!" )
    )

    rem Do the multiplication
    for /l %%A in (0,1,%limit1%) do (
        for /l %%B in (0,1,%limit2%) do (
            set /a iter=%%A+%%B
            set /a iternext=iter+1
            set /a iternext2=iter+2
            set /a prev=digit!iter!
            set /a digit!iter!=!rev1:~%%A,1!*!rev2:~%%B,1!

            rem The next line updates the length of "digits"
            if !iternext! gtr !length! set length=!iternext!
            if !iter! lss !length! set /a digit!iter!+=prev
            set /a currdigit=digit!iter!
            if !currDigit! gtr 9 (
                set /a prev=digit!iternext!
                set /a digit!iternext!=currdigit/10
                set /a digit!iter!=currdigit%%10

                rem The next line updates the length of "digits"
                if !iternext2! gtr !length! set length=!iternext2!
                if !iternext! lss !length! set /a digit!iternext!+=prev
            )
        )
    )

    rem Finalize product reversing the digits
    for /l %%F in (0,1,%length%) do set "prod=!digit%%F!!prod!"
    endlocal & set "%3=%prod%"
goto :eof
It works fine in 64-bit CMD.EXE;
Code:
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

c:\utils>longmult.cmd
340282366920938463463374607431768211456
but not in TCC;
Code:
TCC  21.01.53 x64   Windows 7 [Version 6.1.7601]
Copyright 2017 JP Software Inc.  All Rights Reserved
Your evaluation period expires in 7 days.
You can buy Take Command, TCC, and CMDebug at https://jpsoft.com

[C:\Users\jlc\utils]longmult.cmd
TCC: C:\Users\jlc\utils\longmult.cmd [6]  Unknown command "echo(0"
When I run it through CMDEBUG v21.01.53 x64;
Code:
TCC  21.01.53 x64   Windows 7 [Version 6.1.7601]
TCC Build 53   Windows 7 Build 7601  Service Pack 1
Registered to OPTIPLEX9010
TCC: C:\Users\jlc\utils\longmult.cmd [27]  Not in environment "rev18446744073709551616*"
TCC: C:\Users\jlc\utils\longmult.cmd [27]  Not in environment "rev18446744073709551616*"
I get the above two errors, but not when I run it through BDEBUGGER, but I still get the
Code:
TCC: C:\Users\jlc\utils\longmult.cmd [6]  Unknown command "echo(0"
with BDEBUGGER.

Joe
 
#2
I don't know about the "ECHO(". It's apparently a valid command in CMD. But one place (maybe the first subsequent) where TCC differs is here (in the routine to reverse the digits).
for /l %%B in (0,1,9) do set "num%%A=!num%%A:%%B=%%B !"
In CMD, that puts a space after each digit of NUM1 and NUM2. In TCC it doesn't.
 
Last edited: