Warning when converting to `int' from `double' - c++

How do you get the return type to come back as a double for this expression: a = 2 * NUM + z;?
Here is my full program:
#include <iostream>
using namespace std;
int main()
{
const int NUM = 10;
const double x = 20.5;
int a, b;
double z;
char grade;
a = 25;
cout <<"a = " <<a <<endl;
cout <<" Enter two integers : ";
cin >> a >> b;
cout << endl;
cout << " The numbers you entered are "
<<a <<" and " <<b <<endl;
z = x + 2 * a - b;
cout <<"z = " <<z <<endl;
grade = 'A';
cout <<"Your grade is " <<grade <<endl;
a = 2 * NUM + z;
cout << "The value of a = " << a <<endl;
return 0;
}

Just declare a to be a double instead of an int.
double a;
int b;
// ...

You've defined a as an int so there's no way it can hold a double value. If you change the definition to double a; it should work fine.

Your z is a double so 2 * NUM + z is also a double, but you assigned the value to a which is an int. I'm not really what you are trying to do here; if you are trying to store the value of 2 * NUM + z in a with truncation, then you should add a cast: a = static_cast<int>(2 * NUM + z); OTOH, if you don't want truncation but want to store the full double value, then you need to store it in a double variable.

From the standard, 6.3.1.8 Usual arithmetic conversions
.......
Otherwise, the integer promotions are performed on both operands. Then the following rules are applied to the promoted operands:
If both operands have the same type, then no further conversion is needed.
Otherwise, if both operands have signed integer types or both have unsigned integer types, the operand with the type of lesser integer conversion rank is converted to the type of the operand with greater rank.
So, in the expression -
2 * NUM + z;
NUM is a of type const int which is of lower rank when compared to the type of z which is double. According to rule, int is promoted to double. The result of 2 double type operands will yield result of type double. But you are taking assigning the result to an int type, hence the warning. Changing the type of a to double should work fine.

Related

Applying modulo operation on a value of type int and unsigned integer

For example the code below
int a = -7777;
int b = 10;
cout<< a % b<<endl;
cout<<(a+b)%b<<endl;
cout<< -7777%10 <<endl;
The results are:
-7
-7
-7
but if I changed the type of b to unsigned int, it has different values;
int a = -7777;
unsigned int b = 10;
cout<< a % b<<endl;
cout<<(a+b)%b<<endl;
cout<< -7777%10 <<endl;
The resutls are
9
9
-7
Could any body advise how it is working here? How do the differences come?
Btw: I am using C++ in Xcode latest version.
cout<< a % b << endl;
In the above line a is converted to unsigned type (since you declared b as unsigned) due to usual arithmetic conversions.
When I interpreted binary representation of -7777 on my machine as positive value it yields, 4294959519, which might explain the result of 9 in your case.
In this case:
cout<< -7777%10 <<endl;
no promotion is done since both literals are of type int and you see result -7.

Am I doing double to float conversion here

