Other Crazy result for math operations!!?

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 

Other Crazy result for math operations!!?

3,190 次查看
cicciounico
Contributor III
hi,
I have this problem.
two unsigned int:
UmidRisult = 631;
TempCalcolo = UmidRisult * 4;

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

标签 (1)
标记 (1)
0 项奖励
回复
5 回复数

1,433 次查看
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 项奖励
回复

1,433 次查看
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 项奖励
回复

1,433 次查看
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 项奖励
回复

1,433 次查看
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 项奖励
回复

1,433 次查看
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 项奖励
回复