Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #38954

Re: python math problem

Date 2013-02-15 11:47 -0800
From Gary Herron <gherron@digipen.edu>
Subject Re: python math problem
References <kfm2t8$9ru$1@ger.gmane.org>
Newsgroups comp.lang.python
Message-ID <mailman.1841.1360957660.2939.python-list@python.org> (permalink)

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Re: python math problem Gary Herron <gherron@digipen.edu> - 2013-02-15 11:47 -0800
  Re: python math problem Nobody <nobody@nowhere.com> - 2013-02-16 13:55 +0000
    Re: python math problem Chris Angelico <rosuav@gmail.com> - 2013-02-17 01:30 +1100
    Re: python math problem Roy Smith <roy@panix.com> - 2013-02-16 09:32 -0500
    Re: python math problem Dave Angel <davea@davea.name> - 2013-02-16 13:24 -0500

csiph-web