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.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Floating point numbers can only be represented with a certain amount of precision.
In C, you would normally use the 'double' type for floating point numbers, which has more precision than the 'float' type. This will improve the result in your example. (use "%lf" when formatting the output in printf [ignore this formatting, see below, %lf is only for scanf]).
Last edited by neonsignal; 08-04-2009 at 10:27 PM.
If you need even greater precision, use variables of type long double. Constants should be followed by the letter "L", either upper or lower case. (I recommend you use upper case "L", because lower case "l" looks too much like the numeral 1.) The format is "Lf" (upper case; lower doesn't work).
The 8087 architecture supports an 80 bit float, which is used for 'long double' by the better compilers.
Do you know a command line switch or other method to make gcc for x86_64 use the 8087 for 80 bit long double?
If I understood the documentation right, there is no way to get the Intel compiler (that I normally use) to use the 8087 in x86_64 code.
I also failed (but after not trying very hard) to get long doubles to have more precision than doubles using the Microsoft C++ compiler in x86_64 architecture.
Maybe none of those are the "better" compilers.
IIRC, most 32 bit x86 compilers support true 80 bit long doubles. 32 bit x86 may be the most popular architecture, but it isn't what I meant by most architectures.
Do you know a command line switch or other method to make gcc for x86_64 use the 8087 for 80 bit long double?
Not sure that it is possible (or at least, practical), because some of the 64 bit instructions share state with the 8087 stack.
For the Itanium architecture, there are facilities for doing higher precision multiply and accumulate (which would be useful eg for signal processing apps). But you'd have to resort to assembler for that in most compilers. The HP-UX compiler provides an extended floating point type for this purpose (which uses the 82 bit precision in the hardware). There has been discussion about how this might be done in gcc.
As you suggest, perhaps the GMP (or the APREC) libraries are the option for most applications that need higher precision.
Last edited by neonsignal; 08-05-2009 at 08:38 AM.
Not sure that it is possible (or at least, practical), because some of the 64 bit instructions share state with the 8087 stack.
For the Itanium architecture, there are facilities for doing higher precision multiply and accumulate (which would be useful eg for signal processing apps). But you'd have to resort to assembler for that in most compilers. The HP-UX compiler provides an extended floating point type for this purpose (which uses the 82 bit precision in the hardware). There has been discussion about how this might be done in gcc.
As you suggest, perhaps the GMP (or the APREC) libraries are the option for most applications that need higher precision.
It is not possible - hardware (x86_64) doesn't support it.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.