Question:
In C,how to declare fixed point decimal constants and decimal data types?
Sheer
2012-07-12 20:30:03 UTC
If we know the decimal numbers we would be using or obtaining accurately and exactly,and if we are also sure about the number of decimal places, then how to use fixed point decimal constants/variables in C? One top contributor tells me this :

"Floats are inherently inexact because of the way they are stored internally. If you want exact precision, use decimal variables instead."

But how on earth do I declare fixed point decimal constants in C?And what's the format specifier for it? It's not in my book nor could I find any good answers on google.It takes me to an IBM site which only serves to confuse me more.

http://publib.boulder.ibm.com/infocenter/zos/v1r12/index.jsp?topic=%2Fcom.ibm.zos.r12.cbcpx01%2Fcbcpg1b0263.htm

It says we have to to declare a fixed point decimal like this "decimal(10,2) x;" and also we need to include a "decimal.h" header file.I tried on my Mingw compiler but it says there is no such header file and the "decimal(10,2) x;" thing shows error.The IBM site then says I need to tweak the compiler to enable the header file.DAMN.

So can you PLEASE tell me in simple words how to declare a fixed point decimal constant/variable in C.
Three answers:
2012-07-12 22:00:03 UTC
C has no support for fixed point. If you need it, you have to use either an external library or implement it yourself. The question is, what do you think you need a fixed point number for? If it's something as simple as storing currency, then it's better to use a long int, like so:



#include



int main(void)

{

        long int n = 12345;



        printf("%ld.%02ld\n", n / 100, n % 100);

        return 0;

}



In this case you perform all the internal arithmetic on the number of cents, and the format is only converted when you decide to either write or read the data.



If exact precision is important, then an arbitrary precision arithmetic library like libgmp is a better choice than fixed point for modern platforms. Note that in most real cases (like for storing coordinates in a 3D context) exact precision isn't really important, and floating point is a much better option.
Macadameane
2012-07-12 20:43:16 UTC
A decimal "type" is really either two separate numbers stored or a string of characters that is computed like a number. It does have its uses, like currency, but is usually used to store a number that won't have a whole lot of math.



If you are simply adding dollar amounts together, that is great, but still more expensive than a float or double. If you are multiplying and/or dividing, I wouldn't use them.



Consider figuring out tax:

10.00 * 0.0875 = 0.875 cents



If you want to keep that 0.005 cents around, you cannot store it as a decimal. When the other user said that floats and doubles are inexact, it is because they are, however, it is usually a very minute amount. By the time you get the answer you need, you can just round and it will be correct unless you are dealing with extremely small values, like 0.000000001 or so.



I'm not familiar with the C decimal library, so I can't answer your question there (sorry), but I wanted you to understand that using a double should be just fine to store what you need, and it will be faster.
?
2012-07-12 21:03:37 UTC
just use



#define PI 3.14159265



otherwise just use a double... don't worry yourself with the decimal to binary inexactness, just know that it exists if you are getting weird values....


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Loading...