Repository: astropy/astropy

Issue:
find_equivalent_units returns wrong result for number density
### Description

When asking for equivalent units for a number density, I am getting units of length.

### Expected behavior

I would have thought that any unit in the returned output should be number density units.

### How to Reproduce

```python
In [23]: u.Unit('m**-3').find_equivalent_units()
Out[23]: 
  Primary name | Unit definition | Aliases                         
[
  AU           | 1.49598e+11 m   | au, astronomical_unit            ,
  Angstrom     | 1e-10 m         | AA, angstrom                     ,
  cm           | 0.01 m          | centimeter                       ,
  earthRad     | 6.3781e+06 m    | R_earth, Rearth                  ,
  jupiterRad   | 7.1492e+07 m    | R_jup, Rjup, R_jupiter, Rjupiter ,
  lsec         | 2.99792e+08 m   | lightsecond                      ,
  lyr          | 9.46073e+15 m   | lightyear                        ,
  m            | irreducible     | meter                            ,
  micron       | 1e-06 m         |                                  ,
  pc           | 3.08568e+16 m   | parsec                           ,
  solRad       | 6.957e+08 m     | R_sun, Rsun                      ,
]
```

### Versions

```
macOS-14.1.1-arm64-arm-64bit
Python 3.11.2 (main, Mar 23 2023, 23:28:33) [Clang 14.0.0 (clang-1400.0.29.202)]
astropy 6.0.0
Numpy 1.26.0
pyerfa 2.0.0.3
Scipy 1.10.1
Matplotlib 3.6.3
```


Discussion:
github-actions[bot]: Thank you for your contribution to Astropy! 🌌 This checklist is meant to remind the package maintainers who will review this pull request of some common things to look for.

  - [ ] Do the proposed changes actually accomplish desired goals?
  - [ ] Do the proposed changes follow the [Astropy coding guidelines](https://docs.astropy.org/en/latest/development/codeguide.html)?
  - [ ] Are tests added/updated as required? If so, do they follow the [Astropy testing guidelines](https://docs.astropy.org/en/latest/development/testguide.html)?
  - [ ] Are docs added/updated as required? If so, do they follow the [Astropy documentation guidelines](https://docs.astropy.org/en/latest/development/docguide.html#astropy-documentation-rules-and-guidelines)?
  - [ ] Is rebase and/or squash necessary? If so, please provide the author with appropriate instructions. Also see instructions for [rebase](https://docs.astropy.org/en/latest/development/workflow/development_workflow.html#rebase-if-necessary) and [squash](https://docs.astropy.org/en/latest/development/workflow/development_workflow.html#squash-if-necessary).
  - [ ] Did the CI pass? If no, are the failures related? If you need to run daily and weekly cron jobs as part of the PR, please apply the "Extra CI" label. Codestyle issues can be fixed by the [bot](https://docs.astropy.org/en/latest/development/workflow/development_workflow.html#pre-commit).
  - [ ] Is a change log needed? If yes, did the change log check pass? If no, add the "no-changelog-entry-needed" label. If this is a manual backport, use the "skip-changelog-checks" label unless special changelog handling is necessary.
  - [ ] Is this a big PR that makes a "What's new?" entry worthwhile and if so, is (1) a "what's new" entry included in this PR and (2) the "whatsnew-needed" label applied?
  - [ ] At the time of adding the milestone, if the milestone set requires a backport to release branch(es), apply the appropriate "backport-X.Y.x" label(s) *before* merge.

Patch:
diff --git a/astropy/units/core.py b/astropy/units/core.py
index 9233433bd3..d8f2b5d906 100644
--- a/astropy/units/core.py
+++ b/astropy/units/core.py
@@ -1718,7 +1718,9 @@ class UnitBase:
             max_depth=1,
             include_prefix_units=include_prefix_units,
         )
-        results = {x.bases[0] for x in results if len(x.bases) == 1}
+        results = {
+            x.bases[0] for x in results if len(x.bases) == 1 and x.powers[0] == 1
+        }
         return self.EquivalentUnitsList(results)
 
     def is_unity(self):
diff --git a/docs/changes/units/16127.bugfix.rst b/docs/changes/units/16127.bugfix.rst
new file mode 100644
index 0000000000..9b3e2caec5
--- /dev/null
+++ b/docs/changes/units/16127.bugfix.rst
@@ -0,0 +1,4 @@
+Ensure that ``find_equivalent_units`` only returns actual units, not units
+that raised to some power match the requested one.  With this fix,
+``(u.m**-3).find_equivalent_units()`` properly finds nothing, rather than all
+units of length.


Test patch:
diff --git a/astropy/units/tests/test_equivalencies.py b/astropy/units/tests/test_equivalencies.py
index 07336b803e..6be5b2857e 100644
--- a/astropy/units/tests/test_equivalencies.py
+++ b/astropy/units/tests/test_equivalencies.py
@@ -14,6 +14,16 @@ from astropy.tests.helper import assert_quantity_allclose
 from astropy.units.equivalencies import Equivalency
 
 
+def test_find_equivalent_units():
+    # Only finds units in the namespace
+    e1 = (u.m**3).find_equivalent_units()
+    assert len(e1) == 1
+    assert e1[0] == u.l
+    # Regression test for gh-16124
+    e2 = (u.m**-3).find_equivalent_units()
+    assert len(e2) == 0
+
+
 def test_dimensionless_angles():
     # test that the angles_dimensionless option allows one to change
     # by any order in radian in the unit (#1161)