1

i'm developing a simple application to help my math teacher to teach about trigonometry. The application is nothing but a circle where the angle keeps spinning and shows the variation of the Sin and Cos. Simple as hell.

This is what i have so far:

enter image description here

And this is what i want to achieve

enter image description here

(notice the green part representing the cos).

Everything is being built through CSS. I needed to calculate the height and the width of the green triangle so i could display it, i tought it would be as simple as calculating the sin and the cos and just multiplying it by 200(my circle radius in px). But for some reason js is giving me inaccurate values for the sin and cos. For example, it gives right values if my angle is 0(cos: 1, sen: 0). But if i use an angle like 90, it will not return the right values, as shows the screenshot below:

enter image description here

This is my code so far(reduced for readability)

function trig()
{
    [...]
    this.radianMultiplier = Math.PI / 180;

    this.setDegree = function(degree)
    {
        [...]

        $('#h').html( Math.sin(degree * this.radianMultiplier) );
        $('#w').html( Math.cos(degree * this.radianMultiplier) );
    }
}

Any thoughts on what might be happening?

Leonardo Arroyo
  • 573
  • 6
  • 16
  • 2
    The screenshot is unreadable, can you just show the numbers? – Barmar Apr 06 '13 at 07:12
  • 1
    How is the triangle being rendered? What are the invalid height and width values being returned? – Aiias Apr 06 '13 at 07:12
  • @Barmar To angle 0 (sin: 0, cos: 1). To angle 90 (sin: 1, cos: 6.123031769111886e-17). – Leonardo Arroyo Apr 06 '13 at 07:15
  • 4
    That's just floating point imprecision. See [Is JavaScript's Floating-Point Math Broken?](http://stackoverflow.com/questions/588004/is-javascripts-floating-point-math-broken) – JJJ Apr 06 '13 at 07:16
  • @Aiias Same as above(To angle 0 (sin: 0, cos: 1). To angle 90 (sin: 1, cos: 6.123031769111886e-17)). I'm not rendering the triangle yet, just trying to figure out why i get incorrect values from the sin/cos function. The problem can be reproduced on Opera, Chrome and IE(don't have Firefox right now to try it out). – Leonardo Arroyo Apr 06 '13 at 07:16
  • 4
    `6.123031769111886e-17` is very close to `0`. Is there a particular reason why you would stray from rounding, say 4 decimal places? – Aiias Apr 06 '13 at 07:19
  • @BillyMoon I tried it in Chrome and I get the same 6e-17 he got. – Barmar Apr 06 '13 at 07:20
  • Alright, i'm sorry for the double posting. Marked as duplicate so it can be closed. – Leonardo Arroyo Apr 06 '13 at 07:25

0 Answers0