user16720: Probably something stupid, but I can't figure out how to work with these large numbers, then cast the result (which will be small) to an integer. I keep getting negative numbers, which I think means I'm overflowing somewhere.
int freq = 440;
int len = 256;
long timer = 2000000.0 / (freq * len);
int roundedTimer = int(timer);

I should get 18, but I'm getting -108...

user4143: freq * long, since they are both integers, are calculated as integers.
440*256=112640 - in binary that is 1 1011 1000 0000 0000 and trimmed to 16 bits becomes 1011 1000 0000 0000 which is -18432 and that is where the heart of your problem is.  Unless you explicitly say otherwise the Arduino's compiler does all calculations (except floating point) as 16-bit signed integers.
By casting the values to long it forces the compiler to 'overflow' the 16-bit limit and use a 32-bit limit instead:
long timer = 2000000 / ((long)freq * (long)len);

Another alternative is to define freq and len to be long from the outset instead of int.

16720: <p>Probably something stupid, but I can't figure out how to work with these large numbers, then cast the result (which will be small) to an integer. I keep getting negative numbers, which I think means I'm overflowing somewhere.</p>

<pre><code>int freq = 440;
int len = 256;
long timer = 2000000.0 / (freq * len);
int roundedTimer = int(timer);
</code></pre>

<p>I should get 18, but I'm getting -108...</p>
 4143: <p><code>freq * long</code>, since they are both integers, are calculated as integers.</p>

<p><code>440*256=112640</code> - in binary that is <code>1 1011 1000 0000 0000</code> and trimmed to 16 bits becomes <code>1011 1000 0000 0000</code> which is <code>-18432</code> and that is where the heart of your problem is.  Unless you explicitly say otherwise the Arduino's compiler does all calculations (except floating point) as 16-bit signed integers.</p>

<p>By casting the values to <code>long</code> it forces the compiler to 'overflow' the 16-bit limit and use a 32-bit limit instead:</p>

<pre><code>long timer = 2000000 / ((long)freq * (long)len);
</code></pre>

<p>Another alternative is to define <code>freq</code> and <code>len</code> to be <code>long</code> from the outset instead of <code>int</code>.</p>
