Makes sense, cause double can represent way bigger numbers than integers.
Also, double can and does in fact represent integers exactly.
Only to 2^54. The amount of integers representable by a long is more. But it can and does represent every int value correctly
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)
It's the same in the the standard c library, so Java is being consistent with a real programming language…
Implying java isn't a real programming language. Smh my head.
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.
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.
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.
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.
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)
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).
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 (?)
But there's really no point in flooring a double outside of the range where integers can be represented accurately, is there.
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.
BigInt (yeah, not native everywhere)
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
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?
int
coerces to double
automatically, without explicit casting
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
I'm learning so much from this thread and I don't even use Java
python is like this also. I don’t remember a language that returned ints
Python 2 returns a float, Python 3 returns an int iirc.
My God this is the most relevant meme I've ever seen
AFAIK most typed languages have this behaviour.
Because ints are way smaller. Over a certain value it would always fail.
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.
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?
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.
It's like Java not having unsigned integers...
Try Math.round. It’s been like ten years since I used Java, but I’m pretty sure it’s in there.
Programmer Humor
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
- Keep content in english
- No advertisements
- Posts must be related to programming or programmer topics