Commit e740529d authored by Nick Felt's avatar Nick Felt Committed by TensorFlower Gardener
Browse files

Ensure tf.linspace(start, stop, num)[-1] == stop

The contract of tf.linspace says "A sequence of num evenly-spaced values are generated beginning at start. If num > 1, the values in the sequence increase by stop - start / num - 1, so that the last one is exactly stop."

But the existing logic computes the last value as `start + (num - 1) * ((start - stop) / (num - 1))`.  This exactly equals `stop` for real numbers, but not for floating point numbers.  In particular, for start = 0.0, stop = 1.0, num = 42, this reduces to (1.0/41.0)*41.0, and for IEEE 754 32-bit floats, that resolves to ~0.99999994, not 1.0 (binary values 0x3F7FFFFF vs 0x3F800000).

After this change, `tf.linspace(start, stop, num)[-1] == stop` holds for all values assuming num > 1, because we just set it directly to `stop`.  This matches what numpy does here: https://github.com/numpy/numpy/blob/v1.15.0/numpy/core/function_base.py#L144-L145

PiperOrigin-RevId: 228204335
parent 2325650d
Loading
Loading
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment