Thread: Need help with sawtooth formula

1. 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?

2. Looks like a simple problem with integer division, e.g., 10000/262 should be 10000.0/262.0

3. Nope the answer still comes out to be -1.

4. 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 ?

5. 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.

6. I had just figured I forgot that in the equation. Everything works fine now thanks Tim.

Popular pages Recent additions