#Math.abs

1 messages Β· Page 1 of 1 (latest)

brave pilot
#

Can someone explain to me what Math.abs is?

kind trailBOT
#

<@&987246399047479336> please have a look, thanks.

lofty jay
#

returns an absolute value of the given parameter

#

absolute value being non-negative value of given number

#

-1 would become 1

#

2 will be still 2

meager sandal
#

It amounts to v < 0 ? -v : v - however it is marked as an 'Intrinsic candidate' which means it can be replaced by native code if the platform has a known solution that is more efficient.

brave pilot
lofty jay
#

yes

brave pilot
#

likewise math.abs(2) = 2.

meager sandal
brave pilot
#

thank you very much @lofty jay @meager sandal

brave pilot
#

anyways i'm closing this question. Thank you once again.

kind trailBOT
#

Closed the thread.

meager sierra
brave pilot
#

How far a number is from 0.

#

7 absolute value would be 7 cuz it's 7 numbers away from 0.

runic carbon
#

note that Math.abs(Integer.MIN_VALUE) is still negative

#

because of overflow issues

#

if you want a safe way use Math.absExact

deep aurora
meager sierra
# brave pilot yes

good Math.abs just returns the absolute value of a number, no need to learn anything new

maiden barn
eager tangle
#

Just a more confusing if else statement

runic carbon
#

less readable if else statement

eager tangle
#
if (v < 0) {
    return -v
} else {
    return v
}```
#

I stay away from ternary operators

#

I only use them when I write javascript

meager sandal
#

It can be horribly misused... but we don't avoid it completely.

brave pilot
#

Unrelated to the maths.abs but can anyone also explain instanceof?

e.g -

public boolean equals(Objects compared){
     if (!(compared instanceof Person)){
          return false;
     }
}
lofty jay
#

isnt it pretty obvious?

#

also you arent passing Object but Objects

#

Objects is a utility class

brave pilot
#

Ah objects was a typo.

#
public class SimpleDate {
    private int day;
    private int month;
    private int year;

    public SimpleDate(int day, int month, int year) {
        this.day = day;
        this.month = month;
        this.year = year;
    }

    public int getDay() {
        return this.day;
    }

    public int getMonth() {
        return this.month;
    }

    public int getYear() {
        return this.year;
    }

    public boolean equals(Object compared) {
        // if the variables are located in the same position, they are equal
        if (this == compared) {
            return true;
        }

        // if the type of the compared object is not SimpleDate, the objects are not equal
        if (!(compared instanceof SimpleDate)) {
            return false;
        }

        // convert the Object type compared object
        // into a SimpleDate type object called comparedSimpleDate
        SimpleDate comparedSimpleDate = (SimpleDate) compared;

        // if the values of the object variables are the same, the objects are equal
        if (this.day == comparedSimpleDate.day &&
            this.month == comparedSimpleDate.month &&
            this.year == comparedSimpleDate.year) {
            return true;
        }

        // otherwise the objects are not equal
        return false;
    }

    @Override
    public String toString() {
        return this.day + "." + this.month + "." + this.year;
    }
}
kind trailBOT
brave pilot
#

this is the code

meager sierra
#

for a instanceof B returns a boolean that is true if a is an instance of the class B, and false otherwise

unkempt sandal
#
    public boolean equals(Object compared) {
        // if the variables are located in the same position, they are equal
        if (this == compared) {
            return true;
        }

        // if the type of the compared object is not SimpleDate, the objects are not equal
        if (!(compared instanceof SimpleDate)) {
            return false;
        }

        // convert the Object type compared object
        // into a SimpleDate type object called comparedSimpleDate
        SimpleDate comparedSimpleDate = (SimpleDate) compared;

        // if the values of the object variables are the same, the objects are equal
        if (this.day == comparedSimpleDate.day &&
            this.month == comparedSimpleDate.month &&
            this.year == comparedSimpleDate.year) {
            return true;
        }

        // otherwise the objects are not equal
        return false;
    }
#

so this is what we start with

#

what we can do is

#
public boolean equals(Object compared) {
    if (this == compared) {
        return true;
    }
    
    if (compared instanceof SimpleDate comparedSimpleDate) {
        return this.day == comparedSimpleDate.day &&
            this.month == comparedSimpleDate.month &&
            this.year == comparedSimpleDate.year;
    }

    return false;
}
#

if you put a variable name after the type in instanceof it does the same as this

#
SimpleDate comparedSimpleDate = (SimpleDate) compared;
#

now, we don't really need the == check

#

at least not for correctness

#

which trims us down to this

#
public boolean equals(Object compared) {
    if (compared instanceof SimpleDate comparedSimpleDate) {
        return this.day == comparedSimpleDate.day &&
            this.month == comparedSimpleDate.month &&
            this.year == comparedSimpleDate.year;
    }

    return false;
}
#

now last trick - instanceof can be used in an &&

#
public boolean equals(Object compared) {
    return compared instanceof SimpleDate comparedSimpleDate &&
        this.day == comparedSimpleDate.day &&
        this.month == comparedSimpleDate.month &&
        this.year == comparedSimpleDate.year;
}
brave pilot
#

@meager sierra thank you very much! πŸ™

eager tangle
#

You use it when one of your methods can take in several types of objects, but depending on what object it is, you want to do something different