const double dBLEPTable_8_BLKHAR[4096] = {
0.00000000000000000000000000000000,
-0.00000000239150987901837200000000,
-0.00000000956897738824125100000000,
-0.00000002153888378764179400000000,
-0.00000003830892270073604800000000,
-0.00000005988800189093979000000000,
-0.00000008628624126316708500000000,
-0.00000011751498329992671000000000,
-0.00000015358678995269770000000000,
-0.00000019451544774895524000000000,
-0.00000024031597312124120000000000,
-0.00000029100459975062165000000000
}
If I change the double above to float, am I doing incurring conversion cpu cycles when I perform operations on the array contents? Or is the "conversion" sorted out during compile time?
Say, dBLEPTable_8_BLKHAR[1] + dBLEPTable_8_BLKHAR[2] , something simple like this?
On a related note, how many trailing decimal places should a float be able to store?
This is c++.
Any good compiler will convert the initializers during compile time. However, you also asked
am I incurring conversion cpu cycles when I perform operations on the array contents?
and that depends on the code performing the operations. If your expression combines array elements with variables of double type, then the operation will be performed at double precision, and the array elements will be promoted (converted) before the arithmetic takes place.
If you just combine array elements with variables of float type (including other array elements), then the operation is performed on floats and the language doesn't require any promotion (But if your hardware only implements double precision operations, conversion might still be done. Such hardware surely makes the conversions very cheap, though.)
Ben Voigt answer addresses your question for most parts.
But you also ask:
On a related note, how many trailing decimal places should a float be able to store
It depends on the value of the number you are trying to store. For large numbers there is no decimals - in fact the format can't even give you a precise value for the integer part. For instance:
float x = BIG_NUMBER;
float y = x + 1;
if (x == y)
{
// The code get here if BIG_NUMBER is very high!
}
else
{
// The code get here if BIG_NUMBER is no so high!
}
If BIG_NUMBER is 2^23 the next greater number would be (2^23 + 1).
If BIG_NUMBER is 2^24 the next greater number would be (2^24 + 2).
The value (2^24 + 1) can not be stored.
For very small numbers (i.e. close to zero), you will have a lot of decimal places.
Floating point is to be used with great care because they are very imprecise.
http://en.wikipedia.org/wiki/Single-precision_floating-point_format
For small numbers you can experiment with the program below.
Change the exp variable to set the starting point. The program will show you what the step size is for the range and the first four valid numbers.
int main (int argc, char* argv[])
{
int exp = -27; // <--- !!!!!!!!!!!
// Change this to set starting point for the range
// Starting point will be 2 ^ exp
float f;
unsigned int *d = (unsigned int *)&f; // Brute force to set f in binary format
unsigned int e;
cout.precision(100);
// Calculate step size for this range
e = ((127-23) + exp) << 23;
*d = e;
cout << "Step size = " << fixed << f << endl;
cout << "First 4 numbers in range:" << endl;
// Calculate first four valid numbers in this range
e = (127 + exp) << 23;
*d = e | 0x00000000;
cout << hex << "0x" << *d << " = " << fixed << f << endl;
*d = e | 0x00000001;
cout << hex << "0x" << *d << " = " << fixed << f << endl;
*d = e | 0x00000002;
cout << hex << "0x" << *d << " = " << fixed << f << endl;
*d = e | 0x00000003;
cout << hex << "0x" << *d << " = " << fixed << f << endl;
return 0;
}
For exp = -27 the output will be:
Step size = 0.0000000000000008881784197001252323389053344726562500000000000000000000000000000000000000000000000000
First 4 numbers in range:
0x32000000 = 0.0000000074505805969238281250000000000000000000000000000000000000000000000000000000000000000000000000
0x32000001 = 0.0000000074505814851022478251252323389053344726562500000000000000000000000000000000000000000000000000
0x32000002 = 0.0000000074505823732806675252504646778106689453125000000000000000000000000000000000000000000000000000
0x32000003 = 0.0000000074505832614590872253756970167160034179687500000000000000000000000000000000000000000000000000
const double dBLEPTable_8_BLKHAR[4096] = {
If you change the double in that line to float, then one of two things will happen:
At compile time, the compiler will convert the numbers -0.00000000239150987901837200000000 to the float that best represents them, and will then store that data directly into the array.
At runtime, during the program initialization (before main() is called!) the runtime that the compiler generated will fill that array with data of type float.
Either way, once you get to main() and to code that you've written, all of that data will be stored as float variables.

What argument passed?

I am learning C++ language, but much confused about a little thing, that is
below I have putted some code of making a square of an integer. But I am not understanding the treatment of x in raiseToPow function. Here double x is an argument taking the value from calling function and passing to x in for loop. So how it makes square of that integer passing from x. Please guide me.
#include <iostream>
using namespace std;
double raiseToPow(double x, int power){
double result;
result = 1.0;
for (int i = 1; i <= power; i++)
{
result *= x;
}
return (result);
}
main()
{
double x;
int i;
cout << "Enter the Number: ";
cin >> x;
cout << "Enter the Power: ";
cin >> i;
cout << x << "Raise to power " << i << " is equal to " << raiseToPow(x , i);
}
double x is the base of the exponentiation xpower.
The function uses the definition xpower is 1 multiplied by x power times. Thus 23 is 1*2*2*2 and 45 is 1*4*4*4*4*4. So double result is set to be 1, then the for loop executes power times, each time multiplying result by x and storing that back in result.
Even though this is C++, not C, the logic is the same.
You pass in a number(x) and its exponent(power). The value of x is multiplied 'power' times.
For example, if you want to know the value of 2 raised to the power of 3, you would pass 2, 3 to the function raiseToPow
The statement
result*=x
is the same as
result = x*result.
So, the first time through the loop, result would just be the value of x (2, in this example). The second time through the loop, result*=x would multiply the value of x(which is 2) times the value stored in result (which, for now, is also 2). The third and final time through the loop, result *=x would multiply x times the value stored in result (which now is 4).
The value of 2^3=8, and that is what the value stored in the variable result is, so the value 8 is returned from the call raiseToPow(2,3).

'Int' is not convertible to type 'double'

I keep getting this error at run-time: "'Int' is not convertible to type 'double'", it displays this as soon as I run the program, but then it quickly disappears and then shows my program. I'm using VC2010. (EDIT: This program is to convert Celsius to Fahrenheit, and to tell if it's hot or not hot.)
#include <iostream>
int convert(int);
int main(void)
{
using std::cout;
using std::cin;
using std::endl;
cout << "Enter the degrees in Celsius: ";
int temp;
cin >> temp;
int degrees = convert(temp);
if(degrees<100)
{
cout << degrees << " is not too hot." << endl;
}
else if(degrees>100)
{
cout << degrees << " is hot." << endl;
}
cin.get();
cin.get();
return 0;
}
int convert(int ctf)
{
return ctf * 1.8 + 32;
}
You should explicitly cast the result of convert method to int to avoid this message.
int convert(int ctf)
{
return (int) (ctf * 1.8 + 32);
}
Since the return type is specified ad int, but the result of the float multiplication is not int, it is showing this message.
However, since you are converting temperatures from Celsius to Fahrenheit, it is better to use double or float values instead of int to yield more accurate and meaningful output.
You are getting a compiler warning informing you of a loss of precision by specifying that convert returns an int while the expression ctf * 1.8 + 32 returns a double as 1.8 is of type double. Arithmetic expressions involving variables of type int and double will promote the resulting type to double. I recommend updating your convert function to:
double convert(double ctf)
If you insist on using integers, make the appropriate cast:
int convert(int ctf)
{
return static_cast<int>(ctf * 1.8 + 32);
}
I think your error is in the convert function, by multiplying a int with a decimal you are automatically converting it to a double. so either return a double, or cast it to a int

Getting First digit in double and storing it in an int C++

Hello im coding in C++ and i need some help with converting a double to an int.
what a need is a way to get the first number from a double ie (3.5945) "3".
and put that number into an int.
I'm using static_cast now and its returning a 0.
double X = 3.1234;
double Y = 4.3455;
int myIntX = static_cast <int>(X);
int myIntY = static_cast <int>(Y);
cout << myIntX << endl;
cout << myIntY << endl;
output....
0
0
Try this:
double x=3.1234;
int myintx=(int)x;
while(myintx%10!=0)
myintx/=10;
cout<<myintx;
This will give you the first digit your double as an int.