Thread: Need help with sawtooth formula

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    45

    Question Need help with sawtooth formula

    What I am trying to do is make a loop so that x is increasingly inputted 100 times into a formula times and spit out the result on the screen.

    Here is part of my code:
    Code:
    double sample;
    
    P=(10000/262);
    
    A=i/P;
    
    sample = 2*((A)-(A+.5));
    It is the formula to the sawtooth function found here.

    However every time I run this code, the only answers it gives me is -1. I even tried graphing this function in my graphing calculator and it gives me the same answer. Please could you tell me what I am doing wrong?
    Last edited by Salem; 11-11-2010 at 12:07 AM. Reason: restore for sanity

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Looks like a simple problem with integer division, e.g., 10000/262 should be 10000.0/262.0
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    45
    Nope the answer still comes out to be -1.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    How about showing us the actual code, as opposed to 3 random lines out of context

    Where is the loop, where are the declarations for A, i, P ?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by chickenlittle View Post
    Code:
    sample = 2*((A)-(A+.5));
    You failed to use the floor in the formula.
    Code:
    sample = 2*((A)-floor(A+.5));


    Tim S.
    Last edited by stahta01; 11-10-2010 at 02:12 PM.

  6. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    45
    I had just figured I forgot that in the equation. Everything works fine now thanks Tim.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Zeller's Formula
    By unejam2005 in forum C++ Programming
    Replies: 6
    Last Post: 11-14-2005, 09:48 PM
  2. how to change char to formula
    By kosong in forum C Programming
    Replies: 2
    Last Post: 06-09-2003, 04:31 AM
  3. Math formula (js -->C++)
    By Demon1s in forum C++ Programming
    Replies: 20
    Last Post: 05-02-2003, 05:22 AM
  4. Need help with this formula -b/2a..Please read.
    By foofoo in forum C Programming
    Replies: 4
    Last Post: 06-04-2002, 11:59 PM
  5. Formula string conversion
    By Gr3g in forum C++ Programming
    Replies: 2
    Last Post: 04-12-2002, 08:28 PM