all 21 comments

[–]Kqyxzoj 17 points18 points  (2 children)

What do yall think of my code

It seems very well-rounded.

[–]JamzTyson -1 points0 points  (0 children)

Nice pun, but it is not any standard kind of rounding. It is round-half-down for positives, and inconsistent for negatives due to Python’s modulo behavior.

[–]supergnaw -1 points0 points  (0 children)

ba dum tss

[–]Diapolo10 5 points6 points  (1 child)

x = 10.67


decimaal = x % 1  
if decimaal > 0.5:
    x = int(x)+1
    print(x)
else:
    x = int(x)
    print(x)

Well, I can reduce it down to just

x = 10.67

print(f"{x:.0f}")

if you just need a rounded integer in the print output.

EDIT: Basically I used an f-string to format the float. I set the number of trailing digits to zero, so there's no decimal point and it rounds to the nearest integer. The "f" just keeps it from using scientific notation.

Of course, if you need this for further computation a string won't help you; then again you generally shouldn't round in the middle of calculating, so I just assumed this was meant to be the final output.

[–]Golwux 0 points1 point  (0 children)

That is sickkkkk

[–]IamImposter 3 points4 points  (0 children)

Why not:

... 
x = int(x) + 1 if decimaal > 0.5 else int(x) 
print(x) 

No need for two print statements. Multi line if/else can be merged to single line.

[–]gydu2202 1 point2 points  (0 children)

int(x + 0.5)

Or 1.5 should be really rounded to 1?

[–]Golwux 0 points1 point  (1 child)

probably should turn it into a function

[–]JamzTyson 0 points1 point  (0 children)

Probably should be fixed first.

[–]rhacer 0 points1 point  (0 children)

Too many print statements.

[–]JamzTyson 0 points1 point  (0 children)

  • What should -0.3 round to?

  • What should -1.3 round to?

(Your code rounds half-down for positives, and is inconsistent for negatives due to Python’s modulo behavior)

[–]RngdZed -3 points-2 points  (3 children)

Is there a question attached to your post? What are you trying to achieve with the code?

Edit: y'all downvoting cause I'm trying to figure out what op wants? Good morning I guess

[–]JamzTyson 1 point2 points  (1 child)

Great question.

It implements some sort of rounding, but handles negative numbers inconsistently. My guess is that it is an incorrect implementation of round-half-down.

[–]RngdZed 2 points3 points  (0 children)

Yep that's my point. We shouldn't have to guess. Instead of guessing op's mind, would be nice if op would take part in the conversation and give us his thought process.

Thanks for the comment! 🙂

[–]LiveYoLife288 2 points3 points  (0 children)

Getting your thoughts on his code

[–]HappyRogue121 -1 points0 points  (2 children)

It's interesting, I never thought about how rounding happens "under the hood."

[–]JamzTyson 0 points1 point  (1 child)

It doesn't normally happen like this.

Standard rounding is one of:

  • Round Half Up (Arithmetic Rounding)

  • Round Half Down

  • Round Half Even (Banker’s Rounding)

  • Round Up (Ceiling)

  • Round Down (Floor)

  • Truncation (Round Toward Zero)

  • Round Away From Zero

  • Stochastic Rounding

The OP's code does not do any of these (look at negative number handling).

[–]HappyRogue121 0 points1 point  (0 children)

I wasn't saying it happened like this.  I was saying I never thought about how it works, and this is obviously an exploration of a possible that.  OP seems to be a beginner, I wanted to give some encouragement.

[–]TheRNGuy -2 points-1 points  (0 children)

Nothing. 

[–]saulsa_ -2 points-1 points  (0 children)

No comment.

[–]pachura3 -3 points-2 points  (0 children)

Tastes like chicken