Visit the LQ Articles and Editorials section
 Home Forums HCL Reviews Tutorials Articles Register Search Today's Posts Mark Forums Read
 LinuxQuestions.org #define a float : which precision?
 Programming This forum is for all programming questions. The question does not have to be directly related to Linux and any language is fair game.

Notices

 09-27-2008, 07:05 AM #1 tanoatlq Member   Registered: Mar 2007 Posts: 157 Rep: #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
 09-27-2008, 09:43 AM #2 kaz2100 Senior Member   Registered: Apr 2005 Location: Penguin land, with apple, no gates Distribution: Debian testing woody(32) sarge etch lenny squeeze(+64) wheezy jessie Posts: 1,455 Rep: 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!
09-27-2008, 10:36 AM   #3
ErV
Senior Member

Registered: Mar 2007
Location: Russia
Distribution: Slackware 12.2
Posts: 1,202
Blog Entries: 3

Rep:
Quote:
 Originally Posted by tanoatlq 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.

 09-27-2008, 02:36 PM #4 tanoatlq Member   Registered: Mar 2007 Posts: 157 Original Poster Rep: 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?
09-27-2008, 03:27 PM   #5
jailbait
Guru

Registered: Feb 2003
Location: Blue Ridge Mountain
Distribution: Debian Wheezy, Debian Jessie
Posts: 7,593

Rep:
Quote:
 Originally Posted by tanoatlq 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

Last edited by jailbait; 09-27-2008 at 10:17 PM.

 09-27-2008, 08:41 PM #6 ErV Senior Member   Registered: Mar 2007 Location: Russia Distribution: Slackware 12.2 Posts: 1,202 Blog Entries: 3 Rep: --comment removed, because jailbait posted exactly same thing.--