13
0
geforkt von Mirrors/Paper

Use simple multiplication for squaring. Fixes BUKKIT-4836

This change adds a method to NumberConversions for squaring and
replaces uses of Math.pow(..., 2) with the new method for efficiency
reasons.

By: Wesley Wolfe <weswolf@aol.com>
Dieser Commit ist enthalten in:
Bukkit/Spigot 2013-10-09 01:56:35 -05:00
Ursprung 5b7e75839f
Commit db05700052
3 geänderte Dateien mit 12 neuen und 8 gelöschten Zeilen

Datei anzeigen

@ -334,7 +334,7 @@ public class Location implements Cloneable {
* @return the magnitude * @return the magnitude
*/ */
public double length() { public double length() {
return Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2) + Math.pow(z, 2)); return Math.sqrt(NumberConversions.square(x) + NumberConversions.square(y) + NumberConversions.square(z));
} }
/** /**
@ -345,7 +345,7 @@ public class Location implements Cloneable {
* @return the magnitude * @return the magnitude
*/ */
public double lengthSquared() { public double lengthSquared() {
return Math.pow(x, 2) + Math.pow(y, 2) + Math.pow(z, 2); return NumberConversions.square(x) + NumberConversions.square(y) + NumberConversions.square(z);
} }
/** /**
@ -381,7 +381,7 @@ public class Location implements Cloneable {
throw new IllegalArgumentException("Cannot measure distance between " + getWorld().getName() + " and " + o.getWorld().getName()); throw new IllegalArgumentException("Cannot measure distance between " + getWorld().getName() + " and " + o.getWorld().getName());
} }
return Math.pow(x - o.x, 2) + Math.pow(y - o.y, 2) + Math.pow(z - o.z, 2); return NumberConversions.square(x - o.x) + NumberConversions.square(y - o.y) + NumberConversions.square(z - o.z);
} }
/** /**

Datei anzeigen

@ -20,6 +20,10 @@ public final class NumberConversions {
return floor(num + 0.5d); return floor(num + 0.5d);
} }
public static double square(double num) {
return num * num;
}
public static int toInt(Object object) { public static int toInt(Object object) {
if (object instanceof Number) { if (object instanceof Number) {
return ((Number) object).intValue(); return ((Number) object).intValue();

Datei anzeigen

@ -152,7 +152,7 @@ public class Vector implements Cloneable, ConfigurationSerializable {
* @return the magnitude * @return the magnitude
*/ */
public double length() { public double length() {
return Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2) + Math.pow(z, 2)); return Math.sqrt(NumberConversions.square(x) + NumberConversions.square(y) + NumberConversions.square(z));
} }
/** /**
@ -161,7 +161,7 @@ public class Vector implements Cloneable, ConfigurationSerializable {
* @return the magnitude * @return the magnitude
*/ */
public double lengthSquared() { public double lengthSquared() {
return Math.pow(x, 2) + Math.pow(y, 2) + Math.pow(z, 2); return NumberConversions.square(x) + NumberConversions.square(y) + NumberConversions.square(z);
} }
/** /**
@ -175,7 +175,7 @@ public class Vector implements Cloneable, ConfigurationSerializable {
* @return the distance * @return the distance
*/ */
public double distance(Vector o) { public double distance(Vector o) {
return Math.sqrt(Math.pow(x - o.x, 2) + Math.pow(y - o.y, 2) + Math.pow(z - o.z, 2)); return Math.sqrt(NumberConversions.square(x - o.x) + NumberConversions.square(y - o.y) + NumberConversions.square(z - o.z));
} }
/** /**
@ -185,7 +185,7 @@ public class Vector implements Cloneable, ConfigurationSerializable {
* @return the distance * @return the distance
*/ */
public double distanceSquared(Vector o) { public double distanceSquared(Vector o) {
return Math.pow(x - o.x, 2) + Math.pow(y - o.y, 2) + Math.pow(z - o.z, 2); return NumberConversions.square(x - o.x) + NumberConversions.square(y - o.y) + NumberConversions.square(z - o.z);
} }
/** /**
@ -346,7 +346,7 @@ public class Vector implements Cloneable, ConfigurationSerializable {
* @return whether this vector is in the sphere * @return whether this vector is in the sphere
*/ */
public boolean isInSphere(Vector origin, double radius) { public boolean isInSphere(Vector origin, double radius) {
return (Math.pow(origin.x - x, 2) + Math.pow(origin.y - y, 2) + Math.pow(origin.z - z, 2)) <= Math.pow(radius, 2); return (NumberConversions.square(origin.x - x) + NumberConversions.square(origin.y - y) + NumberConversions.square(origin.z - z)) <= NumberConversions.square(radius);
} }
/** /**