jeudi 12 mars 2015

Counting number of digits in a double C++


I'm using a while loop to count the number of digits in my input.


So my input was 1.525



length = 0;
num = num - int(num);
while ( num >= .0001 ) {
num = num * 10;
length = length + 1;
num = num - int(num); }


When i do



cout << "\n\nLength: " << length << "\n";


The answear I get is 51 and other numbers give me an asnwear of 49 or something that is obviously wrong. Is it the way c++ works or is it just my mistake. Thank you.




Aucun commentaire:

Enregistrer un commentaire