PDA

View Full Version : Doing Simple Math - getting the wrong answer



Tom Gonser
- 5th March 2005, 19:18
Probably a 'newbie' error, but it has me cornered...

I have a routine that is trying to take degrees C and convert to degrees F.. However the math is not working.

Here is how it works:

1.) I take data into an array and read that data tha deals with temperature and convert from hex to decimal

' read data for temperature Temp
SSD4= (ssmax[24]>>4)
SSD3= (ssmax[24] & $f)
SSD2= (ssmax[23]>>4)
SSD1= (ssmax[23] & $f)

' convert to Decimal:

Dec_temp=(ssd4*4096)+(ssd3*256)+(ssd2*16)+ssd1

2) I read these values out into a screen to show degrees (C), then write it out to the screen:

serout2 cpinout, 84,["Temp(C): ",#Dec_temp dig 3, #Dec_temp dig 2,#Dec_temp dig 1, ".",dec1 Dec_temp]

THIS WORKS FINE.. I get the proper 'Degrees C'...

"Temp(C): 025.5 " shows up in HyperTerminal. No problem..

BUT! Here is where it gets funky.. What if I wanted to see Degrees F?

3) I multiply:
Dec_tempf=(Dec_temp*(9/5) + 32) ' convert to farenheit

And output with this:

serout2 cpinout, 84,["Temp(F): ",#Dec_tempf dig 3, #Dec_tempf dig 2,#Dec_tempf dig 1, ".",dec1 Dec_tempf]

The display The same serout2, but now in (F)

It shows "Temp(F): 025.5" - What the heck?? That is the same as (C)???

So then I decide to look at the numbers underneath:

First - theBase # is "223" This is the value in #Dec_temp

Then look at the math:
Testmath1 - (Dec_temp*(9/5)+32) = 255 -- this is wrong

Or even look at **:
Testmath2 - (Dec_temp**(9/5)+32) 32 -- still odd..

Can anyone solve this puzzle?

Thanks

Tom

anj
- 5th March 2005, 22:28
First - theBase # is "223" This is the value in #Dec_temp

Then look at the math:
Testmath1 - (Dec_temp*(9/5)+32) = 255 -- this is wrong

PBP uses full precedence of math operators, and also uses braces to force out of order of calculation.
Secondly it also only works in integers.

As such (9/5) = 1.8 = 1 ( and is calculated first )
hence Dec_tempf = 223 * 1 +32 = 255

try ( Dec_temp*( 90 / 5 ) + 320 ) / 10
ie ( Dec_temp*18 + 320 ) / 10

Also, if you just leave it at ( Dec_temp*18 + 320 ) you can use simple math to get the basic temp and the first decimal.

Andrew

Ingvar
- 8th March 2005, 09:51
Hi Tom,

PBP can't handle fractions very well, the "**" and "*/" operators however makes it possible. 9/5 is 1.8 which PBP will trunctate to 1, not very useful. The "**" operator can handle the fractional part, this operaton makes an "invisible" division by 65536. 0.8*65536=52428.8 which we round up to 52429. Since your Dec_temp variable contains the temerature in tenths of a degree your formula needs to be ......

Dec_tempf=Dec_temp*(9/5) + 320 'Farenheit * 10

We rewrite this to ...
Dec_tempf=Dec_temp*1.8 + 320 'Farenheit * 10

Which is the same as .....
Dec_tempf=Dec_temp*1+Dec_temp*0.8 + 320 'Farenheit * 10

Using "**", we end up with ......
Dec_tempf=Dec_temp + Dec_temp**52429 + 320

Another thing, you can replace ......

' read data for temperature Temp
SSD4= (ssmax[24]>>4)
SSD3= (ssmax[24] & $f)
SSD2= (ssmax[23]>>4)
SSD1= (ssmax[23] & $f)

' convert to Decimal:

Dec_temp=(ssd4*4096)+(ssd3*256)+(ssd2*16)+ssd1

...... with ......

Dec_temp.highbyte = ssmax[24]
Dec_temp.lowbyte = ssmax[23]

/Ingvar

Luciano
- 8th March 2005, 14:27
Fahrenheit = Celsius * (212 - 32)/100 + 32


Example 30 Celsius to Fahrenheit:


30 * 180 / 100 + 32 = 86 Fahrenheit


Best regards,

Luciano