Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix inappropriate approximation of Math.exp #502

Merged
merged 1 commit into from Apr 10, 2023

Conversation

quaelnix
Copy link
Collaborator

FastMath.exp was discontinuous for x < -1 and thus completely ruined the output of Tobler's hiking function:
brouter_toblers_hiking


It also messed up the decayFactor calculation when the distance between two consecutive nodes was greater than 100m.

@quaelnix quaelnix temporarily deployed to BRouter January 27, 2023 17:25 — with GitHub Actions Inactive
@polyscias
Copy link
Contributor

Good catch!

Can you also plot the graph for the new implementation of this commit?

I am wondering how more expensive it is to use Math.exp instead of these approximations.

@quaelnix
Copy link
Collaborator Author

Can you also plot the graph for the new implementation of this commit?

brouter_toblers_hiking

I am wondering how more expensive it is to use Math.exp instead of these approximations.

Car routing (with KinematicModel) is around 2 % slower when Math.exp is used instead of the new FastMath.exp.

@afischerdev
Copy link
Collaborator

Good pointer.

I found a variante of this

    e = 1. + e / 256.;
    e *= e; e *= e;
    e *= e; e *= e;
    e *= e; e *= e;
    e *= e; e *= e;

A little bit closer to the Math.exp, brings back a correct 0.0 on exp(Double.NEGATIVE_INFINITY) and should not lose too much time.
What do you think?

@quaelnix
Copy link
Collaborator Author

What do you think?

The goal of FastMath.exp should be to achieve the same routing result as if Math.exp had been used, using as few CPU cycles as possible. On modern devices with hardware support for Math.exp there is little room for improvement anyway.

brings back a correct 0.0 on exp(Double.NEGATIVE_INFINITY)

I highly doubt that any of the new variants of FastMath.exp will return 0 if you input negative infinity.

What do you think?

After giving it some more thought, I would say we should remove FastMath altogether. This is pretty much a classic example of "premature optimization" and will likely cause problems again without any significant benefit to us.

@afischerdev
Copy link
Collaborator

I highly doubt that any of the new variants of FastMath.exp will return 0 if you input negative infinity.

Right, my fail. I check also StrictMath.exp and Math.pow(2.71828, e). Too many lines.

After giving it some more thought, I would say we should remove FastMath altogether.

All right, agreed.
I did some tests with a 300km route, about 6000 calls to exp(), ~5800 pts. Only minimal time differences in execution.

@quaelnix
Copy link
Collaborator Author

Right, my fail.

Don't worry, this stuff can be tricky.

Only minimal time differences in execution.

I think FastMath is only called a lot in a hot loop when the car routing models are used. And the old code made less than 2 % difference even in the worst scenario where this function is called millions of times per ~ 300 km.

@quaelnix
Copy link
Collaborator Author

If you want to remove FastMath entirely, feel free to just close this pull request. I sadly won't have time to contribute much to BRouter in the next couple of weeks or months.

@quaelnix
Copy link
Collaborator Author

quaelnix commented Apr 8, 2023

@afischerdev, @abrensch, I think we should address this issue as part of the next update. My preferred solution would be to just drop FastMath altogether. What do you think?

@afischerdev
Copy link
Collaborator

I would agree that and could do the job the next days

FastMath.exp was neither continuous nor strictly monotonically increasing for x < -1 and therefore inappropriate for the intended purpose.
@quaelnix quaelnix temporarily deployed to BRouter April 10, 2023 09:53 — with GitHub Actions Inactive
@quaelnix
Copy link
Collaborator Author

I would agree that

@afischerdev, I have changed my pull request accordingly.

@afischerdev afischerdev merged commit 7e29732 into abrensch:master Apr 10, 2023
1 check passed
@quaelnix quaelnix deleted the fastmath-fix branch April 10, 2023 16:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants