GOOGLE ADS

Montag, 25. April 2022

Absolutwert einer Zahl finden, ohne Math.abs() zu verwenden

Gibt es eine Möglichkeit, den absoluten Wert einer Zahl zu finden, ohne die Methode Math.abs () in Java zu verwenden.


Lösung des Problems

Wenn Sie in Math.abs nachsehen, finden Sie wahrscheinlich die beste Antwort:

Zum Beispiel für Schwimmer:

 /*
* Returns the absolute value of a {@code float} value.
* If the argument is not negative, the argument is returned.
* If the argument is negative, the negation of the argument is returned.
* Special cases:
* <ul><li>If the argument is positive zero or negative zero, the
* result is positive zero.
* <li>If the argument is infinite, the result is positive infinity.
* <li>If the argument is NaN, the result is NaN.</ul>
* In other words, the result is the same as the value of the expression:
* <p>{@code Float.intBitsToFloat(0x7fffffff & Float.floatToIntBits(a))}
*
* @param a the argument whose absolute value is to be determined
* @return the absolute value of the argument.
*/
public static float abs(float a) {
return (a <= 0.0F)? 0.0F - a: a;
}

Keine Kommentare:

Kommentar veröffentlichen

Warum werden SCHED_FIFO-Threads derselben physischen CPU zugewiesen, obwohl CPUs im Leerlauf verfügbar sind?

Lösung des Problems Wenn ich das richtig verstehe, versuchen Sie, SCHED_FIFO mit aktiviertem Hyperthreading ("HT") zu verwenden, ...