Skip to content

Fix unsigned short overflow #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 20, 2020
Merged

Fix unsigned short overflow #19

merged 1 commit into from
Oct 20, 2020

Conversation

sayler
Copy link
Contributor

@sayler sayler commented Oct 17, 2020

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
Copy link
Member

ladyada commented Oct 17, 2020

thank you :)

@kevinjwalters wanna take a look?

@sayler
Copy link
Contributor Author

sayler commented Oct 17, 2020

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

@kevinjwalters
Copy link

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.

@ladyada ladyada merged commit cd0c960 into adafruit:master Oct 20, 2020
adafruit-adabot added a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request Oct 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

sine_wave likely to generate value too large for "H" array.array
3 participants