#number closest to 0

1 messages Ā· Page 1 of 1 (latest)

worn epoch
#

Tried googling but i actually found close to 0 šŸ˜‰
i thought i could abs and then min but that changes the number so ye. i need it between 2 numbers

balmy latchBOT
#

<@&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>.

#
TJ-Bot
Tried googling but i actually found close to 0 šŸ˜‰ i thought i could abs and then min but that changes the number so ye. i need it between 2 numbers

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.

worn epoch
worn epoch
#

doubles

#

both positive and negative

last wren
#

no, abs doesn't change the number

#

it simply flips a sign bit

worn epoch
#

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

last wren
#

remember the original numbers

worn epoch
#

wdym

#

so i have to make a custom utility method for this?

#

does this actually not exist

#

sounds like such a basic thing tbh

worn epoch
#

ig i dont need that else

last wren
#

yeah

#

can be ternary

worn epoch
#

actulal

#

ye

#

just changed it XD

#

i shouldnt inline it tho right

#

always have the urge to do that with ternaries

dire adder
worn epoch
#

Well yes

worn epoch
#

With a ternary ofc but that was the first version

forest shell
#

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?

dire adder
#

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

worn epoch
#

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

dire adder
worn epoch
#

So I can’t look at what I actually did

worn epoch
#

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

dire adder
#

not that much more complicated, and could save some time if the method is hot (called very frequently). but to each their own

worn epoch
#

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

dire adder
#

because an equality check takes a lot less than 2 abs calls

worn epoch
#

And they are practically never equal

worn epoch
#

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

dire adder
#
Double.longBitsToDouble(Double.doubleToRawLongBits(a) & DoubleConsts.MAG_BIT_MASK);
worn epoch
#

So idk if it’s worth it

dire adder
#

could always benchmark it

#

but not a huge deal, unless you need to optimize

worn epoch
worn epoch
#

It’s called multiple times every tick tho (mc tick aka 20 times a second)

dire adder
#

and if they're doubles, the numbers being equal will be uncommon

#

unless its within a very small range

#

i feel ya

worn epoch
#

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

dire adder
#

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

worn epoch
#

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

dire adder
#

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

worn epoch
#

Not that I ever prematurely optimize because I’m not great at optimizing

#

Oh i didn’t even know there were tools for that 🤣

dire adder
#

profilers, benchmarking tools, etc..

worn epoch
#

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

dire adder
#

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)

worn epoch
#

Oh I just use the in game command

dire adder
#

ah, then that probably gives less info

worn epoch
#

But I don’t understand it

worn epoch
#

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

dire adder
#

yup, which is good for most cases in terms of MC

worn epoch
#

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

dire adder
#

could you show me what info it gives? im curious

#

whenever you have time

worn epoch
#

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

dire adder
#

thats pretty nice

worn epoch
#

Yep

dire adder
#

gotta give props to the MC world for having tools like that easily accessible

#

didnt know it was like that

worn epoch
#

Gotta give credit to mojang for that

#

They added it themselves

dire adder
#

smart move

#

but thats for the client, yeah?

worn epoch
#

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

dire adder
#

i might dive into it later, wanna see what info they include

#

what info they feel is important for troubleshooting MC

worn epoch
#

I’ll send one when I’m home if you want but that’ll be a long while

worn epoch
#

Or you just add your own profiling steps

#

To further pin down where the problem is

dire adder
#

im just curious if they provide classloading info, allocations, stuff like that

worn epoch
#

I think they do provide some info like that

dire adder
#

oh wait a sec

worn epoch
#

Cant see what’s in the files but that’s the structure

dire adder
#

its /jfr start and /jfr stop

#

it just generated a JFR file

worn epoch
#

No no

#

That’s the jfr

#

You can also hold f3 and press L

dire adder
#

yeah, thats the one im curious about

worn epoch
#

Ye you need to hold the f3 key down and then press l and it’ll start profiling for 10 seconds

dire adder
#

its not some sugar coated profiling

worn epoch
#

Tho you can stop early by pressing it again

worn epoch
dire adder
#

its an actual profile, probably configured to capture quite a bit

worn epoch
#

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

dire adder
#

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

worn epoch
#

Cool