Skip to content

Conversation

smichr
Copy link
Member

@smichr smichr commented Nov 5, 2023

References to other Issues or PRs

yet another attempt to fix #23731
alternate to #25865, #25864, #25856

Brief description of what is fixed or changed

>>> Float(0).is_integer  # same as master
True
>>> print(Float(1).is_integer)  # False in master
None
>>> Float(1.5).is_integer  # same as master
False

Other comments

There was a long discussion of whether Float should be classified as rational here. One major concern was that float/Float objects do not behave as Rationals in operations, e.g.

>>> S(1e-16) + S(2) == 2.0
True
>>> (x+y).n(subs={x:1e-16,y:2}) == 2.
True

Release Notes

  • core
    • Float.is_integer is now None except for 0.0 and Floats with a non-zero fraction. Previously, only Float(0) was True
    • Fixed Eq incorrectly evaluating to False in some cases with floats.

@sympy-bot
Copy link

sympy-bot commented Nov 5, 2023

Hi, I am the SymPy bot. I'm here to help you write a release notes entry. Please read the guide on how to write release notes.

Your release notes are in good order.

Here is what the release notes will look like:

  • core
    • Float.is_integer is now None except for 0.0 and Floats with a non-zero fraction. Previously, only Float(0) was True (#25875 by @smichr)

    • Fixed Eq incorrectly evaluating to False in some cases with floats. (#25875 by @smichr)

This will be added to https://github.com/sympy/sympy/wiki/Release-Notes-for-1.13.

Click here to see the pull request description that was parsed.
<!-- Your title above should be a short description of what
was changed. Do not include the issue number in the title. -->

#### References to other Issues or PRs
<!-- If this pull request fixes an issue, write "Fixes #NNNN" in that exact
format, e.g. "Fixes #1234" (see
https://tinyurl.com/auto-closing for more information). Also, please
write a comment on that issue linking back to this pull request once it is
open. -->

yet another attempt to fix #23731
alternate to #25865, #25864, #25856

#### Brief description of what is fixed or changed

```python
>>> Float(0).is_integer  # same as master
True
>>> print(Float(1).is_integer)  # False in master
None
>>> Float(1.5).is_integer  # same as master
False
```

#### Other comments

There was a long discussion of whether `Float` should be classified as `rational` [here](https://groups.google.com/g/sympy/c/RUYOxxwMgac/m/4-1JXtny32EJ). One major concern was that float/Float objects do not behave as Rationals in operations, e.g.
```python
>>> S(1e-16) + S(2) == 2.0
True
>>> (x+y).n(subs={x:1e-16,y:2}) == 2.
True
```

#### Release Notes

<!-- Write the release notes for this release below between the BEGIN and END
statements. The basic format is a bulleted list with the name of the subpackage
and the release note for this PR. For example:

* solvers
  * Added a new solver for logarithmic equations.

* functions
  * Fixed a bug with log of integers. Formerly, `log(-x)` incorrectly gave `-log(x)`.

* physics.units
  * Corrected a semantical error in the conversion between volt and statvolt which
    reported the volt as being larger than the statvolt.

or if no release note(s) should be included use:

NO ENTRY

See https://github.com/sympy/sympy/wiki/Writing-Release-Notes for more
information on how to write release notes. The bot will check your release
notes automatically to see if they are formatted correctly. -->

<!-- BEGIN RELEASE NOTES -->
- core
  - `Float.is_integer` is now `None` except for `0.0` and `Floats` with a non-zero fraction. Previously, only `Float(0)` was True
  - Fixed Eq incorrectly evaluating to `False` in some cases with floats.
<!-- END RELEASE NOTES -->

Update

The release notes on the wiki have been updated.

Copy link

github-actions bot commented Nov 5, 2023

Benchmark results from GitHub Actions

Lower numbers are good, higher numbers are bad. A ratio less than 1
means a speed up and greater than 1 means a slowdown. Green lines
beginning with + are slowdowns (the PR is slower then master or
master is slower than the previous release). Red lines beginning
with - are speedups.

Significantly changed benchmark results (PR vs master)

Significantly changed benchmark results (master vs previous release)

| Change   | Before [a00718ba]    | After [6f3767c2]    |   Ratio | Benchmark (Parameter)                                                |
|----------|----------------------|---------------------|---------|----------------------------------------------------------------------|
| -        | 69.2±1ms             | 45.6±0.5ms          |    0.66 | integrate.TimeIntegrationRisch02.time_doit(10)                       |
| +        | 17.7±0.2μs           | 30.5±0.2μs          |    1.73 | integrate.TimeIntegrationRisch03.time_doit(1)                        |
| -        | 5.44±0.02ms          | 2.89±0.02ms         |    0.53 | logic.LogicSuite.time_load_file                                      |
| -        | 71.6±0.4ms           | 28.3±0.2ms          |    0.4  | polys.TimeGCD_GaussInt.time_op(1, 'dense')                           |
| -        | 26.2±0.4ms           | 17.1±0.2ms          |    0.65 | polys.TimeGCD_GaussInt.time_op(1, 'expr')                            |
| -        | 72.9±0.3ms           | 28.6±0.2ms          |    0.39 | polys.TimeGCD_GaussInt.time_op(1, 'sparse')                          |
| -        | 253±1ms              | 125±0.4ms           |    0.49 | polys.TimeGCD_GaussInt.time_op(2, 'dense')                           |
| -        | 254±2ms              | 124±1ms             |    0.49 | polys.TimeGCD_GaussInt.time_op(2, 'sparse')                          |
| -        | 652±9ms              | 371±3ms             |    0.57 | polys.TimeGCD_GaussInt.time_op(3, 'dense')                           |
| -        | 658±5ms              | 373±3ms             |    0.57 | polys.TimeGCD_GaussInt.time_op(3, 'sparse')                          |
| -        | 490±6μs              | 283±2μs             |    0.58 | polys.TimeGCD_LinearDenseQuadraticGCD.time_op(1, 'dense')            |
| -        | 1.78±0.03ms          | 1.05±0ms            |    0.59 | polys.TimeGCD_LinearDenseQuadraticGCD.time_op(2, 'dense')            |
| -        | 5.82±0.03ms          | 3.07±0.05ms         |    0.53 | polys.TimeGCD_LinearDenseQuadraticGCD.time_op(3, 'dense')            |
| -        | 445±7μs              | 230±3μs             |    0.52 | polys.TimeGCD_QuadraticNonMonicGCD.time_op(1, 'dense')               |
| -        | 1.46±0.01ms          | 670±2μs             |    0.46 | polys.TimeGCD_QuadraticNonMonicGCD.time_op(2, 'dense')               |
| -        | 4.85±0.01ms          | 1.63±0.01ms         |    0.34 | polys.TimeGCD_QuadraticNonMonicGCD.time_op(3, 'dense')               |
| -        | 371±0.9μs            | 201±0.5μs           |    0.54 | polys.TimeGCD_SparseGCDHighDegree.time_op(1, 'dense')                |
| -        | 2.46±0.02ms          | 1.19±0.01ms         |    0.48 | polys.TimeGCD_SparseGCDHighDegree.time_op(3, 'dense')                |
| -        | 9.78±0.04ms          | 4.34±0.02ms         |    0.44 | polys.TimeGCD_SparseGCDHighDegree.time_op(5, 'dense')                |
| -        | 355±3μs              | 165±1μs             |    0.47 | polys.TimeGCD_SparseNonMonicQuadratic.time_op(1, 'dense')            |
| -        | 2.42±0.01ms          | 876±10μs            |    0.36 | polys.TimeGCD_SparseNonMonicQuadratic.time_op(3, 'dense')            |
| -        | 9.63±0.04ms          | 2.59±0.01ms         |    0.27 | polys.TimeGCD_SparseNonMonicQuadratic.time_op(5, 'dense')            |
| -        | 1.03±0.02ms          | 419±3μs             |    0.41 | polys.TimePREM_LinearDenseQuadraticGCD.time_op(3, 'dense')           |
| -        | 1.71±0.01ms          | 508±1μs             |    0.3  | polys.TimePREM_LinearDenseQuadraticGCD.time_op(3, 'sparse')          |
| -        | 5.77±0.05ms          | 1.77±0.01ms         |    0.31 | polys.TimePREM_LinearDenseQuadraticGCD.time_op(5, 'dense')           |
| -        | 8.38±0.08ms          | 1.53±0.01ms         |    0.18 | polys.TimePREM_LinearDenseQuadraticGCD.time_op(5, 'sparse')          |
| -        | 289±2μs              | 63.2±0.2μs          |    0.22 | polys.TimePREM_QuadraticNonMonicGCD.time_op(1, 'sparse')             |
| -        | 3.40±0.04ms          | 388±3μs             |    0.11 | polys.TimePREM_QuadraticNonMonicGCD.time_op(3, 'dense')              |
| -        | 3.88±0.02ms          | 281±2μs             |    0.07 | polys.TimePREM_QuadraticNonMonicGCD.time_op(3, 'sparse')             |
| -        | 6.73±0.05ms          | 1.25±0.01ms         |    0.19 | polys.TimePREM_QuadraticNonMonicGCD.time_op(5, 'dense')              |
| -        | 8.60±0.1ms           | 835±1μs             |    0.1  | polys.TimePREM_QuadraticNonMonicGCD.time_op(5, 'sparse')             |
| -        | 5.13±0.06ms          | 2.99±0.01ms         |    0.58 | polys.TimeSUBRESULTANTS_LinearDenseQuadraticGCD.time_op(2, 'sparse') |
| -        | 11.9±0.08ms          | 6.43±0.03ms         |    0.54 | polys.TimeSUBRESULTANTS_LinearDenseQuadraticGCD.time_op(3, 'dense')  |
| -        | 22.4±0.08ms          | 9.18±0.06ms         |    0.41 | polys.TimeSUBRESULTANTS_LinearDenseQuadraticGCD.time_op(3, 'sparse') |
| -        | 5.33±0.02ms          | 872±4μs             |    0.16 | polys.TimeSUBRESULTANTS_QuadraticNonMonicGCD.time_op(1, 'sparse')    |
| -        | 12.8±0.07ms          | 7.04±0.03ms         |    0.55 | polys.TimeSUBRESULTANTS_QuadraticNonMonicGCD.time_op(2, 'sparse')    |
| -        | 100±1ms              | 25.4±0.05ms         |    0.25 | polys.TimeSUBRESULTANTS_QuadraticNonMonicGCD.time_op(3, 'dense')     |
| -        | 166±0.6ms            | 54.9±0.2ms          |    0.33 | polys.TimeSUBRESULTANTS_QuadraticNonMonicGCD.time_op(3, 'sparse')    |
| -        | 170±0.7μs            | 111±0.4μs           |    0.65 | polys.TimeSUBRESULTANTS_SparseGCDHighDegree.time_op(1, 'dense')      |
| -        | 360±2μs              | 214±0.7μs           |    0.59 | polys.TimeSUBRESULTANTS_SparseGCDHighDegree.time_op(1, 'sparse')     |
| -        | 4.18±0.01ms          | 830±4μs             |    0.2  | polys.TimeSUBRESULTANTS_SparseGCDHighDegree.time_op(3, 'dense')      |
| -        | 5.31±0.1ms           | 384±1μs             |    0.07 | polys.TimeSUBRESULTANTS_SparseGCDHighDegree.time_op(3, 'sparse')     |
| -        | 19.6±0.06ms          | 2.81±0.01ms         |    0.14 | polys.TimeSUBRESULTANTS_SparseGCDHighDegree.time_op(5, 'dense')      |
| -        | 22.3±0.1ms           | 629±3μs             |    0.03 | polys.TimeSUBRESULTANTS_SparseGCDHighDegree.time_op(5, 'sparse')     |
| -        | 489±4μs              | 134±0.7μs           |    0.27 | polys.TimeSUBRESULTANTS_SparseNonMonicQuadratic.time_op(1, 'sparse') |
| -        | 4.56±0.04ms          | 609±2μs             |    0.13 | polys.TimeSUBRESULTANTS_SparseNonMonicQuadratic.time_op(3, 'dense')  |
| -        | 5.29±0.02ms          | 137±0.9μs           |    0.03 | polys.TimeSUBRESULTANTS_SparseNonMonicQuadratic.time_op(3, 'sparse') |
| -        | 12.7±0.07ms          | 1.30±0.01ms         |    0.1  | polys.TimeSUBRESULTANTS_SparseNonMonicQuadratic.time_op(5, 'dense')  |
| -        | 13.8±0.04ms          | 139±1μs             |    0.01 | polys.TimeSUBRESULTANTS_SparseNonMonicQuadratic.time_op(5, 'sparse') |
| -        | 133±0.4μs            | 75.2±1μs            |    0.56 | solve.TimeMatrixOperations.time_rref(3, 0)                           |
| -        | 250±0.9μs            | 87.9±0.4μs          |    0.35 | solve.TimeMatrixOperations.time_rref(4, 0)                           |
| -        | 24.1±0.06ms          | 10.1±0.04ms         |    0.42 | solve.TimeSolveLinSys189x49.time_solve_lin_sys                       |

Full benchmark results can be found as artifacts in GitHub Actions
(click on checks at the top of the PR).

@asmeurer
Copy link
Member

asmeurer commented Nov 6, 2023

The point should also be made that Float(1).is_integer = None does not preclude making it True in the future, since None means "True or False". So I think this is the safest fix, and it matches what we decided in the past for Float.is_rational = False.

I don't know if making Float.is_integer = False was done intentionally or accidentally, but it's clearly wrong given the Eq behavior, and that sort of thing could happen anywhere in the code that uses an is_integer check.

@smichr smichr enabled auto-merge November 6, 2023 15:34
@smichr smichr merged commit 72c0253 into sympy:master Nov 6, 2023
@smichr smichr deleted the 23731 branch November 6, 2023 16:36
@asmeurer
Copy link
Member

asmeurer commented Nov 6, 2023

Thanks for picking up this work @smichr

For future readers, if we ever in the future decide that making Float(1).is_integer = True (or even Float.is_rational = True in general) we can revisit that. I think it could be dangerous, since floats don't actually satisfy the same mathematical properties as integers and rationals, although I'm not completely sure yet just how much that would matter in practice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

is_zero broken for integer/float comparison
3 participants