#๐Ÿ”’ Overflowing error, negative numbers wrap around.

39 messages ยท Page 1 of 1 (latest)

nova dust
#

I've got this weird issue where negative numbers overflow to whatever 255 minus that number is. I know about 2 complements, etc. and unsigned integers but I don't know how to accommodate for them. How do I stop this error from occuring?

steel masonBOT
#

@nova dust

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

shy scaffold
#

!code

steel masonBOT
#
Formatting code on Discord

Here's how to format Python code on Discord:

```py
print('Hello world!')
```

These are backticks, not quotes. Check this out if you can't find the backtick key.

For long code samples, you can use our pastebin.

shy scaffold
#

Whatever you're working with, you probably need to change the data type from an 8 bit integer to something larger.

nova dust
# shy scaffold hello, please remember to always share code and text as actual text. Not as a sc...
if layer_frame_count % 4 == 0 and layer_frame_count > 20:
                first_five_entries = list(samplewise_max.values())[:5]
                last_four = [entry[-4:] for entry in first_five_entries]
                result = [sublist[3] - sublist[0] for sublist in last_four]
                result2=[x/4 for x in result]
                der_test= sum(result2)/len(result2)
                if der_test > 10:
                    print('increasing')
                    plt.imshow(frame,cmap='gray')
                    plt.show()
                elif der_test < -10:
                    print('decreasing')
                    plt.imshow(frame,cmap='gray')
                    plt.show()
``` Sorry I figured the screenshot would be more helpful since it shows the overflowing occuring
shy scaffold
#

or just put the whole thing in the paste bin

#

!paste

steel masonBOT
#
Pasting large amounts of code

If your code is too long to fit in a codeblock in Discord, you can paste your code here:
https://paste.pythondiscord.com/

After pasting your code, save it by clicking the Paste! button in the bottom left, or by pressing CTRL + S. After doing that, you will be navigated to the new paste's page. Copy the URL and post it here so others can see it.

nova dust
#

Here is the whole script. It is for image processing of an IR video.

#

The pixel intensities in question range from 0-255, hence the overflow of negative numbers to 255-# I believe

shy scaffold
#

try changing converted_list to an array with a float dtype

converted_list = np.array([struct.unpack('b', struct.pack('B', x))[0] for x in sfp2], dtype=np.float32)

you'll need to change the code that comes after it to account for it being an array, and not a list.

#

that should allow the values to go above 2 ** 8 and below 0

nova dust
#
if layer_frame_count % 4 == 0 and layer_frame_count > 20:
                first_five_entries = list(samplewise_max.values())[:5]
                last_four = [entry[-4:] for entry in first_five_entries]
                result = [sublist[3] - sublist[0] for sublist in last_four]
                result2=[x/4 for x in result]
                der_test= sum(result2)/len(result2)
                if der_test > 10:
                    print('increasing')
                    plt.imshow(frame,cmap='gray')
                    plt.show()
                elif der_test < -10:
                    print('decreasing')
                    plt.imshow(frame,cmap='gray')
                    plt.show()```
The if statement in question doesn't draw from converted_list. Converted_list was to account for the overflow being generated previously, sending the numbers back to negative if it occured.
#

Infact I am probably going to delete the whole if statement that converted_list came from

shy scaffold
#

okay. python ints and floats don't overflow like this, so the problem must be coming from some other array-like object (that contains non-python numbers)

#

could it be one of these?

            ret, frame=video_capture.read()
            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
nova dust
#

Probably. That's where the values of the intensity are drawn from

#

The sample_mins, sample_maxs, and sample_sums just do what the name implies for each frame. There are 10 samples in each frame, and those lists store the values for each sample then append them to the main dict's

shy scaffold
#

for what you're doing, if a number goes over 256 or under 0, does that not mean that you made a mistake mathematically?

nova dust
#

Well, no I am trying to take the derivative of the values. Let me explain. Its a lengthy explanation and I am not the best at technical writing so bear with me:

shy scaffold
#

no need. I just wanted to check.

nova dust
#

ok

shy scaffold
#

I haven't worked with cv2. is frame an array? can you do cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY).astype(float)?

shy scaffold
nova dust
#

Wow that was simple

#

Thanks for your help

shy scaffold
#

I mean I guess there are others

#

but I feel like the whole "computer architecture" course could be boiled down to three lectures of relevant content

nova dust
shy scaffold
steel masonBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.