OT Sine Rule [was Re: Functional programming]
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Tue Mar 4 15:25:15 CET 2014
On Wed, 05 Mar 2014 00:01:01 +1100, Chris Angelico wrote:
> On Tue, Mar 4, 2014 at 10:47 PM, Steven D'Aprano
> <steve+comp.lang.python at pearwood.info> wrote:
>> Not even close. I'd like to see the compiler that can work out for
>> itself that this function is buggy:
>>
>> def sine_rule(side_a, side_b, angle_a):
>> """Return the angle opposite side_b.""" return
>> math.sin(side_b/side_a)*angle_a
>>
>>
>> If you don't remember your Sine Rule from trigonometry, that's okay.
>> Trust me, the function is badly wrong. It's rubbish really.
>
> I'm not entirely sure what it's trying to do,
The Sine Rule applies to any triangle, right-angled or not. I'm not going
to try to draw an ASCII-art triangle here, so you just have to imagine
one. Label the three sides "a", "b" and "c". Now label the angle opposite
each side as "A", "B" and "C", so angle A is opposite side a, and so
forth. The Sine Rule, or Law of Sines, tells us that the ratio of the
length of a side and the sine of the angle opposite that side is constant
for any triangle. That is:
a/sin(A) == b/sin(B) == c/sin(C)
Given any two sides and one corresponding angle, let's say a, b and A,
you can calculate the other angle B. The correct formula would be:
B = asin( sin(A)*b/a )
which is not what my bogus function does.
> but you're taking the sine
> of a ratio. That... seems wrong, gut-feeling-wise.
Well of course it's wrong. Given the intended geometric meanings of the
function parameters, the quantity calculated is meaningless. But
mathematically, it's okay to calculate the sine of a ratio of two other
quantities. It's just a number. These even a whole lot of double-angle
formulae that allow you to calculate sin(2*x) given sin(x), or sin(x/2)
given sin(x), etc.
But, giving that ratio physical or geometric meaning is another story,
and in this case the semantics of the function is entirely bogus. That
was my point -- the compiler cannot possibly tell what the function is
intended to do, it can only check that it is self-consistent.
> You take the sine of
> an angle and get a ratio, or the arcsine of a ratio and get an angle.
> Also, trig functions apply only to right triangles,
Not quite. Or to put it another way, not at all :-)
Trig functions are *defined* in terms of right-angled triangles, but
(say) the sine of 30° is 0.5 regardless of whether that 30° angle is in a
right-angled triangle, an acute triangle or an obtuse triangle. 30° is
30° regardless of what the rest of the triangle is doing.
Ask-me-about-versine-and-haversine-ly y'rs,
--
Steven D'Aprano
http://import-that.dreamwidth.org/
More information about the Python-list
mailing list