Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Simple math question

Name: Anonymous 2009-12-15 13:56

Hey /bros/, excuse me while I try to come up with the words to explain this basic math concept. I'm trying to "round" values into steps, like so:

1 2 3 4 5 6 7 8 9

might become

1 1 1 4 4 4 7 7 7

or

1 1 1 1 1 6 6 6 6

I know there's a mathematical operation that can do this. If you were to graph the values, let's say as compared to y = x,  it would look like stairs instead of a straight line. (I'm pretty sure that with the right mathematical function the "step size" could be arbitrary.) I know this must be elementary school-level math and yet nothing is coming to me. Any ideas?

Name: Anonymous 2009-12-15 14:18

[expert]
import math

def xround(val, step):
    num_raters = 1
    def LEAHround(star_sum):
        # round to one decimal place and
        # separate into whole and fractional parts
        parts = str(round(star_sum//num_raters, 1)).split('.')
        whole = int(parts[0])
        frac = int(parts[1])
        if frac < 3:
            frac = 0
        elif frac > 7:
            frac = 0
            whole += 1
        else:
            frac = 5
        # recombine for a star rating rounded to the half
        stars = float(str(whole)+'.'+str(frac))
        return stars
    return int(math.floor(LEAHround(val))//step)+1

print "example:"
for vv in xrange(12):
    print xround(vv, 3)
[/expert]

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List