adafruit / Adafruit_CircuitPython_Waveform

CircuitPython library to generate single wavelength waveforms.
MIT License
7 stars 13 forks source link

Fix unsigned short overflow #19

Closed sayler closed 4 years ago

sayler commented 4 years ago

Before:

>>> sine.sine_wave(100,1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/sayler/Projects/Adafruit_CircuitPython_Waveform/adafruit_waveform/sine.py", line 44, in sine_wave
    b[i] = int(math.sin(math.pi * 2 * i / length) * (2 ** 15) + 2 ** 15)
OverflowError: unsigned short is greater than maximum

After:

>>> max(sine.sine_wave(100000,1))
65535
>>> min(sine.sine_wave(100000,1))
1

Fixes #9

ladyada commented 4 years ago

thank you :)

@kevinjwalters wanna take a look?

sayler commented 4 years ago

I will say: there are fancier, and certainly faster, ways to do this if the range [0, 2**16-1] is desired.

kevinjwalters commented 4 years ago

Change looks fine. Reduce the range by 1 to avoid hitting 65536 which will blow up on the 16bit unsigned storage is all that's required.