#number closest to 0
1 messages Ā· Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
Here is an AI assisted attempt to answer your question š¤. Maybe it helps! In any case, a human is on the way š. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.
You can achieve this by using a custom comparison function with Collections.min() or by iterating through the list and finding the number closest to 0. Here's an example using Collections.min():
import java.util.*;
public class ClosestToZero {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(-4, 3, -1, 5, -2);
int closestToZero = Collections.min(numbers, Comparator.comparingInt(Math::abs));
System.out.println("Number closest to 0: " + closestToZero);
}
}
This code will output "Number closest to 0: -1", as -1 is the number in the list that is closest to 0 in terms of absolute distance. You can adjust the list of numbers as needed for your specific use case.
well thats kind of dumb for 2 numbers
whole numbers?
it does change from -1 to 1
like
if i have -2 and 4
i want to get -2
with my method i would get 2
i mean i can do this manually but its something i would expect a method to already exist for
remember the original numbers
wdym
so i have to make a custom utility method for this?
does this actually not exist
sounds like such a basic thing tbh
this should work right?
ig i dont need that else
actulal
ye
just changed it XD
i shouldnt inline it tho right
always have the urge to do that with ternaries
if you're talking about distance, then youll want abs
Well yes
I just did this
With a ternary ofc but that was the first version
This method seems like busy work to whatever its being used for. For example, what if both numbers are equal. Or what if they are both infinity or NaN. Is the value returned still meaningful, or would assuming some state (first is closer than second) and returning a boolean make more sense?
could add some checks to optimize, but the idea of distance seemed to be the problem
could just be an assignment
but you're right, it should be looked at from a productive view
should check if the numbers are equal, should consider whether isFurther should be the proper tool
Nope I actually needed it but I got it working. If both are equal it doesnāt matter which one I get of course because they are the same
I just need to make sure there isnāt a smaller number because if so I need that one
Or bigger if itās negative ofc
I donāt have the code with me tho I forgot to push
would help avoid the abs operations, in the case that they are equal
So I canāt look at what I actually did
Why do I aboid abs?
Def doesnāt seem like an expensive call to me
Extra check makes it way more complicated
And the result will be the exact same
not that much more complicated, and could save some time if the method is hot (called very frequently). but to each their own
I donāt understand how a check like that is faster than abs
Abs is just inverting a number and a check
And only in a single case
Sounds like it would be slower in every case except if they are equal
because an equality check takes a lot less than 2 abs calls
And they are practically never equal
Does it?
Thought abs was just flipping a bit after a check
But ye the chance of them being equal is quite small
It will be called a lot tho
Double.longBitsToDouble(Double.doubleToRawLongBits(a) & DoubleConsts.MAG_BIT_MASK);
So idk if itās worth it
Idk how to do something like that
I donāt think I need to
Itās called multiple times every tick tho (mc tick aka 20 times a second)
and if they're doubles, the numbers being equal will be uncommon
unless its within a very small range
i feel ya
Ye thatās my point
Itās like distance from a point to the floor
But the points move together
And are diff heights
So the floor would have to be aligned perfectly with multiple points for it to be equal
And I donāt see that happening a lot unless someone does that on purpose but even then Iām not sure if itās even possible with the blocks in the game to perfectly align it
it was more "food for thought", spanish was trying to point out it's simplicity, and that it could probably benefit from handling corner-cases
no need to worry about it though, dont prematurely optimize
just if there are some obvious corner cases that could avoid excess processing, always good to account for them
equality check doesnt seem like one you should worry about though
could be cases that better apply to your situation, but wouldnt recommend putting much thought into it if you cant think of any off the top of your head
Fair enough
May I ask whatās bad about prematurely optimizing?
Wouldnāt that be easier than having to find out what the issue is after you realize it is slow and implemented like 10 thinfs
its better to use a tool to find where you can optimize. otherwise you may waste a lot of time optimizing something that only benefits little
Not that I ever prematurely optimize because Iām not great at optimizing
Oh i didnāt even know there were tools for that š¤£
profilers, benchmarking tools, etc..
Ah
Ye I have never really understood how to use the profiler properly š¤£
Havenāt rly needed to as mc has profiling tools built in and those are way better for optimizing
I guess those are benchmarking tools
sorta. spark, yeah?
at least thats the one i was shown
its pretty good, should cover most MC needs
the tool i saw, spark, uses JFR, which is the same tool used in JMC (a popular Java profiling tool)
Oh I just use the in game command
ah, then that probably gives less info
Mc has /jfr command too
But I donāt understand it
Ye but thatās probably the reason why I understand it
It just says what % of time in a tick is spent on a specific thing and it has a lot of other info too
But the % is the most useful
yup, which is good for most cases in terms of MC
The flame chart you get from the jfr is super confusing
It just gives the number of objects for each class or something idk doesnāt seem that useful
I canāt rn because Iām on my phone but
Iāll send over a report later
It even has a folder with ticks that are longer than normal analyzed
So like if you only get it once every 5 ticks you can see what is causing it by comparing to the average
thats pretty nice
Yep
gotta give props to the MC world for having tools like that easily accessible
didnt know it was like that
It also gives server info if youāre on singleplayer
Im not sure if you can trigger a report on multiplayer with a server side command though
Might do it if you are OP
Not sure
But ye there is also /jfr tho idk how useful that is as I donāt understand it
i might dive into it later, wanna see what info they include
what info they feel is important for troubleshooting MC
Iāll send one when Iām home if you want but thatāll be a long while
Most stuff is quite useful but you def also need debug mode along with it
Or you just add your own profiling steps
To further pin down where the problem is
im just curious if they provide classloading info, allocations, stuff like that
I think they do provide some info like that
oh wait a sec
Cant see whatās in the files but thatās the structure
yeah, thats the one im curious about
Ye you need to hold the f3 key down and then press l and itāll start profiling for 10 seconds
its not some sugar coated profiling
Tho you can stop early by pressing it again
Wdym
its an actual profile, probably configured to capture quite a bit
Ye
Itās useful
I use it all the time if I need to get some info about lag or something
Funny how we both learnt something
yeah, gotta love these platforms. great way to learn, to teach, and to get humbled in some situations
definitely rose my interest in the MC domain
Cool