Problem with unsigned divide

For general Flowcode discussion that does not belong in the other sections.
Post Reply
User avatar
p.erasmus
Posts: 434
http://meble-kuchenne.info.pl
Joined: Thu Dec 03, 2020 12:01 pm
Location: Russia / Россия
Has thanked: 104 times
Been thanked: 88 times

Problem with unsigned divide

Post by p.erasmus »

Hi All,
I am having an problem with a unsigned divide I am not sure what I am missing or doing wrong

Variable Raw is a unsigned int and factor is also an unsigned int value 32768 by dividing it to get the Resistance send from the MAX31865 SPI as per datasheet my result is always 0.0 ,Resistance is a float variable ,
when I just use a value of 32 for the factor then I get a result it is somehow connected to the 32768 number ,

How can I solve this or how can I use the type conversion component to do this
P1.jpg
P1.jpg (25.16 KiB) Viewed 2175 times

Divide by 32 give a correct result but using the factor of 32768 does not work what Am I doing wrong
P2.jpg
P2.jpg (22.35 KiB) Viewed 2175 times
Regards Peter - QME Electronics

mnfisher
Valued Contributor
Posts: 1046
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 107 times
Been thanked: 538 times

Re: Problem with unsigned divide

Post by mnfisher »

Hi Peter,

Currently your result is changed to float after the division..

Either need
fresult = n / 32.0
or fresult = float(n) / 32

Martin

Steve-Matrix
Matrix Staff
Posts: 1310
Joined: Sat Dec 05, 2020 10:32 am
Has thanked: 175 times
Been thanked: 299 times

Re: Problem with unsigned divide

Post by Steve-Matrix »

Raw and Factor are both integers, and so the result of their division will also (initially) be an integer. This will be zero because Factor is high. Only then is it converted to a float.

Instead, you should do the divide using at least one float number.

One way to do this is to first set Resistance = Raw. Then perform Resistance = Resistance / Factor.
float_maths.png
float_maths.png (53.23 KiB) Viewed 2171 times
(edit: Martin beat me to it!)

User avatar
p.erasmus
Posts: 434
Joined: Thu Dec 03, 2020 12:01 pm
Location: Russia / Россия
Has thanked: 104 times
Been thanked: 88 times

Re: Problem with unsigned divide

Post by p.erasmus »

Hi Martin / Steve

Thanks so much you pulled me out of this loop :D
I almost have no hair left

Many thanks again it is working spot on now :D
Regards Peter - QME Electronics

Post Reply