Repository: dask/dask

Issue:
Rechunking an array of specic shape with dtype float32 using `chunks='auto'`  creates chunks of size 0
<!-- Please include a self-contained copy-pastable example that generates the issue if possible.

Please be concise with code posted. See guidelines below on how to provide a good bug report:

- Craft Minimal Bug Reports http://matthewrocklin.com/blog/work/2018/02/28/minimal-bug-reports
- Minimal Complete Verifiable Examples https://stackoverflow.com/help/mcve

Bug reports that follow these guidelines are easier to diagnose, and so are often handled much more quickly.
-->

**Describe the issue**:

When a specific array with dtype `float32` is rechunked a chunk of size 0 is created. This might lead to further problems downstream (e.g., when these resulting chunks are used to rechunk another array a zero division error is raised).

**Minimal Complete Verifiable Example**:

```python
import dask.array as da
import numpy as np


x = da.ones((48, 720, 1440), dtype=np.float32, chunks=((36, 12), (720,), (1440,)))

y = x.rechunk()

print(x.chunks)  # ((36, 12), (720,), (1440,))
print(y.chunks)  # ((0, 36, 12), (720,), (1440,))
```

**Anything else we need to know?**:

When another dtype (e.g., `float` or `int`) is used, we get the expected result `((21, 21, 6), (631, 89), (1264, 176))`. This also seems to depend on the shape, e.g.
- `(49, 720, 1440)` with chunks `((36, 11), (720,), (1440,))` gives `((0, 36, 11), (720,), (1440,))`
- `(48, 720, 1440)` with chunks `((24, 24), (720,), (1440,))` gives `((24, 24), (720,), (1440,))`

**Environment**:

- Dask version: 2024.12.1
- Python version: 3.13.1
- Operating System: Linux
- Install method (conda, pip, source): conda

Discussion:
github-actions[bot]: ## Unit Test Results
_See [test report](https://dask.github.io/dask/test_report.html) for an extended history of previous test failures. This is useful for diagnosing flaky tests._

     15 files  ±0       15 suites  ±0   3h 57m 20s ⏱️ -51s
 12 611 tests ±0   11 448 ✅ ±0   1 163 💤 ±0  0 ❌ ±0 
156 885 runs  ±0  140 231 ✅ +2  16 654 💤  - 2  0 ❌ ±0 

Results for commit 650b82ac. ± Comparison against base commit 16b986ef.

[test-results]:data:application/gzip;base64,H4sIABcCZmcC/03NTQ6DIBCG4asY1l0MMAy0l2kANSH1p0FYNb170Sq6fJ9JvvmwPgzdwh4NV7eGLTmks9ocbQrztDYKhELlmra7IM6Pfi7Z+xU5ojnxFd4bkqzW2zAUq0vPLsY57hLz9H9Nxqi96zSCkLziPk2k8LDL9NbXZT+PY0glGClwRlivwYAGbYjuzmkP0LUonPBSWk9aSPb9AbsvMAUaAQAA


Patch:
diff --git a/dask/array/core.py b/dask/array/core.py
index 7dfff2b0f..898f5a33e 100644
--- a/dask/array/core.py
+++ b/dask/array/core.py
@@ -3329,7 +3329,8 @@ def auto_chunks(chunks, shape, limit, dtype, previous_chunks=None):
                             new_chunk += c
                         else:
                             # We reach the boundary so start a new chunk
-                            dimension_result.append(new_chunk)
+                            if new_chunk > 0:
+                                dimension_result.append(new_chunk)
                             new_chunk = c
                     if new_chunk > 0:
                         dimension_result.append(new_chunk)


Test patch:
diff --git a/dask/array/tests/test_array_core.py b/dask/array/tests/test_array_core.py
index 228dbccb9..de1f1ee29 100644
--- a/dask/array/tests/test_array_core.py
+++ b/dask/array/tests/test_array_core.py
@@ -3077,6 +3077,15 @@ def test_align_chunks_to_previous_chunks():
     assert all(c % 512 == 0 for c in chunks[1][:-1])
     assert all(c % 512 == 0 for c in chunks[2][:-1])
 
+    chunks = normalize_chunks(
+        "auto",
+        shape=(48, 720, 1440),
+        previous_chunks=((36, 12), (720,), (1440,)),
+        limit=134217728,
+        dtype=np.float32,
+    )
+    assert chunks == ((36, 12), (720,), (1440,))
+
 
 def test_raise_on_no_chunks():
     x = da.ones(6, chunks=3)