this post was submitted on 05 Aug 2023
541 points (92.5% liked)

Programmer Humor

19713 readers
110 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
[–] [email protected] 105 points 1 year ago (18 children)

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

[–] lysdexic 33 points 1 year ago (1 children)

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

[–] [email protected] 20 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)
[–] [email protected] 27 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.

[–] [email protected] 9 points 1 year ago

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)
[–] [email protected] 67 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…

[–] [email protected] 24 points 1 year ago (5 children)

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

[–] [email protected] 6 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.

[–] [email protected] 10 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.

[–] [email protected] 3 points 1 year ago (1 children)

Composition + Interfaces is love

load more comments (1 replies)
[–] [email protected] 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)
[–] [email protected] 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 (3 replies)
[–] [email protected] 50 points 1 year ago (7 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)

[–] [email protected] 8 points 1 year ago (2 children)

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

[–] [email protected] 5 points 1 year ago

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).

[–] [email protected] 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 (?)

[–] [email protected] 4 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.

load more comments (5 replies)
[–] MarekKnapek 37 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.

[–] [email protected] 7 points 1 year ago (1 children)

BigInt (yeah, not native everywhere)

[–] [email protected] 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

[–] [email protected] 31 points 1 year ago (6 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?

[–] [email protected] 15 points 1 year ago (1 children)

int coerces to double automatically, without explicit casting

[–] [email protected] 18 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 3 points 1 year ago

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

load more comments (5 replies)
[–] [email protected] 27 points 1 year ago (1 children)

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

[–] [email protected] 3 points 1 year ago

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

[–] [email protected] 17 points 1 year ago

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

[–] [email protected] 15 points 1 year ago (1 children)

AFAIK most typed languages have this behaviour.

[–] Lmaydev 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)
[–] [email protected] 6 points 1 year ago (3 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 24 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?

[–] [email protected] 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 (2 replies)
[–] [email protected] 5 points 1 year ago

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

[–] [email protected] 3 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 ›