error

why does this code give -27 no it should answer yes what is wrong
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
	long double a,d=0.3333333333333333333333;
	long double b;
	int c,i=0;
		while(1)
	{
		cin>>a;
		b=pow(a,d);
		c=b;
		if(a==0)
			break;
		if(b==c)
		{
			cout<<"yes"<<endl;
			i++;
		}
		else
			cout<<"No"<<endl;
	}
		cout<<i;
	system("pause");
}
closed account (zb0S216C)
When c is assigned to b, c will truncate the fractional part of b, leaving c with a smaller number than b. So when the b == c comparison is made, c has a different value than b. Also, implicitly, c is temporarily casted to a float so c's type matches that of b's.

Wazzak
Last edited on
cout<< the value of each number. What do you notice? b is a double which will have decimals. When you assign the value of b to the integer variable c, you lose those decimals. Are the two truly the same (*hint* the program tells you)?

Edit:

Ninja'd
Last edited on
i want to know wich number is like a^3 and if is like that type yes so what should i do to fix it
that when i type -27 or 125 the program should type yes because they are -3^3and 5^3
when i give 27 it says yes but when i give -27 says no
Last edited on
any help
Topic archived. No new replies allowed.