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
Related
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 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.
Problem: In a homework problem (it is to be done on paper with a pen so no coding) I must determine the type and value of an addition performed in C++.
1 + 0.5
What I've answered is:
Type float (because I thought that integer + float = float)
Value 1.5 (As far as I know when two different datatypes are added,
the result of the addition is going to be converted to the datatype that does not loose any information.)
Solution says:
Type: double
Value: 1.5
My Question: Why is 0.5 a double and not a float? How can I distingish between a float and a double? I mean, 0.5 looks to me like a float and a double.
First of all, yes. integer + float = float. You are correct about that part.
The issue is not with that, but rather your assumption that 0.5 is a float. It is not. In C++, float literals are followed by an f meaning that 0.5f is a float. However, 0.5 is actually a double. That means that your equation is now:
integer + double = double
As you can see, this result is a double. That is why the correct answer to your question is that the resulting type is a double.
By the way, to clear the record, technically what's going on here isn't integer + double = double. What is happening is that the 1 is falling subject to implicit conversion. Essentially, the 1 is converted to a double, since the other side of the operation is a double as well. This way, the computer is adding the same types and not different ones. That means that the actual addition taking place here is more like:
double + double = double
In C++, floating point literals without a type suffix are double by default. If you want it to be float, you need to specify the f suffix, like 0.5f.
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:
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.