this post was submitted on 05 Aug 2023
450 points (91.8% liked)

Programmer Humor

19519 readers
337 users here now

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

founded 1 year ago
MODERATORS
 
top 50 comments
sorted by: hot top controversial new old
[–] RoyaltyInTraining@lemmy.world 95 points 1 year ago (18 children)

Makes sense, cause double can represent way bigger numbers than integers.

[–] lysdexic@programming.dev 31 points 1 year ago (1 children)

Also, double can and does in fact represent integers exactly.

[–] Killing_Spark@feddit.de 18 points 1 year ago (3 children)

Only to 2^54. The amount of integers representable by a long is more. But it can and does represent every int value correctly

load more comments (3 replies)
[–] fsxylo@sh.itjust.works 21 points 1 year ago

Also because if you are dealing with a double, then you're probably dealing with multiple, or doing math that may produce a double. So returning a double just saves some effort.

Yeah it makes sense to me. You can always cast it if you want an int that bad. Hell just wrap the whole function with your own if it means that much to you

(Not you, but like a hypothetical person)

load more comments (14 replies)
[–] jayrhacker@kbin.social 62 points 1 year ago (1 children)

It's the same in the the standard c library, so Java is being consistent with a real programming language…

[–] idunnololz@lemmy.world 23 points 1 year ago (3 children)

Implying java isn't a real programming language. Smh my head.

[–] Glome@feddit.nl 5 points 1 year ago (3 children)

Java has many abstractions that can be beneficial in certain circumstances. However, it forces a design principle that may not work best in every situation.

I.e. inheritance can be both unnatural for the programmer to think in, and is not representative of how data is stored and manipulated on a computer.

[–] whats_a_refoogee@sh.itjust.works 8 points 1 year ago (1 children)

You don't have to use inheritance with Java. In fact, in most cases it's better that you don't. Practically all of the Java standard library doesn't require the use of inheritance, same with most modern libraries.

On the contrary, I think inheritance is a very natural way to think. However, that doesn't translate into readable and easy to maintain code in the vast majority of the cases.

I am not sure what you mean by how it's stored or manipulated on a computer. A garbage collected language like Java manages the memory for you. It doesn't really care if your code is using inheritance or not. And unless you're trying to squeeze the last drops of performance out of your code, the memory layout shouldn't be on your mind.

[–] shotgun_crab@lemmy.world 3 points 1 year ago (1 children)

Composition + Interfaces is love

load more comments (1 replies)
[–] lightsecond@lemmy.world 4 points 1 year ago

We’re gate-keeping the most mainstream programming language now? Next you’ll say English isn’t a real language because it doesn’t have a native verb tense to express hearsay.

load more comments (1 replies)
[–] jayrhacker@kbin.social 5 points 1 year ago (2 children)

Java is, of course, Turing Complete™️ but when you have to hide all the guns and knives in jdk.internal.misc.Unsafe something is clearly wrong.

load more comments (2 replies)
load more comments (1 replies)
[–] korstmos@kbin.social 41 points 1 year ago (3 children)

Doubles have a much higher max value than ints, so if the method were to convert all doubles to ints they would not work for double values above 2^31-1.

(It would work, but any value over 2^31-1 passed to such a function would get clamped to 2^31-1)

[–] karlthemailman@sh.itjust.works 9 points 1 year ago (2 children)

So why not return a long or whatever the 64 bit int equivalent is?

To avoid a type conversion that might not be expected. Integer math in Java differs from floating point math.

Math.floor(10.6) / Math.floor(4.6) = 2.5 (double)

If floor returned a long, then

Math.floor(10.6) / Math.floor(4.6) = 2 (long)

If your entire code section is working with doubles, you might not like finding Math.floor() unexpectedly creating a condition for integer division and messing up your calculation. (Have fun debugging this if you're not actively aware of this behavior).

[–] korstmos@kbin.social 4 points 1 year ago

Because even a long (64-bit int) is too small :)
A long can hold 2^64-1 = 1.84E19
A double can hold 1.79E308

Double does some black magic with an exponent, and can hold absolutely massive numbers!

Double also has some situations that it defines as "infinity", a concept that does not exist in long as far as I know (?)

[–] parlaptie@feddit.de 3 points 1 year ago

But there's really no point in flooring a double outside of the range where integers can be represented accurately, is there.

