Home > Software design >  RangeError: Maximum call stack size exceeded (native stack depth) when trying to find ratio using Ma
RangeError: Maximum call stack size exceeded (native stack depth) when trying to find ratio using Ma

Time:10-10

I have a large numerical array (results of an audio file's signal which I return to my react native app from a python backend.

And the array looks like this

[1098, 522, 597, 325, 201, 16, ...................... 75, 447, 253, 58, 273]

size of the array is around 535754

I have this code which I use to find raio

peaks = audio_signal_array
const ratio = Math.max(...peaks) / 100
// ratio = 1.1 (example result)
const normalized = peaks.map(point => Math.round(point / ratio))
// normalized = [0, 0, 2, 5, 18, 50, 100, 18] (example result)

Everytime I run this code it throws me the error

error [RangeError: Maximum call stack size exceeded (native stack depth)]

So it would be great if anyone can help me to either fix this code from the javascript side or help me convert this code to python so I can return the ratio and the normalized array to the react native app

CodePudding user response:

Try

peaks.reduce((a, b) => Math.max(a, b))

instead of

Math.max(...peaks)

On the python side, you can simply do

ratio = max(peaks) / 100

See also https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/max:

...spread (...) and apply will either fail or return the wrong result if the array has too many elements, because they try to pass the array elements as function parameters.

  • Related