Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

RangeError: Maximum call stack size exceeded (native stack depth) when trying to find ratio using Math.max and large array

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading