www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Nested for loop issue

reply "Sumit Adhikari" <sumit.adhikari gmail.com> writes:
Dear All,

Greetings!

I has been an hour I starting making my hands dirty with D. I 
wrote following program:

import std.string;
import std.stdio;

ushort adder(ushort a, ushort b) {
    return (cast(ushort)(a + b)) ;
}

unittest {

    bool testStimEq(ushort a, ushort b, ushort result){

       try {
          writefln(" STIM :: Testing for  %d  and  %d", a,b);
          assert(adder(a,b) == result);
          writefln(" STAT :: Test for  %d  and  %d passed", a,b);
          return (false) ;
       } catch (core.exception.AssertError) {
          writefln(" STAT :: Test for  %d  and  %d failed", a,b);
          return(true) ;
       }

    }

    bool testStimnEq(ushort a, ushort b, ushort result){

       try {
          writefln(" STIM :: Testing for  %d  and  %d", a,b);
          assert(adder(a,b) != result);
          writefln(" STAT :: Test for  %d  and  %d passed", a,b);
          return (false) ;
       } catch (core.exception.AssertError) {
          writefln(" STAT :: Test for  %d  and  %d failed", a,b);
          return(true) ;
       }

    }

    writeln(" MESG :: Starting unit testing");

    int status = 0 ;

    //status += testStimEq(1,1,2);
    //status += testStimnEq(1,10,3);

    for (ushort i = 0 ; i < 65536 ; ++i){ // Holy King-Kong!

       for (ushort j = 0 ; j < 65536 ; ++j){

          status += testStimEq(i,j,cast(ushort)(i+j));

       }
    }

    if(status == 0) writeln(" MESG :: All unit tests passed");
    else            writeln(" ERRN :: Unit test failed");

}

void main()
    {}

I found that inner for loop is behaving correctly, but i is not 
changing.
What is that I am doing ? OR is it gcd ?

Regards, Sumit
Oct 27 2013
parent reply "safety0ff" <safety0ff.dev gmail.com> writes:
On Sunday, 27 October 2013 at 09:05:06 UTC, Sumit Adhikari wrote:
    for (ushort i = 0 ; i < 65536 ; ++i){ // Holy King-Kong!

       for (ushort j = 0 ; j < 65536 ; ++j){

          status += testStimEq(i,j,cast(ushort)(i+j));

       }
    }
 What is that I am doing ?
You have an infinite loop nested within another infinite loop, ushort cannot represent a value which is >= 65536.
Oct 27 2013
parent reply "Sumit Adhikari" <sumit.adhikari gmail.com> writes:
On Sunday, 27 October 2013 at 09:33:32 UTC, safety0ff wrote:
 On Sunday, 27 October 2013 at 09:05:06 UTC, Sumit Adhikari 
 wrote:
   for (ushort i = 0 ; i < 65536 ; ++i){ // Holy King-Kong!

      for (ushort j = 0 ; j < 65536 ; ++j){

         status += testStimEq(i,j,cast(ushort)(i+j));

      }
   }
 What is that I am doing ?
You have an infinite loop nested within another infinite loop, ushort cannot represent a value which is >= 65536.
Oops, missed that i & j is 16 bit!. Thanks for pointing that out Regards, Sumit
Oct 27 2013
parent Marco Leise <Marco.Leise gmx.de> writes:
Am Sun, 27 Oct 2013 10:40:01 +0100
schrieb "Sumit Adhikari" <sumit.adhikari gmail.com>:

 On Sunday, 27 October 2013 at 09:33:32 UTC, safety0ff wrote:
 On Sunday, 27 October 2013 at 09:05:06 UTC, Sumit Adhikari 
 wrote:
   for (ushort i = 0 ; i < 65536 ; ++i){ // Holy King-Kong!

      for (ushort j = 0 ; j < 65536 ; ++j){

         status += testStimEq(i,j,cast(ushort)(i+j));

      }
   }
 What is that I am doing ?
You have an infinite loop nested within another infinite loop, ushort cannot represent a value which is >= 65536.
Oops, missed that i & j is 16 bit!. Thanks for pointing that out Regards, Sumit
The idea of using correctly sized types everywhere is honorable, but I found that for loops over N iterations better use 32-bit integers or machine word size. One issue is that you cannot iterate over all possible ushorts with a ushort, another is that the loop variables fit better into CPU registers as int/uint or size_t. So I came to think of loop variables as indexes into the list of ushorts and indexes are size_t. :) -- Marco
Oct 27 2013