#Math.abs
1 messages Β· Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
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
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.
nah -v, not -1
So like math.abs(-2) which gives the result 2?
yes
likewise math.abs(2) = 2.
I blame 'morning'.
thank you very much @lofty jay @meager sandal
Tho i need to learn this v < 0 ? -v : v expression π
anyways i'm closing this question. Thank you once again.
Closed the thread.
do you know waht an absolute value is?
yes
How far a number is from 0.
7 absolute value would be 7 cuz it's 7 numbers away from 0.
note that Math.abs(Integer.MIN_VALUE) is still negative
because of overflow issues
if you want a safe way use Math.absExact
For questions like this I also recommend looking at the JavaDoc: https://docs.oracle.com/en/java/javase/23/docs/api/java.base/java/lang/Math.html#abs(double) .
declaration: module: java.base, package: java.lang, class: Math
good Math.abs just returns the absolute value of a number, no need to learn anything new
Itβs called a ternary operator, you can Google it if you want
Just a more confusing if else statement
less readable if else statement
if (v < 0) {
return -v
} else {
return v
}```
I stay away from ternary operators
I only use them when I write javascript
It can be horribly misused... but we don't avoid it completely.
Thank you.
Unrelated to the maths.abs but can anyone also explain instanceof?
e.g -
public boolean equals(Objects compared){
if (!(compared instanceof Person)){
return false;
}
}
isnt it pretty obvious?
also you arent passing Object but Objects
Objects is a utility class
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;
}
}
Detected code, here are some useful tools:
this is the code
for a instanceof B returns a boolean that is true if a is an instance of the class B, and false otherwise
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;
}
Thank you.
@meager sierra thank you very much! π
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