Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

I Rounding decimals to ruler scale (log, pow math?)

  1. Dec 18, 2016 #1
    I have data (all less than 1) and I need to round down to the nearest 1/2, 1/4, 1/8, 1/16, and 1/32.
    I have no idea how to do this mathematically, but I'm guessing I use log or pow functions.

    For example:
    0.81 = 0.5 or 1/2
    0.33 = 0.25 or 1/4
    0.18 = 0.125 or 1/8

    Any help would be appreciated.
  2. jcsd
  3. Dec 18, 2016 #2


    User Avatar
    2017 Award

    Staff: Mentor

    Are you looking for the mathematical functions and symbols to use, or for an algorithmic solution?
  4. Dec 18, 2016 #3


    Staff: Mentor

    What does this mean?
    You can round down to the nearest 1/2 OR to the nearest 1/4 etc. You can't round to the nearest 1/16 AND 1/32, for example.
  5. Dec 18, 2016 #4
    Sorry for not knowing how to phrase my question.
    In the end, I figured it out myself after discovering that pow(0.5, x); gives fractions of the power of 2.

    As for my data:
    On a calculator, round up the result of: ln(x)/ln(0.5)
    And then do a (0.5^y) exponent: 0.5^rounded_UP_result

    In code, it is:
    pow(0.5, ceil(log(x)/log(0.5)));

    Thanks anyway.
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted