Other Crazy result for math operations!!?

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

Other Crazy result for math operations!!?

2,353件の閲覧回数
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 返答(返信)

596件の閲覧回数
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 件の賞賛

596件の閲覧回数
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 件の賞賛

596件の閲覧回数
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 件の賞賛

596件の閲覧回数
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 件の賞賛

596件の閲覧回数
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 件の賞賛