# Problem with my for-loop

This post is a wiki. Anyone with karma >750 is welcome to improve it.

Does someone know how to get answer to this ?

list = [x for x in range (100) if (x + 0.75) == 20]

I was experimenting with Sage when computer says that there isn´t possible answer [] to this elementary maths problem .

There is any problem with using decimals number in a list?

I also try doing this : but it still doesn´t working

list = [float(x) for x in range (100) if (x + 0.75) == 20]

Thank you so much , Excuse my bad English! :))

edit retag close merge delete

What is the mathematical problem you are trying to solve? (Note that range(100) is the set of integers from 0 to 99 included as can be seen with

sage: range(100)
[0, 1, ..., 99]


)

( 2016-08-01 16:36:47 +0200 )edit

Thanks for answering Yes , the problem that Im trying to solve is too complex to explain . But If I execute this code , that is a simplificacion of the problem, I suppose to get a list of the numbers that satisfy the condition in a range from 1 to 100 .

In this particulary case the answer it is a list with one number ( 19,25 ) but sage says that there isn´t any solution.

list = [x for x  in range (100) if (x + 0.75) == 20]
list
[]


But if I execute another code with and answer that it isn´t a decimal number , Sage give me a a solution

list = [x for x  in range (100) if (x + 1) == 20]
list
[19]


With this explanation , I want to know if is possible to get a solution from the first code or if the for command doesn´t support decimals numbers.

Thank ...(more)

( 2016-08-01 17:57:25 +0200 )edit

As I said range (in Sage) is the set of integers between 0 and 99. It is different from the common sense of range. If you are looking for solutions of an equation that are real numbers, you indeed need something else like find_root mentioned in the other comments.

( 2016-08-02 03:56:54 +0200 )edit

Sort by » oldest newest most voted

Sage is correctly telling you that for no integer x in the set 0,1,2,3,...,99, the equation x+0.75==20 holds.

Beware that testing for exact equality with floats is a certain source for bugs, because rounding errors will cause computations that mathematically should result in the same answers, to result in different ones. For example:

sage: [x for x in range(100) if float(x)+float(2/3) == float(1+2/3)]
[]


(whereas, of course, x=1 would be a mathematical solution)

Note, by the way, that this behaviour is basically just python.

more

Thank you for answering I´m so impressed , If I ´m correct the better solution will be stop ussing Sage and try luck with Mathematica , MatLab... Do you know any other possibility to do this using Sage?

( 2016-08-01 23:24:52 +0200 )edit

ans=solve(x+0.75==20,x)
[a.rhs() for a in ans if a.rhs()>0 and a.rhs()<100]


or if there is only one solution,

find_root(x+0.75==20,0,100)

( 2016-08-01 23:41:57 +0200 )edit

Thanks I'm not be use to solve ecuations sage but this more effective than my proposition . Im interested , What do I do if I want solve something like this?

list = [x for x in range (100) if mpmath.isint(mpmath.li(x))]
[]


Some solutions that I get "Manually"

Li(1.969047...) = 1

Li(2.825187...) = 2

( 2016-08-02 00:24:35 +0200 )edit

Again, range(100) is essentially a shorthand for [0,1,...,99]. If you want to find values for which li(x) takes an integral value, one approach would be to fix an integer value n and solve the equation li(x)=n numerically. You can do that for multiple n. For instance:

sage: [(sage.numerical.optimize.find_root(li(x)-i,2,100),i) for i in [2..10]]
[(2.825187152005897, 2),
(4.045118486230981, 3),
(5.60927669305089, 4),
(7.480870261577476, 5),
(9.621873135298816, 6),
(11.99863924296339, 7),
(14.582903118076294, 8),
(17.35126822426327, 9),
(20.284365456596614, 10)]

( 2016-08-02 06:37:01 +0200 )edit

If I ´m correct the better solution will be stop ussing Sage and try luck with Mathematica , MatLab... Do you know any other possibility to do this using Sage?

The problems that arise from using floating point numbers to represent real numbers are not unique to sage. The same problems arise in Mathematica. In matlab it'll be even worse, because there are basically no alternatives available, and you're stuck with machine precision (in return you get to compute quite quickly with these numbers). The right solution is to either avoid using floating point (if possible/practical) and compute with rational/algebraic numbers exactly, or to use appropriate numerical techniques to deal with loss of precision problems (e.g., don't test for equality but test for closeness).

( 2016-08-02 06:43:29 +0200 )edit