Repository: sympy/sympy

Issue:
Issue with sympy.physics.quantum.cg.CG returning incorrect values
I have encountered an issue where the `sympy.physics.quantum.cg.CG` function and the `py3nj.clebsch_gordan` function return different results for the same inputs. 
Here is a minimal reproducible example:
```python3
import sympy.physics.quantum.cg as spy
import py3nj

def CGsmpy(j1, m1, j2, m2, j3, m3):
    return float(spy.CG(j1, m1, j2, m2, j3, m3).doit())

def CGpy3nj(j1, m1, j2, m2, j3, m3):
    j1_, m1_, j2_, m2_, j3_, m3_ = int(2*j1), int(2*m1), int(2*j2), int(2*m2), int(2*j3), int(2*m3)
    try:
        return py3nj.clebsch_gordan(j1_, j2_, j3_, m1_, m2_, m3_)
    except ValueError:
        return 0.0

sR = 0.5
print("Calculating Clebsch-Gordan coefficients... \n")
for l in range(14):
    for j in range(14):
        for mi in range(-1, 2):
            for mip in range(-1, 2):
                for mR in range(1, 3, 2):
                    for mRp in range(-1, 3, 2):
                        j1, m1, j2, m2, j3, m3 = l, mi, sR, mR*0.5, j * 0.5, mi + mR*0.5
                        cg_sympy = CGsmpy(j1, m1, j2, m2, j3, m3)
                        cg_py3nj = CGpy3nj(j1, m1, j2, m2, j3, m3)
                        if abs(cg_sympy - cg_py3nj) > 1e-10:
                            print("j1, m1, j2, m2, j3, m3: ", l, mi, sR, mR*0.5, j * 0.5, mi + mR*0.5)
                            print("sympy: ", cg_sympy)
                            print("py3nj: ", cg_py3nj)
                            print("--------------------")
```

For some inputs, such as j1, m1, j2, m2, j3, m3: 4 -1 0.5 0.5 4.0 -0.5, the sympy function returns 0.474341649025257 while the py3nj function returns -0.0. I checked with other libraries too (and Mathematica) and it seems like `sympy` returns wrong values. 

The `python` and `sympy` versions are:
```python3
Python version
3.10.13 (main, Sep 11 2023, 13:44:35) [GCC 11.2.0]
Version info.
sys.version_info(major=3, minor=10, micro=13, releaselevel='final', serial=0)
Sympy version
1.12
```

Could you please look into this?

Discussion:

Patch:
diff --git a/sympy/physics/wigner.py b/sympy/physics/wigner.py
index adc198e036..5e67682b9f 100644
--- a/sympy/physics/wigner.py
+++ b/sympy/physics/wigner.py
@@ -197,7 +197,9 @@ def wigner_3j(j_1, j_2, j_3, m_1, m_2, m_3):
     - zero for `m_1 + m_2 + m_3 \neq 0`
 
     - zero for violating any one of the conditions
-      `j_1 \ge |m_1|`,  `j_2 \ge |m_2|`,  `j_3 \ge |m_3|`
+         `m_1  \in \{-|j_1|, \ldots, |j_1|\}`,
+         `m_2  \in \{-|j_2|, \ldots, |j_2|\}`,
+         `m_3  \in \{-|j_3|, \ldots, |j_3|\}`
 
     Algorithm
     =========
@@ -230,6 +232,10 @@ def wigner_3j(j_1, j_2, j_3, m_1, m_2, m_3):
         return S.Zero
     if (abs(m_1) > j_1) or (abs(m_2) > j_2) or (abs(m_3) > j_3):
         return S.Zero
+    if not (int_valued(j_1 - m_1) and \
+            int_valued(j_2 - m_2) and \
+            int_valued(j_3 - m_3)):
+        return S.Zero
 
     maxfact = max(j_1 + j_2 + j_3 + 1, j_1 + abs(m_1), j_2 + abs(m_2),
                   j_3 + abs(m_3))


Test patch:
diff --git a/sympy/physics/quantum/tests/test_cg.py b/sympy/physics/quantum/tests/test_cg.py
index e5ff16265f..384512aaac 100644
--- a/sympy/physics/quantum/tests/test_cg.py
+++ b/sympy/physics/quantum/tests/test_cg.py
@@ -178,3 +178,6 @@ def test_doit():
     assert Wigner9j(
         2, 1, 1, Rational(3, 2), S.Half, 1, S.Half, S.Half, 0).doit() == sqrt(2)/12
     assert CG(S.Half, S.Half, S.Half, Rational(-1, 2), 1, 0).doit() == sqrt(2)/2
+    # J minus M is not integer
+    assert Wigner3j(1, -1, S.Half, S.Half, 1, S.Half).doit() == 0
+    assert CG(4, -1, S.Half, S.Half, 4, Rational(-1, 2)).doit() == 0