Results 1 to 21 of 21

Thread: how does a calc do roots and trig functions?

  1. #1

    Thread Starter
    Addicted Member Flip's Avatar
    Join Date
    Jun 2002
    Location
    Burke, VA
    Posts
    247

    how does a calc do roots and trig functions?

    How does a calculator calculate square roots and stuff like sin(10)? Does it do it mathamaticaly?
    The "company" website My homepage: nerisoft.com
    scars heal but glory is forever

  2. #2
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256

    Re: how does a calc do roots and trig functions?

    Originally posted by Flip
    Does it do it mathamaticaly?
    How else can you do math?
    My evil laugh has a squeak in it.

    kristopherwilson.com

  3. #3
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134
    You can calculate a cube root mathematically (what I think you mean by that is explain one math 'function' by combining others?), did you know that?
    All contents of the above post that aren't somebody elses are mine, not the property of some media corporation.
    (Just a heads-up)

  4. #4
    Addicted Member
    Join Date
    Jul 2000
    Location
    California
    Posts
    154
    The Calculator uses approximation functions, in our second semester calculus class we derived approximations for cos(x),sin(x) and e^x
    the approximation functions are just regular polynomials, if I remember correctly.
    VB-World addict!

    All spelling errors are undocumented words!
    http://www.bells.f2s.com

  5. #5
    Fanatic Member bugzpodder's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    787
    Really! if they have an approximation of sinx to the 9th digit of an polynomial for all angles, i'd love to see that polynomial!!!

    anywayz i am not sure how a calculator does that, but it is possible to calculate sinx by using its taylor expansion.
    Last edited by bugzpodder; Oct 20th, 2002 at 11:13 AM.
    Massey RuleZ! ^-^__Cheers!__^-^ Massey RuleZ!


    Did you know that...
    The probability that a random rational number has an even denominator is 1/3 (Salamin and Gosper 1972)? This result is independently verified by me (2002)!

  6. #6
    Addicted Member
    Join Date
    Aug 2002
    Location
    London UK
    Posts
    255
    I think that's what he was referring to, but don't quote me on it.
    Not at all related to sheep...

  7. #7

    Thread Starter
    Addicted Member Flip's Avatar
    Join Date
    Jun 2002
    Location
    Burke, VA
    Posts
    247
    like for example, how does the calculator calculate the square root of three? Is there a pencil-and-paper method that the calculator follows?
    The "company" website My homepage: nerisoft.com
    scars heal but glory is forever

  8. #8
    Fanatic Member bugzpodder's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    787
    newton's square root approximation method would work. instead of sqrt(3) it can try to find a number x such that 3-x*x<0.00000001
    Massey RuleZ! ^-^__Cheers!__^-^ Massey RuleZ!


    Did you know that...
    The probability that a random rational number has an even denominator is 1/3 (Salamin and Gosper 1972)? This result is independently verified by me (2002)!

  9. #9
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573
    Calculators and computers user Taylor series. They have infinite terms but if the variable lies in a specific range of values they converge and usually they do very rapidly. For example, for the sinus you have:

    sin(x) = x - x3/3! + x5/5! - x7/7! + ...

    so the nth term is: (-1)n+1x(2n-1)/(2n-1)!

    This works for |x|<=? (I'm not sure if it's 1 or Pi/2, you'll have to check this out on a calculus book or insist to me that I do it myself as soon as I have one handy). If x is outside this range, then you can use a trigonometric formula such as:

    sin(a + b)=sin(a)cos(b) + sin(b)cos(a) where a and b meet the requirement (if they don't, keep splitting them and use the formula recursively)

  10. #10
    Addicted Member
    Join Date
    Jul 2000
    Location
    California
    Posts
    154
    it works for all x's but as x grows the number of terms in the series that we have to use to remain close to the actual sin value goes to infinity
    VB-World addict!

    All spelling errors are undocumented words!
    http://www.bells.f2s.com

  11. #11
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111
    If you're using a program, to do the 3rd root is the same as

    num ^ (1/3)

    4th

    num ^ (1/4)

    etc.

  12. #12
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573
    Originally posted by Bjwbell
    it works for all x's but as x grows the number of terms in the series that we have to use to remain close to the actual sin value goes to infinity
    It doesn't actually work for all x, for instance, if x = Pi the series does not converge. Though it works only for 0<=x<=Pi/2, it can still be used for other values of x outside this range, by resourcing to "tricks" i.e. properties such as

    sin(-x) = -sin(x), or sin(x + Pi/2) = cos(x), etc.

    Just very few terms are really necessary for a high accuracy, only bear in mind x must be radiants, not degrees. For example, for 90 deg. you have approx. x = Pi/2 = 1.5708 and the result using just 5 terms is 1.000003543, not so different from the actual value 1

  13. #13
    Addicted Member
    Join Date
    Jul 2000
    Location
    California
    Posts
    154
    it does converge for x=Pi
    try doing 10 taylor terms and evaluating it, it come out to 0.00679 which is pretty close to zero
    VB-World addict!

    All spelling errors are undocumented words!
    http://www.bells.f2s.com

  14. #14
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573
    Originally posted by Bjwbell
    it does converge for x=Pi
    try doing 10 taylor terms and evaluating it, it come out to 0.00679 which is pretty close to zero
    Yup...! I'll definitely have to take a new look into my calculus books...

    Btw here's an applet to show you folks how accurate those series can get...

    http://math.furman.edu/~dcs/java/taylor.html

  15. #15

    Thread Starter
    Addicted Member Flip's Avatar
    Join Date
    Jun 2002
    Location
    Burke, VA
    Posts
    247
    The website "http://" doesn't really have much on it...
    Repost the link or edit your post

    ADDED FROM LAST POST: Yeah, im a retard. Just copy and paste. I am so observant...
    Last edited by Flip; Oct 26th, 2002 at 06:19 PM.
    The "company" website My homepage: nerisoft.com
    scars heal but glory is forever

  16. #16
    Fanatic Member riis's Avatar
    Join Date
    Nov 2001
    Posts
    551
    The URL is displayed in the text. You can just copy and paste it if the liks doesn't work accidentally.

  17. #17
    Addicted Member
    Join Date
    Aug 2002
    Location
    Windsor, Ontario's City of Pollution
    Posts
    165
    There is a way to calculate square roots by hand, and it's kind of like a complicated form of division. Each time, two digits are dropped instead of one, and the previous quotient is multiplied by 20, while the new quotient is added to it. (Something like that)
    Merry Math Making!

  18. #18
    Frenzied Member
    Join Date
    Jul 1999
    Location
    Huntingdon Valley, PA 19006
    Posts
    1,151
    There is at least one thread in this forum that discusses the Newton method for solving all sorts of problems. Square root is a special case, which works as follows.

    Suppose you wanted the Square root of 100 and did not know that 10 was the answer. Take a wild guess, say 20.

    Then 100/20 = 5, and answer is between 5 & 20.

    Take average: (20 + 5) / 2 = 12.5, which is a lot better than 20.

    100/12.5 = 8

    Average: (12.5 + 8) / 2 = 10.25, we are getting closer.

    If you keep it up, you get the following.

    20
    12.5
    10.25
    10.003 048 781
    10.000 000 465
    10.000 000 000

    Note that when two successive values are almost equal, you know you are very close, even if you did not know what the final answer should be. If you started with a first guess of 100, it would only take a few more iterations to get an accurate result. A modern PC or calculator is so fast that a few extra iterations are not noticable. The first guess is not important.

    The methods used by calculators & computers use some simple algorithm for the first guess. The (number + 1)/2 is not a terrible first guess. It is a compromise catering to original numbers less than one and greater than one. Trying half or 10% of the number is better for big numbers, but worse for numbers less than one.

    Trig functions use power series as mentioned in a previous post. For an angle in radians (Pi * degrees / 180), Sine(x) is approximated by the following.

    x - x3/3! + x5/5! - x7/7! . . .

    3! = 3*2*1
    5! = 5*4*3*2*1
    et cetera.

    Tricks are used to avoid calculating for large values of x, which require many terms of the polynomial. Some tricks are as follows.

    For angles between 45 & 90, compute cosine of (90-angle).
    Compute both Sine & Cosine of 1/2 or 1/4 of the angle and use formula for twice the angle.
    Live long & prosper.

    The Dinosaur from prehistoric era prior to computers.

    Eschew obfuscation!
    If a billion people believe a foolish idea, it is still a foolish idea!
    VB.net 2010 Express
    64Bit & 32Bit Windows 7 & Windows XP. I run 4 operating systems on a single PC.

  19. #19
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    To calculate square roots we can iterate the following equation:

    x = x/2 + k/2x

    Solving the above U get x 2 = k

    In VB
    VB Code:
    1. Public Function SquareRoot(thenumber As Double) as Double
    2. Dim theVals() As Double
    3. ReDim theVals(1 To thenumber)
    4. theVals(1) = (1 / 2) + (thenumber / 2)
    5. For i = 2 To thenumber
    6.     theVals(i) = (theVals(i - 1) / 2) + (thenumber / (2 * theVals(i - 1)))
    7.     If theVals(i) = theVals(i - 1) Then
    8.         SquareRoot = theVals(i)
    9.         Exit Function
    10.     End If
    11. Next i
    12. End Function

    "Brothers, you asked for it."
    ...Francisco Domingo Carlos Andres Sebastian D'Anconia

  20. #20
    Frenzied Member
    Join Date
    Jul 1999
    Location
    Huntingdon Valley, PA 19006
    Posts
    1,151
    KayJay: Why use an array? If you do not have a lot of memory, you might have a problem finding the square root of a really large number like one billion.

    Iterating until an exact match is overkill. If two successive values match in the first 6-7 digits, you have about 12 or more correct digits.

    Can you Redim an array with a Double, which might not be an integer? Does this program compile?

    BTW: Can your program find the square root of two or three? It seems to me that you would only iterate once or twice.
    Live long & prosper.

    The Dinosaur from prehistoric era prior to computers.

    Eschew obfuscation!
    If a billion people believe a foolish idea, it is still a foolish idea!
    VB.net 2010 Express
    64Bit & 32Bit Windows 7 & Windows XP. I run 4 operating systems on a single PC.

  21. #21
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    Very true. It does not need an array, but this was not a final optimized routine. that said i'll optimize it. Yes to find out square roots of three and two are a problem with this. But we can use a "STEP" statement in the Loop to increment by 0.01 or 0.001 or whatever. An yes again the array cannot be dimensioned for as a double. But as U rightly pointed an array is not required at all. And yes this code will compile. And using the Round() function to test the results will make life easier.

    http://www.vbforums.com/showthread.p...ber+squareroot

    "Brothers, you asked for it."
    ...Francisco Domingo Carlos Andres Sebastian D'Anconia

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width