Ask Your Question
0

Problem with my for-loop

asked 2016-07-31 08:48:47 -0500

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 flag offensive close merge delete

1 answer

Sort by » oldest newest most voted
1

answered 2016-08-01 13:58:25 -0500

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2016-07-31 08:48:47 -0500

Seen: 87 times

Last updated: Aug 01 '16
{"cc_re__cc_re__cc_by_sa_3_0_le": ["rror-mixing-exact-numbers-and-decimal-ones/\">Memory error mixing exact numbers and decimal ones</a> </p> <p> <a href=\"/question/34671/what-is-the-equivalent-of-cin-in-sage/\">what is the equivalent of cin&gt;&gt; in sage</a> </p> <p> <a href=\"/question/41375/is-it-possible-to-speed-up-loop-iteration-in-sage/\">Is it possible to speed up loop iteration in Sage?</a> </p> </div> </div> </div> </div> <!-- template footer.html --> <div id=\"ground\"> <div class=\"content-wrapper\"> <div class=\"copyright\"> <a href=\"http://creativecommons.org/licenses/by-sa/3.0/legalcode\"> <img\n                            class=\"license-logo\"\n                            src=\"/m/default/media/images/cc-by-sa.png?v=19\"\n                            title=\"cc-by-sa\"\n                            alt=\"cc-by-sa\"\n                        /> </a>\n                \n                Copyright Sage, 2010. Some rights reserved under creative commons license. Content on this site is licensed under a <a href=\"http://creativecommons.org/licenses/by-sa/3.0/legalcode\">Creative Commons Attribution Share", "wrapper\"> <div class=\"copyright\"> <a href=\"http://creativecommons.org/licenses/by-sa/3.0/legalcode\"> <img\n                            class=\"license-logo\"\n                            src=\"/m/default/media/images/cc-by-sa.png?v=19\"\n                            title=\"cc-by-sa\"\n                            alt=\"cc-by-sa\"\n                        /> </a>\n                \n                Copyright Sage, 2010. Some rights reserved under creative commons license. Content on this site is licensed under a <a href=\"http://creativecommons.org/licenses/by-sa/3.0/legalcode\">Creative Commons Attribution Share Alike 3.0</a> license.\n\n            </div> <div class=\"footer-links\" > <a href=\"/about/\">about</a><span class=\"link-separator\"> |</span> <a href=\"/faq/\">faq</a><span class=\"link-separator\"> |</span> <a href=\"/help/\" title=\"help\">help</a> <span class=\"link-separator\"> |</span> <a href=\"/privacy/\">privacy policy</a><span class=\"link-separator\"> |</span> <a href=\"/terms/\">terms of service</a> </div> <div class=\"powered-link\"> <a href=\"http://askbot.com\" target=\"_b"], "copyright__copyright__copyright": ["nt ones. For example:\n\nsage: [x for x in range(100) if float(x)+float(2/3) == float(1+2/3)]\n[]\n\n(whereas, of course, x=1 would be a mathematical solution)\n\nNote, by the way, that this behaviour is basically just python.\n\nedit flag offensive delete link more\n\nYour Answer\n\nPlease start posting anonymously - your entry will be published after you log in or create a new account.\n\nAdd Answer\n\nQuestion Tools\n\n1 follower\n\nStats\n\nAsked: 2016-07-31 08:48:47 -0500\n\nSeen: 87 times\n\nLast updated: Aug 01 '16", "nt ones. For example:\n\nsage: [x for x in range(100) if float(x)+float(2/3) == float(1+2/3)]\n[]\n\n(whereas, of course, x=1 would be a mathematical solution)\n\nNote, by the way, that this behaviour is basically just python.\n\nedit flag offensive delete link more\n\nYour Answer\n\nPlease start posting anonymously - your entry will be published after you log in or create a new account.\n\nAdd Answer\n\nQuestion Tools\n\n1 follower\n\nStats\n\nAsked: 2016-07-31 08:48:47 -0500\n\nSeen: 87 times\n\nLast updated: Aug 01 '16"]}