Feb 5, 2010 at 2:59pm UTC
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?
Feb 5, 2010 at 3:04pm UTC
Use the iostream library's <<
operator. It's exists to deal with typed I/O.
Feb 5, 2010 at 5:34pm UTC
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.
Feb 5, 2010 at 6:31pm UTC
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 Feb 5, 2010 at 6:32pm UTC
Feb 5, 2010 at 9:55pm UTC
Can you copy/cast it to an int and compare that against the original?