grame-cncm / faust

Functional programming language for signal processing and sound synthesis
http://faust.grame.fr
Other
2.57k stars 322 forks source link

ERROR : stack overflow in eval #394

Open magnetophon opened 4 years ago

magnetophon commented 4 years ago

I get the above error when I compile the following code:

fixedDelayMedianOfMedians =
  case {
    (1,x) => x;
    (2,x) => (x+x')/2;  // hey, it's better than picking one!
    (n,x) =>
(fixedDelayMedianOfMedians(n/3,x), fixedDelayMedianOfMedians(n/3,x)@(n/3), fixedDelayMedianOfMedians(n/3,x)@(n/3*2))
    : MedianOfMedians3 with {
      MedianOfMedians3(x0,x1,x2) =
        select3(sell,x0,x1,x2) with {
      sell =
        (  ( ((x1>=x0) & (x1<=x2)) | ((x1>=x2) & (x1<=x0))))
+       (2*( ((x2> x1) & (x2< x0)) | ((x2> x0) & (x2< x1)))) ;
        };
    };
  };

  process =
  fixedDelayMedianOfMedians(pow(3,3));

When I use

process =
  fixedDelayMedianOfMedians(27);

all is good.

I wanna turn this into a variable window construct, like slidingReduce, but for that, I need to specify n as a power .

PS: Can anyone tell me if I'm on the right track with this, or will Julius do it in a one-liner that doesn't take any CPU? :)

The web tells me I should be using groups of 5 or 7, for performance reasons. I don't understand any of the explanations of the algorithms they are using, but as far as I can tell in this case it doesn't matter for the performance because I'm able to re-use older values here and because I'm not calculating the exact Median but just an approximation.

Can someone enlighten me?

magnetophon commented 4 years ago

I found out that fixedDelayMedianOfMedians(pow(3,3):int); works. Not sure why though.

sletz commented 4 years ago

Probably because the (n,x) => pattern matching clause is always taken, and never the first two.

magnetophon commented 4 years ago

May I ask why you closed the issue?

It's not high priority or anything, but I'd consider this a bug: faust should behave the same when I write pow(3,3) and when I write 27, wouldn't you agree?.

sletz commented 4 years ago

pow(3,3) produces a "float" type, and 27 is seen as an integer. A probably better and safer way is to have fixedDelayMedianOfMedians cast its argument to "int" internally, so that pattern-matching would work as expected ?

magnetophon commented 4 years ago

Sure, I can do that. I was just wondering if it would be possible to do in the compiler.

On the lower levels it might very well be true that the power of two ints gives a float, but I think Faust's main strength is that it abstracts away those levels, and just lets me focus on audio! :)

Again, not a priority at all, just something that would be nice to have some day.

sletz commented 4 years ago

Maybe one day. I reopen the issue to keep it hot !

magnetophon commented 4 years ago

Thanks!