[–] affiliate@lemmy.world 2 points 1 year ago (2 children)
[–] idunnololz@lemmy.world 6 points 1 year ago (1 children)
[–] affiliate@lemmy.world 3 points 1 year ago (1 children)

yeah that would be pretty effective. could also go to three just to be safe

load more comments (1 replies)
load more comments (1 replies)
[–] MarekKnapek@programming.dev 34 points 1 year ago (1 children)

Makes sense, how would you represent floor(1e42) or ceil(1e120) as integer? It would not fit into 32bit (unsigned) or 31bit (signed) integer. Not even into 64bit integer.

[–] natanael@lemmy.ml 6 points 1 year ago (1 children)

BigInt (yeah, not native everywhere)

[–] 1rre@discuss.tchncs.de 3 points 1 year ago

I feel this is worse than double though because it's a library type rather than a basic type but I guess ceil and floor are also library functions unlike toInt

[–] rev@ihax0r.com 26 points 1 year ago (1 children)

python is like this also. I don’t remember a language that returned ints

[–] shotgun_crab@lemmy.world 3 points 1 year ago

Python 2 returns a float, Python 3 returns an int iirc.

[–] maniacal_gaff@lemmy.world 25 points 1 year ago (2 children)

It would be kinda dumb to force everyone to keep casting back to a double, no? If the output were positive, should it have returned an unsigned integer as well?

[–] larvyde@sh.itjust.works 10 points 1 year ago (1 children)

int coerces to double automatically, without explicit casting

[–] snake_baitman@lemmy.sdf.org 16 points 1 year ago (1 children)

The CPU has to do real work to convert between float and int types. Returning an int type would just be giving the CPU extra work to do for no reason

[–] notnotmike@programming.dev 3 points 1 year ago

I'm learning so much from this thread and I don't even use Java

[–] baseless_discourse@mander.xyz 2 points 1 year ago (1 children)

I think one of the main reason to use floor/ceilling is to predictably cast a double into int. This type signature kind of defeats this important purpose.

I don't know this historical context of java, but possibly at that time, people see type more of a burden than a way to garentee correctness? (which is kind of still the case for many programmers, unfortunately.

[–] BorgDrone@lemmy.one 11 points 1 year ago* (last edited 1 year ago) (1 children)

You wouldn’t need floor/ceil for that. Casting a double to an int is already predictable as the java language spec explicitly says how to do it, so any JVM will do this the exact same way.

The floor/ceil functions are simply primitive math operations and they are meant to be used when doing floating point math.

All math functions return the same type as their input parameters, which makes sense. The only exception are those that are explicitly meant for converting between types.

[–] baseless_discourse@mander.xyz 2 points 1 year ago (2 children)

"predicable" in the sense that people know how it works regardless what language they know.

I guess I mean "no surprise for the reader", which is more "readability" than "predictability"

load more comments (2 replies)
[–] CaptainHowdy@lemm.ee 16 points 1 year ago

My God this is the most relevant meme I've ever seen

[–] Acetamide@lemmy.world 13 points 1 year ago (1 children)

AFAIK most typed languages have this behaviour.

[–] Lmaydev@programming.dev 4 points 1 year ago (2 children)

Because ints are way smaller. Over a certain value it would always fail.

load more comments (2 replies)
[–] Aelorius@jlai.lu 6 points 1 year ago (2 children)

Logic, in math, if you have a real and you round it, it's always a real not an integer. If we follow your mind with abs(-1) of an integer it should return a unsigned and that makes no sense.

[–] kogasa@programming.dev 22 points 1 year ago (1 children)

in math, if you have a real and you round it, it's always a real not an integer.

No, that's made up. Outside of very specific niche contexts the concept of a number having a single well-defined type isn't relevant in math like it is in programming. The number 1 is almost always considered both an integer and a real number.

If we follow your mind with abs(-1) of an integer it should return a unsigned and that makes no sense.

How does that not make sense? abs is always a nonnegative integer value, why couldn't it be an unsigned int?

[–] Aelorius@jlai.lu 6 points 1 year ago

I'm ok with that, but what I mean is that it makes no sense to change the type of the provided variable if in mathematics the type can be the same.

load more comments (1 replies)
[–] Fantasmita@lib.lgbt 4 points 1 year ago

It's like Java not having unsigned integers...

[–] MrGeekman@lemmy.world 2 points 1 year ago

Try Math.round. It’s been like ten years since I used Java, but I’m pretty sure it’s in there.

load more comments
view more: next ›