This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
C programming division
probably my question is very simple and stupid.
I would like to store the value of a division, in particular 1 / x where x is a
integer value.
int x = 17;
double result = 1/x;
I try to do it but I always get 0.000000 ... I try to enter a value fixed in x, for example 1/17 but always get the same value .. what's Wrong?
You are doing integer division.
Try the following and it will work as expected:
int x = 17;
double result = 1.0 / x;
The type of the 1 in the expression you have above is int, and the type of x is int. When you do int / int, you get an int back. You need at least one of the types involved to be floating point (float or double) in order for floating point division to occur.
Unlike in Mathematics, division in C++ can either refer to truncated integer division (what you did) or floating point division (what I did in my example). Be careful of this!
In my example, explicitly what we have is double / int -> double.
Related
This question already has answers here:
old-style simple cast precedence in c++
(2 answers)
Closed 4 months ago.
I have the following code. My main question lies in line 5.
int x1 = extRes1.at(1).toInt(); //A fairly large int value. This is from Qt, where extRes is a QStringList. the key is, the function returns an int value.
int x2 = extRes2.at(1).toInt();
int y1 = extRes1.at(2).toInt();
int y2 = extRes2.at(2).toInt();
double c = (double)(y2*x1-y1*x2)/(x1-x2); //Typecasting, as I want this arithmetic to return a floating point properly.
My question is, what is the exact behavior of the typecasting on line 5?
Based on what I've found on the topic so far, I believe that the result of Line 5 RHS (y2 *x1-y1 * x2)/(x1-x2) is represented by a double. But does typecasting work by turning all individual elements (such as y2, x1) in the arithmetic into the type (in this case double)? Or does it work by only converting the result of the final solution?
I am aware that on a technical level, my issue can be solved by converting the preexisting ints to doubles. Please let me know if more information is required.
Only the result of (y2*x1-y1*x2) (which is an int) is converted to double.
This is due to the precedence of the casting operator:
As you can see here, casting is priority over all "normal" artihmetic operations like multiplication and division.
Then this double is divided by the result of (x1-x2) (an int promoted to double for the division) using floating-point division, yielding the double result assigned to c.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
C programming division
probably my question is very simple and stupid.
I would like to store the value of a division, in particular 1 / x where x is a
integer value.
int x = 17;
double result = 1/x;
I try to do it but I always get 0.000000 ... I try to enter a value fixed in x, for example 1/17 but always get the same value .. what's Wrong?
You are doing integer division.
Try the following and it will work as expected:
int x = 17;
double result = 1.0 / x;
The type of the 1 in the expression you have above is int, and the type of x is int. When you do int / int, you get an int back. You need at least one of the types involved to be floating point (float or double) in order for floating point division to occur.
Unlike in Mathematics, division in C++ can either refer to truncated integer division (what you did) or floating point division (what I did in my example). Be careful of this!
In my example, explicitly what we have is double / int -> double.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
C programming division
probably my question is very simple and stupid.
I would like to store the value of a division, in particular 1 / x where x is a
integer value.
int x = 17;
double result = 1/x;
I try to do it but I always get 0.000000 ... I try to enter a value fixed in x, for example 1/17 but always get the same value .. what's Wrong?
You are doing integer division.
Try the following and it will work as expected:
int x = 17;
double result = 1.0 / x;
The type of the 1 in the expression you have above is int, and the type of x is int. When you do int / int, you get an int back. You need at least one of the types involved to be floating point (float or double) in order for floating point division to occur.
Unlike in Mathematics, division in C++ can either refer to truncated integer division (what you did) or floating point division (what I did in my example). Be careful of this!
In my example, explicitly what we have is double / int -> double.
This question already has answers here:
What is the behavior of integer division?
(6 answers)
Closed 1 year ago.
This behaves as wanted:
double t = r[1][0] * .5;
But this doesn't:
double t = ((1/2)*r[1][0]);
r is a 2-D Vector.
Just thought of a possibility. Is it because (1/2) is considered an int and (1/2) == 0?
Is it because (1/2) is considered an int and (1/2) == 0?
Yes, both of those literals are of type int, therefore the result will be of type int, and that result is 0.
Instead, make one of those literals a float or double and you'll end up with the floating point result of 0.5, ie:
double t = ((1.0/2)*r[1][0]);
Because 1.0 is of type double, the int 2 will be promoted to a double and the result will be a double.
Write this instead:
double t = ((1/2.0)*r[1][0]);
1 / 2 is an integer division and the result is 0.
1 / 2.0 is a floating point division (with double values after the usual arithmetic conversions) and its result is 0.5.
Because 1/2 is int/int division. That means whatever is the result will have anything after the decimal point removed (truncated). So 1/2 = 0.5 = 0.
Normally I always write the first number in double : 1.0/2 …..
If you make the very first number a double then all remaining calculation is done in double only.
double t = r[1][0] * .5;
is equivalent to:
double t = ((1/2f)*r[1][0]);
and not:
double t = ((1/2)*r[1][0]);
Due to loss of decimal part when the temporary result of 1/2 is stored in an int variable.
As a guideline whenever there is a division and there is a possibility of the answer being real number, do not use int or make one of the operands float or double or use cast.
You can write 1.0/2.0 instead. 1/2 displays this behaviour because both the denominator and numerator act are of an integer type and a variable of an integer type divided by another variable of an integer type is always truncated to an integer.
I cannot merit or demerit the standard of the question but this seem very critical issue to me. We assume that compiler will do the laundry for us all the time , but that is not true some times.
Is there any way to avoid this situation ?
Possibly
OR
More importantly knowing the monster (C,C++) as most of the people point out above
I would like to know if there are other ways to trace these "truncation" issues at compile time
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
C programming division
probably my question is very simple and stupid.
I would like to store the value of a division, in particular 1 / x where x is a
integer value.
int x = 17;
double result = 1/x;
I try to do it but I always get 0.000000 ... I try to enter a value fixed in x, for example 1/17 but always get the same value .. what's Wrong?
You are doing integer division.
Try the following and it will work as expected:
int x = 17;
double result = 1.0 / x;
The type of the 1 in the expression you have above is int, and the type of x is int. When you do int / int, you get an int back. You need at least one of the types involved to be floating point (float or double) in order for floating point division to occur.
Unlike in Mathematics, division in C++ can either refer to truncated integer division (what you did) or floating point division (what I did in my example). Be careful of this!
In my example, explicitly what we have is double / int -> double.