Merge pull request #502 from quaelnix/fastmath-fix

Fix inappropriate approximation of Math.exp
This commit is contained in:
afischerdev 2023-04-10 13:15:34 +02:00 committed by GitHub
commit 7e2973222f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 3 additions and 29 deletions

View file

@ -5,9 +5,6 @@
*/ */
package btools.router; package btools.router;
import btools.util.FastMath;
final class KinematicPath extends OsmPath { final class KinematicPath extends OsmPath {
private double ekin; // kinetic energy (Joule) private double ekin; // kinetic energy (Joule)
private double totalTime; // travel time (seconds) private double totalTime; // travel time (seconds)
@ -57,7 +54,7 @@ final class KinematicPath extends OsmPath {
double curveSpeed = aa > 10. ? 200. / aa : 20.; double curveSpeed = aa > 10. ? 200. / aa : 20.;
double distanceTime = dist / curveSpeed; double distanceTime = dist / curveSpeed;
double decayFactor = FastMath.exp(-distanceTime / km.turnAngleDecayTime); double decayFactor = Math.exp(-distanceTime / km.turnAngleDecayTime);
floatingAngleLeft = (float) (floatingAngleLeft * decayFactor); floatingAngleLeft = (float) (floatingAngleLeft * decayFactor);
floatingAngleRight = (float) (floatingAngleRight * decayFactor); floatingAngleRight = (float) (floatingAngleRight * decayFactor);

View file

@ -5,8 +5,6 @@
*/ */
package btools.router; package btools.router;
import btools.util.FastMath;
final class StdPath extends OsmPath { final class StdPath extends OsmPath {
/** /**
* The elevation-hysteresis-buffer (0-10 m) * The elevation-hysteresis-buffer (0-10 m)
@ -200,7 +198,7 @@ final class StdPath extends OsmPath {
} else if (elevation_buffer < -min_delta) { } else if (elevation_buffer < -min_delta) {
shift = min_delta; shift = min_delta;
} }
double decayFactor = FastMath.exp(-dist / 100.); double decayFactor = Math.exp(-dist / 100.);
float new_elevation_buffer = (float) ((elevation_buffer + shift) * decayFactor - shift); float new_elevation_buffer = (float) ((elevation_buffer + shift) * decayFactor - shift);
double incline = (elevation_buffer - new_elevation_buffer) / dist; double incline = (elevation_buffer - new_elevation_buffer) / dist;
elevation_buffer = new_elevation_buffer; elevation_buffer = new_elevation_buffer;
@ -227,7 +225,7 @@ final class StdPath extends OsmPath {
double f_roll = rc.totalMass * GRAVITY * (rc.defaultC_r + incline); double f_roll = rc.totalMass * GRAVITY * (rc.defaultC_r + incline);
if (rc.footMode) { if (rc.footMode) {
// Use Tobler's hiking function for walking sections // Use Tobler's hiking function for walking sections
speed = rc.maxSpeed * FastMath.exp(-3.5 * Math.abs(incline + 0.05)); speed = rc.maxSpeed * Math.exp(-3.5 * Math.abs(incline + 0.05));
} else if (rc.bikeMode) { } else if (rc.bikeMode) {
speed = solveCubic(rc.S_C_x, f_roll, rc.bikerPower); speed = solveCubic(rc.S_C_x, f_roll, rc.bikerPower);
speed = Math.min(speed, maxSpeed); speed = Math.min(speed, maxSpeed);

View file

@ -1,21 +0,0 @@
package btools.util;
/**
* Some fast approximations to mathematical functions
*
* @author ab
*/
public class FastMath {
/**
* Approximation to Math.exp for small negative arguments
*/
public static double exp(double e) {
double x = e;
double f = 1.;
while (e < -1.) {
e += 1.;
f *= 0.367879;
}
return f * (1. + x * (1. + x * (0.5 + x * (0.166667 + 0.0416667 * x))));
}
}