PDA

View Full Version : Math problem that won't work



Christopher4187
- 16th August 2012, 13:29
Why won't this work?

Data6 is a variable that ranges from 0 to 127

Light = (Data6/127)*100

I get 0 from 0-126 and 127 gives me 100.


I had a similar issue with another math problem and I'm guessing they are related. I had to use a lookup table to solve this one and if I can get rid of the lookup table it would save me some code space.

T=((DATA4*9/5)+32)*10

I know you can't use decimal points when multiplying but I think you could multiply by 10 and then use the //10 to get the remainder but in this case I'm just getting 0.

HenrikOlsson
- 16th August 2012, 13:44
Hi,
PBP works with integers only.
1/127=0.0079 which will be truncated to 0, 0*100=0
126/127=0.99 which will be truncated to 0, 0*100=0
127/127=1, 1*100=100

Multiply it by 100 first - then divide it by 127.
126*100=12600, 12600/127=99
Obviously it's not perfect because (1*100)/127 will still return 0

/Henrik.

Demon
- 16th August 2012, 19:58
What if you multiply/divide by more than 100 to pick up extra decimals?

Robert

Christopher4187
- 16th August 2012, 23:59
Obviously it's not perfect because (1*100)/127 will still return 0I'm not making NASA type products here so even if there is a 2 or 3% error, the end user probably wouldn't know. And if they did know, they won't care. That tip worked out great, thanks Henrik.

What if you multiply/divide by more than 100 to pick up extra decimals?The more the merrier but you can't have an amount that is greater than a word sized variable. I've found that in most cases you don't need a long to do decent math.