Page 1 of 1

Problem with unsigned divide

Posted: Tue Apr 06, 2021 9:04 am
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 3591 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 3591 times

Re: Problem with unsigned divide

Posted: Tue Apr 06, 2021 9:34 am
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

Re: Problem with unsigned divide

Posted: Tue Apr 06, 2021 9:40 am
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 3587 times
(edit: Martin beat me to it!)

Re: Problem with unsigned divide

Posted: Tue Apr 06, 2021 9:57 am
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