Type double is not supported, and treated the same as "float" on AVR-based Arduinos, so you get six or sometimes seven (at best) total digits of accuracy.
I doubt anyone could hear the resulting difference in musical tones.
All good suggestions. Ultimately going to do this on a Teensy, so the extra precision is probably there… even though I agree it probably isn’t necessary.
Thanks!
Have you considered how long it would take to measure the difference between those as audio frequencies?
At 1kHz it would be quarter of an hour before it was a whole cycle error.
Yes I agree that level of precision isn’t necessary. Partly just gaining experience with programming.
However, it needs to be a float. At the lower frequencies, say guitar low E, 1 Hz is 20 cents and very noticeable. Most guitarists need to tune to within 5 cents. For reference there are 100 cents (semitones) in a 1/2 step.
That would be insignificant by far. I agree that double isn’t needed, but float is absolutely necessary for any frequency calcs. Integers would not work.
Many digital music synths use scaled integers for frequency calculations. As long as you're within a couple cents (about 0.1%), any frequency error is below the threshold of human discernment. You can easily get that level of accuracy with a fixed-point number with, say, 9 or 10 bits in the fractional part. To keep it simple you can have a 16.16 fixed-point number in a 32-bit value (unsigned long on Arduino), and with that you can represent any possible frequency from 0 Hz to 65 kHz with a resolution of 0.026 cents.
That's enough for the whole hearing range of cats or possums (up to 64 kHz) but not for a porpoise (up to 150 kHz). So if you need it for porpoises you can take some of the fractional bits and give them to then integer part (maybe 18.14 fixed-point). Then you can say it's good enough for all intents and porpoises.
Fixed-point is an alternative method to floating-point to store a number with a fractional part. Floating-point has a radix point (known as a "decimal point" when talking about decimal) that can "move" depending on the exponent, but fixed-point keeps the radix point in the same spot. It can be faster than floating point because of that. One downside is it can't represent the same range of numbers as floating point in the general case, but in specific cases with fairly small ranges like frequencies in music, fixed-point can work great.
Also, saying something like "16.16 fixed-point" is just a shorthand way of saying there are 16 integer bits and 16 fractional bits.