Batch File Unknown Loop Error

December 28, 2009 at 17:02:25
Specs: Windows Vista
Hello,

I've created a batch file to help quickly calculate the Fibonacci Sequence.

For those who don't know, the Fibonacci sequence is a sequence where the next number is the previous two numbers added together.
it looks like this:
1 1 2 3 5 8 13 21
1+1=2
1+2=3
2+3=5
3+5=8
5+8=13
8+13=21

so below is the batch file. It works well until the 47th line. can anyone indicate the problem??


@echo off
cls
title The Fibonacci Sequence Calculator
color 04

set line=1
set fib1=1
set fib2=1
set fib3=0

echo   %line%  -  %fib1%


:circle1
set /a line=%line%+1
if '%line%'=='10' goto circle2

set fib3=%fib2%
set fib2=%fib1%
set /a fib1=%fib2%+%fib3%

echo   %line%  -  %fib1%
goto circle1


:circle2
set /a line=%line%+1
if '%line%'=='101' goto end

set fib3=%fib2%
set fib2=%fib1%
set /a fib1=%fib2%+%fib3%

echo  %line%  -  %fib1%
goto circle2


:end
pause >nul


See More: Batch File Unknown Loop Error

Report •


#1
December 28, 2009 at 19:30:44
looks like integer overflow to me as the no.s went "negative"

Report •

#2
December 28, 2009 at 20:47:55
Yep 2 ^ 31 - 1 is the max for a signed 32bit int, which set /a uses.

I stitched it one of my old subroutines to work with large numbers, it will be quite a bit slower.

http://pastebin.com/m284f2cc3

I didn't check any of the numbers though.


Batch Variable how to


Report •

#3
December 28, 2009 at 21:48:27
my test maxed out at 2147483647 before neg.
(ie 2^31-1)
i'll be interested to check out your workaround! logical shift? too tired rt now.

Report •

Related Solutions

#4
December 28, 2009 at 22:46:54
c++, anyone?


=====================================
Helping others achieve escape felicity

M2


Report •

#5
December 29, 2009 at 04:11:54
What data type for c++?

An "unsigned long long" bottoms out after 92, my compiler won't take a "long long long".

#include <iostream>

int main()
{
    unsigned long long int part1 = 0;
    unsigned long long int part2 = 1;
    unsigned long long int part3 = 0;
    for (int i=1;i<101;i++) {
        part3 = part2;
        part2 = part1 + part2;
        part1 = part3;
        std::cout << i << " " << part2 << '\n';
    }
    system("PAUSE");
    return EXIT_SUCCESS;
}

[edit]
@nbrane

i'll be interested to check out your workaround! logical shift? too tired rt now.

Sorry to disappoint it just adds the last digits and collects the values in a variable, striping the last char every time. It works but is much slower than other approaches.
[/edit]



Batch Variable how to


Report •

#6
December 29, 2009 at 04:55:59
Hi Judago,

I dunno c++ but can't you define your own type?


=====================================
Helping others achieve escape felicity

M2


Report •

#7
December 29, 2009 at 05:25:49
Hi M2,

I must have misinterpreted what you were saying, I thought you were suggesting c++ as an alternative language..... Perhaps a grade? ;)

I'm fairly basic on c++ myself, but I think it would be an absolute nightmare to setup, allocate memory, hammer out the logic and integrate(i.e. set up/overload the operators).

@jcgun

Your script also skips the line number "10" but keeps the sequence intact.


Batch Variable how to


Report •

#8
December 29, 2009 at 06:05:20
C++ can be used, but don't bother defining your own type when there are so many Open Source arbitrary-precision arithmetic libraries you can download. Similarly, you can use Java, with the BigInteger type from the standard Java API, or indeed C# with a similar type from the .NET library.

Report •

#9
December 30, 2009 at 02:31:53
Hi Judago,

Yes, I meant language, not the report card.

Hi klint,

I figured somebody had already invented that whell ic++.

LOL with .NET. I got a SIM card reader which required the .NET framework: 50MB [this is not a typo]


=====================================
Helping others achieve escape felicity

M2


Report •


Ask Question