#1716·Gymnasium

[Bug Report] DiscretizeObservation collapses narrow Box ranges into the first bin

Author: HaokaiDingCreated Sep 16, 2026Updated Sep 17, 2026

Describe the bug

DiscretizeObservation clips observations to high - 1e-8. For a valid finite Box(0, 1e-8, shape=(1,)), this maps every observation to bin 0, despite requesting four bins. The behavior reproduces with both float32 and float64, and with Discrete and MultiDiscrete output.

Code example

python
import gymnasium as gym
import numpy as np
from gymnasium.spaces import Box, Discrete
from gymnasium.wrappers import DiscretizeObservation

env = gym.Env()
env.observation_space = Box(0, 1e-8, shape=(1,), dtype=np.float64)
env.action_space = Discrete(1)
wrapped = DiscretizeObservation(env, bins=4)
values = np.linspace(0, 1e-8, 5)
result = [wrapped.observation(np.array([value])) for value in values]
print(result)
assert result == [0, 1, 2, 3, 3]

Actual output on main: [0, 0, 0, 0, 0], followed by an assertion failure.

Expected: [0, 1, 2, 3, 3], covering all four bins while keeping the upper bound in the last bin.

System info

  • Gymnasium source checkout of main, installed editable; reports version 1.4.0.
  • macOS 26.6.2 ARM64, Python 3.13.15, NumPy 2.5.3.

Additional context

The bin edges already exclude both endpoints (linspace(...)[1:-1]), so digitize returns at most bins - 1. Clipping to high directly removes the scale-dependent error without producing an out-of-range index. I have a small fix and regression tests covering both dtypes/output modes, the endpoints, bin centers, and a normal-width control.

Checklist

  • I have checked that there is no similar issue in the repo. #1691 addressed multidimensional inputs and retained this clipping margin.

Source: Farama-Foundation/Gymnasium