# Need help with sawtooth formula

• 11-10-2010
chickenlittle
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?
• 11-10-2010
laserlight
Looks like a simple problem with integer division, e.g., 10000/262 should be 10000.0/262.0
• 11-10-2010
chickenlittle
Nope the answer still comes out to be -1.
• 11-10-2010
Salem
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 ?
• 11-10-2010
stahta01
Quote:

Originally Posted by chickenlittle
Code:

`sample = 2*((A)-(A+.5));`

You failed to use the floor in the formula.
Code:

`sample = 2*((A)-floor(A+.5));`

Tim S.
• 11-10-2010
chickenlittle
I had just figured I forgot that in the equation. Everything works fine now thanks Tim.