question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Centralize definitions of mathematical and physical constants

See original GitHub issue

Currently, I’ve seen π or some approximation to it be defined in 5 files. This can be shown with the output of grep -r '3\.14' $OPENPILOT_TOPLEVEL:

./selfdrive/car/ford/carcontroller.py:        curvature = self.vehicle_model.calc_curvature(actuators.steerAngle*3.1415/180., CS.out.vEgo)
./selfdrive/car/mock/interface.py:LPG = 2 * 3.1415 * YAW_FR * TS / (1 + 2 * 3.1415 * YAW_FR * TS)
./selfdrive/controls/lib/lateral_mpc/generator.cpp:#define PI 3.1415926536
./selfdrive/modeld/thneed/include/msm_kgsl.h: * are cherry picked into other trees out of order so it is frozen as of 3.14.
./selfdrive/locationd/ublox_msg.cc:      double gpsPi = 3.1415926535898;
./selfdrive/locationd/test/ephemeris.py:    gpsPi = 3.1415926535898

Something similar happens with the gravitational acceleration constant g (often defined as 9.81 or even as 10) and probably with other constants as well. Naturally, this occurs in python source code as much as in c++ code.

I propose that all mathematical and physical constants be defined in a central location. It may be defined in a language-agnostic format and have language-specific files generated at build time for inclusion in source code, e.g. with m4. My proposal is that this file be located in common/constants.txt and the make procedure can generate therefrom constants.py and constants.h.

What are other opinions in regard to this? It’s not a big problem, probably not a problem at all – I don’t think it could be causing any bugs. It can be solved pretty effortlessly though, and I’m willing to implement it.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
pd0wmcommented, Feb 22, 2021

If we had tens or hundreds of physical constants it would probably make sense to add something like that. But don’t think it’s worth the complexity.

I would merge something that replaces all the instances of PI with math.pi and M_PI though!

0reactions
wolterhvcommented, Mar 3, 2021

Oh, I had a feeling about that one. Thanks for letting me know. I’ve updated my branch, I’ll make a pull request.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Constants of Physics and Mathematics - Stan's Hub
Constant Value Dimension Alias Speed of light c 2.997 924 580 e+8 m.s‑1 m/s Permeability of vacuum μ0 12.566 370 614 359 ... e‑7 kg.m.s...
Read more >
Physical constant - Wikipedia
A physical constant, sometimes fundamental physical constant or universal constant, is a physical quantity that is generally believed to be both universal ...
Read more >
Centralization of positive definite functions, weak containment ...
In this paper, we study positive definite functions on the infinite symmetric group S∞ and their relations to its unitary representations. Our principal....
Read more >
Meet the Constants | NIST
The Planck constant is one of the fundamental constants of quantum mechanics. Quantum mechanics shows us that energy is exchanged and absorbed ...
Read more >
The Meaning of Decentralization - Medium
Architectural (de)centralization — how many physical computers is a ... a panacea than a naive mathematical model would sometimes predict.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found