Skip to content

Conversation

smichr
Copy link
Member

@smichr smichr commented Sep 6, 2023

References to other Issues or PRs

Brief description of what is fixed or changed

This adds a function that indicates, regardless of precision, whether an expression is a Number with no decimal portion, e.g. Float(3) but not Float(3.1).

Done in anticipation of #25614 but useful whether that is committed or not.

Other comments

I am note sure what to do about int_valued(1e23) -> True; it is true that it has no remainder but it is also not what one might expect:

>>> Float(1e23)
1.00000000000000e+23
>>> int(_)
99999999999999991611392
>>> Float(1e23)._mpf_
(0, 2980232238769531, 25, 52)  # sign, mantissa, e for 2**e and bits in mantissa
>>> Float(1e23)._prec
53

>>> Float('1e23')._mpf_
(0, 11920928955078125, 23, 54)
>>> int(Float('1e23'))
100000000000000000000000

>>> Float(10**5)
100000.000000000
>>> int(_)
100000
>>> Float(10**5)._mpf_
(0, 3125, 5, 12)
>>> Float(10**5)._prec
53

How do I determine whether a number will suffer from precision loss or not? It seems that the only way to know is to store, at instantiation, a flag that indicates how the number was passed (string vs float, for example).

Release Notes

  • core
    • added int_valued function to sympy/core/numbers.py for testing whether an expression is a literal number with no decimal portion

@sympy-bot
Copy link

sympy-bot commented Sep 6, 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
    • added int_valued function to sympy/core/numbers.py for testing whether an expression is a literal number with no decimal portion (#25640 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. -->


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

This adds a function that indicates, regardless of precision, whether an expression is a Number with no decimal portion, e.g. `Float(3)` but not `Float(3.1)`.

Done in anticipation of #25614 but useful whether that is committed or not.

#### Other comments

I am note sure what to do about `int_valued(1e23) -> True`; it is true that it has no remainder but it is also not what one might expect:
```python
>>> Float(1e23)
1.00000000000000e+23
>>> int(_)
99999999999999991611392
>>> Float(1e23)._mpf_
(0, 2980232238769531, 25, 52)  # sign, mantissa, e for 2**e and bits in mantissa
>>> Float(1e23)._prec
53

>>> Float('1e23')._mpf_
(0, 11920928955078125, 23, 54)
>>> int(Float('1e23'))
100000000000000000000000

>>> Float(10**5)
100000.000000000
>>> int(_)
100000
>>> Float(10**5)._mpf_
(0, 3125, 5, 12)
>>> Float(10**5)._prec
53
```

How do I determine whether a number will suffer from precision loss or not? It seems that the only way to know is to store, at instantiation, a flag that indicates how the number was passed (string vs float, for example).

#### 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
    * added `int_valued` function to sympy/core/numbers.py for testing whether an expression is a literal number with no decimal portion
<!-- END RELEASE NOTES -->

Update

The release notes on the wiki have been updated.

@smichr smichr force-pushed the isint branch 2 times, most recently from 7a20c49 to 370e355 Compare September 7, 2023 04:39
@smichr smichr force-pushed the isint branch 2 times, most recently from bbd7f94 to 60468c9 Compare September 8, 2023 01:21
@github-actions
Copy link

github-actions bot commented Sep 8, 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 [8059df73] <sympy-1.12^0>   | After [c84dc53e]    |   Ratio | Benchmark (Parameter)                                                |
|----------|------------------------------------|---------------------|---------|----------------------------------------------------------------------|
| -        | 84.0±1ms                           | 55.0±0.4ms          |    0.65 | integrate.TimeIntegrationRisch02.time_doit(10)                       |
| -        | 84.7±1ms                           | 53.7±0.5ms          |    0.63 | integrate.TimeIntegrationRisch02.time_doit_risch(10)                 |
| +        | 20.0±0.1μs                         | 39.7±0.2μs          |    1.99 | integrate.TimeIntegrationRisch03.time_doit(1)                        |
| -        | 6.80±0.01ms                        | 3.67±0.01ms         |    0.54 | logic.LogicSuite.time_load_file                                      |
| -        | 2.10±0ms                           | 615±3μs             |    0.29 | polys.TimePREM_LinearDenseQuadraticGCD.time_op(3, 'sparse')          |
| -        | 10.4±0.03ms                        | 1.86±0.01ms         |    0.18 | polys.TimePREM_LinearDenseQuadraticGCD.time_op(5, 'sparse')          |
| -        | 361±2μs                            | 78.9±0.4μs          |    0.22 | polys.TimePREM_QuadraticNonMonicGCD.time_op(1, 'sparse')             |
| -        | 4.70±0.01ms                        | 345±0.7μs           |    0.07 | polys.TimePREM_QuadraticNonMonicGCD.time_op(3, 'sparse')             |
| -        | 10.5±0.02ms                        | 1.04±0ms            |    0.1  | polys.TimePREM_QuadraticNonMonicGCD.time_op(5, 'sparse')             |
| -        | 6.21±0.02ms                        | 3.82±0.01ms         |    0.61 | polys.TimeSUBRESULTANTS_LinearDenseQuadraticGCD.time_op(2, 'sparse') |
| -        | 27.5±0.03ms                        | 11.6±0.03ms         |    0.42 | polys.TimeSUBRESULTANTS_LinearDenseQuadraticGCD.time_op(3, 'sparse') |
| -        | 6.77±0.04ms                        | 1.13±0ms            |    0.17 | polys.TimeSUBRESULTANTS_QuadraticNonMonicGCD.time_op(1, 'sparse')    |
| -        | 15.8±0.06ms                        | 8.91±0.01ms         |    0.57 | polys.TimeSUBRESULTANTS_QuadraticNonMonicGCD.time_op(2, 'sparse')    |
| -        | 205±0.3ms                          | 68.4±0.07ms         |    0.33 | polys.TimeSUBRESULTANTS_QuadraticNonMonicGCD.time_op(3, 'sparse')    |
| -        | 6.36±0.02ms                        | 504±2μs             |    0.08 | polys.TimeSUBRESULTANTS_SparseGCDHighDegree.time_op(3, 'sparse')     |
| -        | 27.3±0.08ms                        | 803±2μs             |    0.03 | polys.TimeSUBRESULTANTS_SparseGCDHighDegree.time_op(5, 'sparse')     |
| -        | 591±1μs                            | 201±0.9μs           |    0.34 | polys.TimeSUBRESULTANTS_SparseNonMonicQuadratic.time_op(1, 'sparse') |
| -        | 6.32±0.02ms                        | 205±0.7μs           |    0.03 | polys.TimeSUBRESULTANTS_SparseNonMonicQuadratic.time_op(3, 'sparse') |
| -        | 16.4±0.02ms                        | 207±0.6μs           |    0.01 | polys.TimeSUBRESULTANTS_SparseNonMonicQuadratic.time_op(5, 'sparse') |
| -        | 162±0.5μs                          | 90.5±0.2μs          |    0.56 | solve.TimeMatrixOperations.time_rref(3, 0)                           |
| -        | 306±0.5μs                          | 106±0.3μs           |    0.35 | solve.TimeMatrixOperations.time_rref(4, 0)                           |
| -        | 29.9±0.03ms                        | 12.7±0.04ms         |    0.43 | 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).

@smichr smichr changed the title add is_int property add int_valued function Sep 8, 2023
@oscarbenjamin
Copy link
Collaborator

Looks good!

@oscarbenjamin oscarbenjamin merged commit e6c67c4 into sympy:master Sep 8, 2023
@smichr smichr deleted the isint branch September 11, 2023 23:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants