Skip to content

Tensorflow float32 conversion bug from python to tensorflow constants #45596

Closed

Description

@JohanMeppelinkDRA

Tensorflow changes values well within tf.float32 range to nearby values.

Is this expected behaviour? Numpy doesn't have this issue and given the values are within float32 range I'm not sure why this is happening.

System information

  • Have I written custom code (as opposed to using a stock example script provided in TensorFlow): No
  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Windows 10, same issue on linux though
  • Mobile device (e.g. iPhone 8, Pixel 2, Samsung Galaxy) if the issue happens on mobile device: No
  • TensorFlow installed from (source or binary): No
  • TensorFlow version (use command below): version 2.1.0
  • Python version: 3.6.8
  • Bazel version (if compiling from source): Not build from source
  • GCC/Compiler version (if compiling from source): Not build from source
  • CUDA/cuDNN version: running on cpu
  • GPU model and memory: ****

You can collect some of this information using our environment capture
script
You can also obtain the TensorFlow version with:

  1. TF 1.0: python -c "import tensorflow as tf; print(tf.GIT_VERSION, tf.VERSION)"
  2. TF 2.0: python -c "import tensorflow as tf; print(tf.version.GIT_VERSION, tf.version.VERSION)"

**
For example
tf.constant(20200131, dtype=tf.float32)
Becomes <tf.Tensor: shape=(), dtype=float32, numpy=20200132.0>

Why the extra 1?**

Expected behaviour:
**
tf.constant(20200131, dtype=tf.float32)
Becomes <tf.Tensor: shape=(), dtype=float32, numpy=20200131.0>
**

Standalone code to reproduce the issue
import tensorflow as tf
tf.constant(20200131, dtype=tf.float32)

Activity

  1. aaronmondal commented on Dec 12, 2020

    @aaronmondal
    Contributor

    In fact, numpy does have the same behaviour:

    >>> import numpy as np
    >>> np.full([], 20200131, dtype=np.float32)
    array(20200132., dtype=float32)

    It's just that numpy defaults to float64, which is why you do not notice it when invoking without specifying dtype:

    >>> a = np.full([], 20200131)
    a = np.full([], 20200131)
    >>> a
    array(20200131)
    >>> a.dtype
    dtype('int64')

    Float32 rounds to even numbers for large values. See also this interesting Wikipedia page:

    Integers between 224=16777216 and 225=33554432 round to a multiple of 2 (even number)

  2. google-ml-butler commented on Dec 12, 2020

    @google-ml-butler

    Are you satisfied with the resolution of your issue?
    Yes
    No

  3. added 20 commits that reference this issue on Jul 16, 2026
  4. added 3 commits that reference this issue on Jul 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

TF 2.1for tracking issues in 2.1 releasetype:bugBug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions