Need help with sawtooth formula

This is a discussion on Need help with sawtooth formula within the C++ Programming forums, part of the General Programming Boards category; What I am trying to do is make a loop so that x is increasingly inputted 100 times into a ...

  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-10-2010 at 11:07 PM. Reason: restore for sanity

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,980
    Looks like a simple problem with integer division, e.g., 10000/262 should be 10000.0/262.0
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    2,372
    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 01: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, 08: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21