Thursday, May 03, 2007

SVN, C++ weirdness on floats

Ok, I've decided to revive the old tavli project that I had left somewhere in the middle.

Now, maybe I'll leave it again before finishing, but there is a bright side (isn't there always...if you really really look ;-)

So, I now have subversion server running and I am happily exploring how the things work both from the linux side and also from that other OS you (I mean YOU) are using :P

And finally, C++ had me scratching my head once again.
In that neural network project I was talking about (tavli) I have defined a type like this:

typedef float real;

so, I go ahead and use real everywhere. The point of all this being that whenever I feel I'll alter the definition (say from float to double) and all that it would be needed is a re-compilation. Now, the application saves the weights of the neurons to a file.

I've decided to switch from float to double. Filesize didn't change. Weird huh?
I went to 'long double'. Still no filesize change. Ok, something was obvious wrong.

How was I to have the higher accuracy when the std::fstream was handling floating numbers the same no matter what I've tried?

Anyway, to cut a long story short. Here is the solution:
file_op.precision( std::numeric_limits<real>::digits10 );

You have to tell the fstream object what accuracy you want. Dumb if you ask me. (Not that you are asking me :P)