Magic infinite loop inside foreach

MrSmith mrsmith33 at yandex.ru
Fri Jan 31 06:22:21 PST 2014


On Friday, 31 January 2014 at 14:05:10 UTC, MrSmith wrote:
> On Thursday, 30 January 2014 at 23:45:14 UTC, Joseph Rushton 
> Wakeling wrote:
>> On 31/01/14 00:08, MrSmith wrote:
>>> Somehow if i comment out
>>> //matrix = solveTemp(temp);
>>> it works, but this method works fine, and after it done1 is 
>>> printed. Strange.
>>
>> That does rather suggest that it's that method that is causing 
>> things to get stuck.  Can you share what's inside it?  And 
>> when you say "this method works fine", do you mean that you've 
>> manually tested it with the temp variable that goes in before 
>> it hangs?
>>
>> Could it be that whatever printout you're doing of variables 
>> like matrix is missing some tiny differences -- floating-point 
>> rounding errors? -- that are responsible for the 
>> transformations inside makeTemp or solveTemp behaving wrongly 
>> and therefore (in the latter case) getting stuck?
>
> Here is complete code (excluding rational.d by DSimcha) 
> https://gist.github.com/MrSmith33/8732520
>
> It prints:
>
> foreach start
> after makeTemp
> solveTemp start
> before solveTemp return
> foreach end
>
> and then stucks.
> As you can see it goes through solveTemp method and stops at 
> the foreach end.
> So, i can not see any reason why it goes infinite there.


Found it!
First i was thinkig that it was stuck at the end of foreach. Than 
i placed print at the start of the loop. Turned out that problem 
was in makeTemp in pow function which i poorly implemeneted and 
it looped infinitely.

ElementType pow(ElementType, I)(ElementType number, I power) if 
(isIntegral!I)
{
	ElementType result = number;
	writeln("pow start ", number, " ", power);
	stdout.flush();
	foreach(_; 0..power-1) number *= number;
	writeln("pow end");
	stdout.flush();
	return number;
}

that printed:

pow start 0/1 0

I've completely forgot about power of 0.
Thanks guys!


More information about the Digitalmars-d-learn mailing list