Other Crazy result for math operations!!?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Other Crazy result for math operations!!?

2,687 Views
cicciounico
Contributor III
hi,
I have this problem.
two unsigned int:
UmidRisult = 631;
TempCalcolo = UmidRisult * 4;

TempCalcolo=....6584!!
I dont understand why!!!
ScreenShot:

Labels (1)
Tags (1)
0 Kudos
Reply
5 Replies

930 Views
stanish
NXP Employee
NXP Employee
Hello cicciounico,

I'd suggest you to check if the value UmidRisult shown by debugger is correct .  Is indeed 631? You may step into SHT_Measure() to see the exact result.
I guess this might be also caused by C optimization switched on. Is the result of the expression correct at the end of the function? If yes then try to turn the compiler optimization off and debug it once more?

Stanish

0 Kudos
Reply

930 Views
cicciounico
Contributor III
Good!
I have removed the optimization ( -O0 ) and now work!

After I have removed also  '^2' : this does wrong result!

Original:
UmidRisult = (-(((((Temp/25)^2)/16)*(28))/10) + Temp*4 - 400)/100;

transformed in:

TempCalcolo    = (Temp * 4) - 400;
TempCalcoloDue = ((Temp/25)*(Temp/25));
UmidRisult = (-((((TempCalcoloDue)/16)*(28))/10) + TempCalcolo)/100;

Thanks a lot!!!
0 Kudos
Reply

930 Views
Lundin
Senior Contributor IV
It doesn't give the wrong result. You are using the bitwise XOR operator ^. C only supports x raised by y for float numbers with the pow() function.
0 Kudos
Reply

930 Views
cicciounico
Contributor III
Reply bigmac: the sensor humidity (sensirion) cannot go over 3400. At 3350 = 100,234% of humidity
Reply Lundin: right! I was wrong. '^' is XOR and not POW
0 Kudos
Reply

930 Views
bigmac
Specialist III
Hello,
 
It would seem that the value of Temp would correspond with a 12-bit range (0-4095).  Is this correct?  If so, the integer calculation for UmidRisult would overflow a signed integer value, at an intermediate point in the calculation, should the value of Temp exceed 3400.
 
Regards,
Mac
 
0 Kudos
Reply