Math problem that won't work


Closed Thread
Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2005
    Location
    New Jersey
    Posts
    425

    Default Math problem that won't work

    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.

  2. #2
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,604


    Did you find this post helpful? Yes | No

    Default Re: Math problem that won't work

    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.

  3. #3
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    3,154


    Did you find this post helpful? Yes | No

    Default Re: Math problem that won't work

    What if you multiply/divide by more than 100 to pick up extra decimals?

    Robert
    Last edited by Demon; - 16th August 2012 at 20:10.

  4. #4
    Join Date
    Oct 2005
    Location
    New Jersey
    Posts
    425


    Did you find this post helpful? Yes | No

    Default Re: Math problem that won't work

    Obviously it's not perfect because (1*100)/127 will still return 0
    I'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.

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts