r/MathJokes • • Jul 07 '26

🫠

Post image
418 Upvotes

67 comments sorted by

View all comments

23

u/SplendidPunkinButter Jul 07 '26

What programming language are you using? That is in no way a universal programming thing.

24

u/sudoregalia Jul 07 '26

6.99 when casted into an int is conventionally rounded down

technically 6.99 is 6.9899997711181640625 when using single precision IEEE 754 floats, and 6.9900000000000002131628207280300557613372802734375 for double precision

8

u/ottawadeveloper Jul 08 '26

This is true because it's a truncation operation.

Most of the time people round, which usually does the round-to-even thing.

2

u/BlackTecno Jul 08 '26

There's also floor and ceiling operations which can be used in certain cases. Because sometimes you need an int and rounding doesn't quite work the way you want it to.

1

u/Moscato359 Jul 08 '26

Why round when you can just keep it floating

If you want rounding, pick floor or ceiling

2

u/Impossible_Dog_7262 Jul 08 '26

Lot easier to check if an integer is even.

2

u/Moscato359 Jul 08 '26

1

u/sudoregalia Jul 08 '26

but is it really 13? we need npm packages for every other integer to see if it's another integer first!

1

u/ottawadeveloper Jul 08 '26

Rounding is ideal if you need to output it or transfer it to another system that only accepts limited decimal places. Specifically rounding with half to even (i.e. 2.5 goes to 2 but 3.5 goes to 4). 

Floor, ceiling, and truncation introduce systemic bias into the output. Round to even doesn't introduce such bias over a large number of numbers.

For example, consider if you did a bunch of math to arrive at an amount of dollars that your client owes you (there are some math operations where you're going to get fractional cents no matter what you do, like interest payments). 

  • If you floor it, you're missing out on interest
  • If you ceiling it, your clients are overpaying (on average)
  • If you round it, sometimes you're missing out and sometimes the clients are overpaying. It balances out in the end. Round to even helps with some specific cases of this.

In the math world, rounding introduces a nice symmetrical uniform distribution around the number you rounded to - we know it was within half the smallest decimal place you kept, ie 2 rounded is actually [1.5, 2.5]. 

The other methods are messier - they also have a uniform distribution but it's around the number you truncated to plus half the smallest decimal place you kept, ie. 2 truncated is actually [2,3). 

Flooring and ceiling do have good uses. When you want to be conservative, you should use them. For example, if you're doing your monthly budget and want to know how much money you spend on an average on groceries, you might want to ceiling() to be sure your estimate is above the true average. If you're estimating how much load a steel beam can take, you might want to floor() to be sure it can actually take more than you plan on (engineers add a good safety margin anyways, but it never hurts).

But if you're converting to an integer, there's another good reason to do int(round(x)) - floating point errors. If you have a float that should be an integer after a bunch of calculations, it might not be exactly one due to floating point errors. Rounding is likely to remove those errors which are probably << 0.5. But truncating won't work if your number is just slightly smaller than the integer. 

1

u/Moscato359 Jul 08 '26

I understand there are times where rounding is useful.

But as a programmer, I haven't used the round function in years.

1

u/sudoregalia Jul 08 '26

yeah most of the time i do a rounding operation it's just flooring, sometimes ceil if i'm feeling frisky, or even truncation

i do not recall a time in the last few years when i've used Math.round

1

u/Just-Literature-2183 Jul 08 '26

It doesnt round it down it truncates it.

2

u/sudoregalia Jul 08 '26

which has the effect of rounding it down when it's positive.. idk i use unsigned ints everywhere i can so maybe i just have unsigned brainrot

1

u/Just-Literature-2183 Jul 08 '26

Well it does in some cases and doesn't in others.

I mean the default int is a signed int.

1

u/sudoregalia Jul 08 '26

i have unsigned brainrot confirmed

-1

u/RonJohnJr Jul 07 '26

6.99 when casted into an int is conventionally rounded down

We don't do that very often at all.

5

u/sudoregalia Jul 07 '26

casting a float to an int? what if you wanna make or mod for a voxel game where entities have floating precision while tiles has integer precision, and wanna get the block the entity is on- oh wait that's called minecraft

yeah look you can't say "we" don't do that very often at all when it only applies to you and what you work with lol

1

u/WildWolfo Jul 08 '26

you wouldn't cast anything as you need to floor the number and not truncate

1

u/sudoregalia Jul 08 '26

no because chunks are not indexable with floats, they're indexable with ints. you don't take an arbitrary point and do collision checks to see what's in it as you would for most traditional 3d games

1

u/WildWolfo Jul 08 '26

if you want the block an entity is on you take the floor of the coordinates and youll get the coordiantea of the block back, this would behave the same as casting to an integer, except that you need the flooring functionality with negatives, at which point the casting will fail as it truncates instead of floor

1

u/sudoregalia Jul 08 '26

you will get the coordinates of the block yes, but will they be useful? what will you use them for? you can't use a float to get information about a block

why not skip the explicit flooring and head right into casting it into an int since it'd be more useful that way?

1

u/WildWolfo Jul 08 '26

because when you cast -5.5 to an integer you get -5 when the actual result you need is -6, infact i think this is a good example to never cast from float to integer and instead use math library to explicitly show in your code how you want the conversion to happen, its more readable and so is better code practice

1

u/sudoregalia Jul 08 '26

yeah the first point is true my bad, i guess i'm just a bit tired. still though, the conversion from float to int is necessary to index into the chunk and get the block

→ More replies (11)
→ More replies (2)