Skip to content

tf.range have accumulate floating point error on CPU #58133

@fuzzyswan

Description

@fuzzyswan
Click to expand!

Issue Type

Bug

Source

binary

Tensorflow Version

tf 2.10.0

Custom Code

Yes

OS Platform and Distribution

Linux Ubuntu 20.04

Mobile device

No response

Python version

No response

Bazel version

No response

GCC/Compiler version

No response

CUDA/cuDNN version

No response

GPU model and memory

No response

Current Behaviour?

tf.range have accumulate greater and greater floating point error on CPU. In the example below, range(- 10, 10, 0.01) gives [... 9.980267 9.990267] when the correct result should be [... 9.98 9.99], and this deviation is too much in my opinion.

Standalone code to reproduce the issue

import tensorflow as tf
with tf.device('cpu'):
    tensor1 = tf.range(- 10, 10, 0.01) # [-10., -9.99 ... 9.980267 9.990267]
    print(tensor1)
with tf.device('gpu'):
    tensor2 = tf.range(- 10, 10, 0.01) # [-10., -9.99 ... 9.98 9.99]
    print(tensor2)
assert np.allclose(tensor1, tensor2) # AssertionError

Relevant log output

tf.Tensor([-10.        -9.99      -9.98     ...   9.970266   9.980267   9.990267], shape=(2000,), dtype=float32)
tf.Tensor([-10.        -9.99      -9.98     ...   9.969999   9.98       9.99    ], shape=(2000,), dtype=float32)
AssertionError

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions