15,996,250 members
1.00/5 (1 vote)
See more:
hmmm in last loop when it assigns the value of int (b) to d...it always assigns int (b)-1 value to it...like if i enter 10.37 then the values on output are like given below...
http://postimg.org/image/4hetuo873/[^]
C++
```// Decimal Points Couter.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <conio.h>
using namespace std;
int main ()
{
double a,b,c,d;
int i=0;
cout << "Enter number a:";
cin >> a;
b=a-int (a);
cout << '\n';
cout << b << endl;
cout << '\n' << '\n';
loop:
b=b*10;
cout << b << endl;
d=int (b);
cout << d << endl;
c=b-d;
cout << c << endl;
b=c;
i++;
cout << '\n';
if (b>0) goto loop;
cout << i ;
_getch();
return 0;
}```
Posted
Updated 28-Jun-13 2:22am
v5
Maciej Los 28-Jun-13 7:12am
What's the question?
ImpedanceZ 28-Jun-13 7:23am
hmmm in last loop when it assigns the value of int (b) to d...it always assigns int (b)-1 value to it...like if i enter 10.37 then the values on output are like given below...
http://postimg.org/image/4hetuo873/
Richard MacCutchan 28-Jun-13 8:34am
Instead of pointing to an external link try explaining your problem properly. What results do you get and what should you get, would be a start.
ImpedanceZ 28-Jun-13 8:42am
hmmm ok...I get value to of d=int (b)-1...and I need value of d=int (b) to make c=0 and to terminate the loop...
Actually in last loop the value of float b is integer value itself like 1,2,3,etc...
Richard MacCutchan 28-Jun-13 8:59am
Ah, that makes it so much clearer.

## Solution 1

When you multiply a fraction like 0.7 by 10.0 you receive a result that is close to 7.0 but not exactly 7.0. The result might for example be 6.999998. And if you assign that to an int variable the result will be 6.

The reason for that behavior is that in double variables fractions are stored in the binary system, not in the decimal system. For example 0.7 has no exact representation as a binary fraction. And that is why instead of 0.7 the double variable holds something like 0.699999999999.

ImpedanceZ 28-Jun-13 8:46am
so how can I actually count the value of digits after decimal points?
nv3 28-Jun-13 9:46am
Strictly speaking, trying to count the digits in the fractional part does not make much sense. In the case of 0.6999999999999 you would get 12. What you expected was 1 - corresponding 0.7.

What you can do: Make an assumption about the accuracy of the value in your double variable, for example 10**-6. Then compare the remainder of the fraction in your loop to that accuracy and abort the loop if the remainder is either in the range of 1.0 +/ epsilon or 0.0 +/- epsilon (epsilon being the assumed accuracy).
ImpedanceZ 28-Jun-13 10:00am
thanx bro :) didn't get about assumption of accuracy because I have just started learning c++.
anyways thanx :) I found another way to find number of digits :)

## Solution 2

I'm not sure I got you, however it seems related on the classic problem people have with `double` numbers. Computer-stored `double` numbers are not (maths) real numbers: they are memory constrained and cannot achieve inifinite precision (see for instance "Double-precision floating-point format" at Wikipedia[^]). In your case, if you see (for instance with the debugger) the actual number in computer memory representing `10.37` then you could be surprised by the 'strange' `10.369999999999999`.

## Solution 3

This is what i got ...thanx for your support :)
C++
```#include "stdafx.h"
#include <iostream>
#include <conio.h>
using namespace std;
int main ()
{
double a,b;
int i=0;
cout << "Enter number a:";
cin >> a;
cout << '\n';
do
{
a=a*10;
cout << a << endl;
b=a-int (a);
cout << b << endl;
i++;
cout << '\n';
}while (b!=0);
cout << i << endl;
_getch();
return 0;
}
```

v2