ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Welcome to LinuxQuestions.org, a friendly and active Linux Community.

You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!

Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.

Hello,
for a small project I need to #define some constants of the
note frequencies. So I build an even smaller C program that calculates
the freqs as doubles, and have to print to stdout to get my #define file.

But printf("#define SI_BEMOLLE\t%.16f", val) gives me 16 digit of precision, 32 gives me 32, so, when I have to stop?

Hello,
for a small project I need to #define some constants of the
note frequencies. So I build an even smaller C program that calculates
the freqs as doubles, and have to print to stdout to get my #define file.

But printf("#define SI_BEMOLLE\t%.16f", val) gives me 16 digit of precision, 32 gives me 32, so, when I have to stop?

Thanks,
tano

musical frequencies don't require many digits in precision. 5..8 should be pretty enough, so it doesn't matter much which one you use. Besides, (if you want best quality) frequencies for notes should be picked by ear, or measured from real instrument. This is because mathematically correct frequencies (each next octave is 2x frequency of previous - as it used in synthesizers, MIDI etc.) sound incorrectly for very high/ very low pitches. High sound will be lower than they should be for high notes (third octave, i.e. 2 octaves higher than "middle C"), and it'll be higher that it should be for lower octaves (2 octaves below first octave). This is common problem in most midi players, mod trackers, etc.

Ok, take the question in a more abstract way: if I have to define a constant that is
periodic, like 3.33333333.... how can I know how many digits I have to put in my #define
for my pc?

Ok, take the question in a more abstract way: if I have to define a constant that is
periodic, like 3.33333333.... how can I know how many digits I have to put in my #define
for my pc?

You start by looking at the precision you need in the final answer in your calculation. If you are calculating the length of a wall for an architectural drawing then the nearest tenth of an inch is good enough. If you are calculating the size of the part of an automobile engine part then you probably need something like .001 inch precision.

Once you decide on your final precision then you have to allow for roundoff error. Every intermediate calculation in your program is subject to roundoff error. The more calculations you have the greater the roundoff error in the final result. You need as precise a constant as needed to give good enough precision in your final answer.

Presumably you will go through this exercise and determine that your variables need to be single precision floating point or perhaps double precision floating point. Then you set your periodic constant by dividing 10 by 3. Suppose you chose double precision floating point. Set up three floating point variables. Then initialize the first two variables to 10 and 3. Initialize the third variable to 10/3. The third variable will be repeating 3.33333 to the precision available in double precision floating point.

LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.