LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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



Reply
 
Search this Thread
Old 09-27-2008, 07:05 AM   #1
tanoatlq
Member
 
Registered: Mar 2007
Posts: 157

Rep: Reputation: 30
#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
 
Old 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: Reputation: 85
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!
 
Old 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: Reputation: 62
Quote:
Originally Posted by tanoatlq View Post
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.
 
Old 09-27-2008, 02:36 PM   #4
tanoatlq
Member
 
Registered: Mar 2007
Posts: 157

Original Poster
Rep: Reputation: 30
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?
 
Old 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: Reputation: 188Reputation: 188
Quote:
Originally Posted by tanoatlq View Post
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.
 
Old 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: Reputation: 62
--comment removed, because jailbait posted exactly same thing.--
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[C++] char * to float Ossar Programming 2 08-03-2005 11:28 AM
count digits of a float || convert float to string nadroj Programming 6 07-11-2005 05:52 PM
how big is a float in C? SciYro Programming 11 04-04-2005 10:24 AM
How to use a float in a script. philipina Programming 4 03-18-2004 09:06 AM
C newbie float ? bluesky Programming 7 09-03-2003 01:51 AM


All times are GMT -5. The time now is 07:47 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration