Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.022 X-Spam-Evidence: '*H*': 0.96; '*S*': 0.00; 'line:': 0.07; 'length)': 0.09; 'subject:python': 0.11; 'portion': 0.13; '(x,': 0.16; '*never*': 0.16; 'angle': 0.16; 'coordinates': 0.16; 'language)': 0.16; 'multiples': 0.16; 'purposes?': 0.16; 'string': 0.17; 'wrote:': 0.17; '(or': 0.18; 'written': 0.20; 'trying': 0.21; 'subject:problem': 0.22; 'programming': 0.23; 'header:In-Reply- To:1': 0.25; 'header:User-Agent:1': 0.26; 'values': 0.26; 'am,': 0.27; 'gary': 0.29; 'point': 0.31; 'skip:- 10': 0.32; 'getting': 0.33; 'zero': 0.33; 'to:addr:python-list': 0.33; 'skip:. 20': 0.35; 'expected': 0.35; '(i.e.': 0.36; 'should': 0.36; 'enough': 0.36; 'why': 0.37; 'subject:: ': 0.38; 'skip:( 30': 0.38; 'received:10': 0.38; 'science': 0.38; 'instead': 0.39; 'to:addr:python.org': 0.39; 'end': 0.40; 'your': 0.60; 'close': 0.63; 'dr.': 0.72; 'institute': 0.72; 'received:204': 0.72; 'received:10.10': 0.75; '180': 0.84; 'calculations': 0.84 Date: Fri, 15 Feb 2013 11:47:33 -0800 From: Gary Herron User-Agent: Mozilla/5.0 (X11; Linux i686; rv:17.0) Gecko/20130106 Thunderbird/17.0.2 MIME-Version: 1.0 To: python-list@python.org Subject: Re: python math problem References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 35 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1360957660 news.xs4all.nl 6874 [2001:888:2000:d::a6]:36987 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:38954 On 02/15/2013 11:39 AM, Kene Meniru wrote: > I am trying to calculate the coordinates at the end of a line. The length > and angle of the line are given and I am using the following formula: > > x = (math.sin(math.radians(angle)) * length) > y = (math.cos(math.radians(angle)) * length) > > The following are sample answers in the format (x, y) to the given > length/angle values of the line: > > 120/0 = (0.0, 25.0) > 120/89 = (24.9961923789, 0.436310160932) > 120/90 = (25.0, 1.53075794228e-15) > 120/91 = (24.9961923789, -0.436310160932) > > Why am I getting a string number instead of the expected answer for 120/90 > which should be (25.0, 0.0). This happens at all multiples of 90 (i.e. 180 > and 270) > Floating point calculations on a computer (ANY computer, and ANY programming language) can *never* be expected to be exact! (Think about 1/3 , PI, and sqrt(2) for instance.) The values written out as 1.53075794228e-15 is the (scientific notation) representation of .00000000000000153075794228. Is that not close enough to zero for your purposes? (Or is it that you don't understand the 'e-15' portion of the output?) -- Dr. Gary Herron Department of Computer Science DigiPen Institute of Technology (425) 895-4418