How to verify if a value stored in a double is an integer value?

Hi,

I am writing matrix to files. The values in the cels of matrix are double or int.

So I do use fprinf("..."%.4f", value)

for write all values with 4 decimal places.

But sometimes all values in the matrix are int values, in this case I prefer to wrie them as %d, whithout decimal places.

It need to mention that all values are stored in a variable of type double.

How can I check if a particular value require decimal places or, in other words, how to check if that value can be stored in a int?
Use the iostream library's << operator. It's exists to deal with typed I/O.
You can use modf :

http://www.cplusplus.com/reference/clibrary/cmath/modf/

and check if fractional part equals to 0 and if yes that would mean that your number could be stored as an int
closed account (1yR4jE8b)
kbw is right, std::iostream exists so you shouldn't have to deal with cases like this. fprintf() is old C language and is only in C++ for backwards compatibility.
I proposed modf since he was using plain C. Of course C++ is more flexible. You will be amazed with how much C is out there though :)
Last edited on
Can you copy/cast it to an int and compare that against the original?
Topic archived. No new replies allowed.