Integer overflow

For questions and comments on programming in general. And for any items that don't fit into the forums below.

Moderators: Benj, Mods

Post Reply
Dutch
Posts: 102
Joined: Mon Aug 19, 2013 8:38 am
Location: Netherlands
Has thanked: 24 times
Been thanked: 5 times
Contact:

Integer overflow

Post by Dutch »

Hi,

Im running in an issue where I found what happens, but cant solve it. I found here in flowcode forum and on line what possible to doo but changing the local variable in an U long does not solve it.

I took a part from my flowcode project and pasted it here. I have 4 8bit data numbers I receive and need to add those for a number to work with. the maximum number I have is inbeteween 0 and 400000 where in my program I need to do all kind of calculations with. turn out the maximum is 32767 for an signed/unsigned integer. how can I use bigger numbers? any tricks or changes I need to set?

the simulation don't show a fault mssage only the display shows a negative number. of coarse making a mess out of all my further calcualtions.
when I compile the message is 'integer overflow in expression'

(help... :( )
Attachments
integer overflow.PNG
integer overflow.PNG (4.02 KiB) Viewed 4422 times
integer overflow_.PNG
integer overflow_.PNG (30.2 KiB) Viewed 4422 times
Integer overflow.fcfx
(8.55 KiB) Downloaded 159 times

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: Integer overflow

Post by mnf »

Hi Angelo,

One problem is that the LCD Print Number routine just takes a 16 bit integer (for the display you've used)

You could use ToStrings$ to convert a 32 bit integer to a string then print that...
Note that you use .data21 / 255 is this what you intended - or are the data elements just 4 bytes of a 32 bit number?
One way to avoid any problems (though not with the display)
Use n32 = d1 (extend d1 to 32 bit)
n32 = n32 << 8 + d2 (Left shift 8 bits and add on next 8 bits)
n32 = n32 << 8 + d3
n32 = n32 << 8 + d4
(Final no = d1d2d3d4 where d1..d4 are 8 bit nos and n32 is a 32 bit unsigned number)

Martin

Dutch
Posts: 102
Joined: Mon Aug 19, 2013 8:38 am
Location: Netherlands
Has thanked: 24 times
Been thanked: 5 times
Contact:

Re: Integer overflow

Post by Dutch »

Hi Martin!

well I don't use the display to show that large number. but I use the number later in my calculations. the print on the display only was to show what goes wrong.

yes the calculation is correct. it are four separate received (Hexadecimal) 8bit numbers. (This is still in the program you helped me big time with last year!)

in the simulation in FC I can put in any number from 0 to 256 for all four bits and the result is fine. just in real the processor does wrong calculations above 32767.

Angelo
Last edited by Dutch on Sat Aug 29, 2020 7:13 pm, edited 2 times in total.

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: Integer overflow

Post by mnf »

This gives the result as expected.....

Note that for left shift 8 bits you need * 256 (not 255) - I'd misread the screenshot on my phone before - you have 255 but are you sure this is what you need?
Integer overflow.fcfx
(8.39 KiB) Downloaded 181 times
Martin

Dutch
Posts: 102
Joined: Mon Aug 19, 2013 8:38 am
Location: Netherlands
Has thanked: 24 times
Been thanked: 5 times
Contact:

Re: Integer overflow

Post by Dutch »

my bad :roll: 256 indeed. my bad. I changed the numbers to 256 in your code.

wow that looks it is what I need. I really need to sit down to see/understand what you did there.

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: Integer overflow

Post by mnf »

Glad it's worked...

Let us know how the project goes...

Martin

Post Reply