samedi 25 avril 2015

Save a float into an integer without losing floating point precision


I want to save the value of f in the third element of the array named i in a way that the floating point part isn't wiped (i.e. we don't want to save 1 instead of 1.5). After that, complete the last line in a way that we see 1.5 in the output (don't use cout<<1.5; or cout<<f; or some similar tricks!)

float f=1.5;
int i[3];
i[2] = ... ;
cout<<... ; 

Does anybody have any idea?

Note: I know this is not a typical, but this is a question of my Advanced C++ Programming course midterm exam!


Aucun commentaire:

Enregistrer un commentaire