LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   #define a float : which precision? (http://www.linuxquestions.org/questions/programming-9/define-a-float-which-precision-672694/)

tanoatlq 09-27-2008 06:05 AM

#define a float : which precision?
 
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

kaz2100 09-27-2008 08:43 AM

Hya,

If I understand your question correct, the answer is whenever you are satisfied.

Say, val=1.234e-40, what you have is only a lot of zeros.

If you want to resolve 1.234567890123456 and 1.234567890123457, you need to code as accordingly.

Also, precision of double is implementation dependent.

Happy Penguins!

ErV 09-27-2008 09:36 AM

Quote:

Originally Posted by tanoatlq (Post 3293273)
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.

tanoatlq 09-27-2008 01:36 PM

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?

jailbait 09-27-2008 02:27 PM

Quote:

Originally Posted by tanoatlq (Post 3293592)
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.

---------------------
Steve Stites

ErV 09-27-2008 07:41 PM

--comment removed, because jailbait posted exactly same thing.--


All times are GMT -5. The time now is 09:35 AM.