I have a question I was not able to find an answer for, I'm looking for some clarification.
From my understanding, C++ follows PEMDAS, is this correct?
So if I do something like 5/9 * (34/.2), it will have a hard time computing this formula due to the fact that the parentheses are at the end and not the front. Is this correct?
When I do (34/.2) *5/9, my formula works.
Any tips, pointers, guidance is appreciated. I feel like I'm really lost here.
The C++ Operator Precedence cppreference page contains the order of all operations in c++. It's a bit hard to digest all at once, but for simple mathematical operations you are concerned about row #5 and #6.
So yes, you can say that C++ somewhat follows PEMDAS, except, it doesn't have an exponent operator (see std::pow). But to clarify, multiplication and division are of the same priority (evaluated left to right) so are addition and subtraction (also left to right)
Assuming you mean in 5/9 * (34/.2) that 5/9 is a fraction, or in other words, the (34/.2) is not in the denominator, 5/9 * (34/.2) and (34/.2) * 5/9 should in theory evaluate to the same thing, right?
The reason they don't is because of integer division. When you write 5/9 what you should get is 0.555556, but because both 5 and 9 are integers, the / operator returns only the integral part of the result, in this case 0. However, when you evaluate the brackets first, you start by dividing an integer by a non-integer value (in this case, double). This returns a double result which when multiplied by 5 gives another double result, and dividing it by 9 doesn't do the integer division (because the first operand is double).
To reclarify the fault is not that the order is wrong, it's because of unexpected integer division. You can fix this problem by changing 5 into 5.0. This way, it's a double value and the / operator will never do integer division.
The simple answer is "Yes" C++ follows the standard order of precedence.
I would note that in PEMDAS the E stands for "exponent" and there is no way to express that in C++ you need to make a function call to achieve it but the rest is correct.
Definition:
P Parathasis first.
E Exponent second.
MD Multiplication and Division third (have the same precedence)
AS Addition and Subtraction fourth (have the same precedence)
MD happen left to right
AS happen left to right
5/9 * (34/.2)
it will have a hard time computing this formula due to the fact that the parentheses are at the end and not the front. Is this correct?
Wrong. The above expression is well define in maths and in C++ and has the same meaning.
(34/.2) *5/9
my formula works. Any tips, pointers, guidance is appreciated.
These are not the same expression. Multiplication and division have the same precedence and are applied left to right. You have changed the order of how these operators are applied. But you say they are supposed to be associative (i.e. order is not important). That is true. But you also have to throw in the type information. One easy thing to forget is that integer division is not what you expect (it throws away the remainder).
Version 1:
5/9 * (34/.2)
34/.2 => P1 170.0
5/9 => P2 0 // Integer division
P2 * P1 => P3 0
Version 2
(34/.2) *5/9
34/.2 => P1 170.0
P1 * 5 => P2 850.0
P2 / 9 => P3 94.4
Related
In C++ , when both numerator and denominator are integers and of same sign , then division operator gives the floor value of the quotient . But when they are of opposite sign , then it gives ceil value . Is my understanding correct or is there more to it ?
You have it right. Some 20th-century hardware engineer decided to do it this way, and as far as I know this is how all microprocessors now natively do it. Mathematically, it's often a little inconvenient, which is why Python (for example) corrects in software always to round toward toward floor.
For additional insight, besides p/q for an integer quotient, try p%q for the corresponding remainder.
Your question is tagged C++ but this is really a computer hardware issue and, as such, it may be more helpful to consult the C17 standard, whose sect. 6.5.5(6) reads:
When integers are divided, the result of the / operator is the algebraic quotient with any fractional
part discarded. If the quotient a/b is representable, the expression (a/b)*b + a%b shall equal a....
(I have a shred of a memory from 25 or 30 years ago, reading about a CPU that rounded toward floor. If my memory is not altogether imaginary, then that CPU apparently did not succeed in the marketplace, did it?)
Teaching myself C and finding that when I do an equation for a temp conversion it won't work unless I change the fraction to a decimal. ie,
tempC=(.555*(tempF-32)) will work but tempC=((5/9)*(tempF-32)) won't work.
Why?
According to the book "C Primer Plus" it should work as I'm using floats for both tempC and tempF.
It looks like you have integer division in the second case:
tempC=((5/9)*(tempF-32))
The 5 / 9 will get truncated to zero.
To fix that, you need to make one of them a floating-point type:
tempC=((5./9.)*(tempF-32))
When you do 5/9, 5 and 9 are both integers and integer division happens. The result of integer division is an integer and it is the quotient of the two operands. So, the quotient in case of 5/9 is 0 and since you multiply by 0, tempC comes out to be 0. In order to not have integer division, atleast one of the two operands must be float.
E.g. if you use 5.0/9 or 5/9.0 or 5.0/9.0, it will work as expected.
5/9 is an integer division not a floating point division. That's why you are getting wrong result.
Make 5 or 9 floating point variable and you will get correct answer.
Like 5.0/9 OR 5/9.0
5/9 is an integer expression, as such it gets truncated to 0. your compiler should warn you about this, else you should look into enabling warnings.
If you put 5/9 in parenthesis, this will be calculated first, and since those are two integers, it will be done by integer division and the result will be 0, before the rest of the expression is evaluated.
You can rearrange your expression so that the conversion to float occurs first:
tempC=((5/9)*(tempF-32)); → tempC=(5*(tempF-32))/9;
or of course, as the others say, use floating point constants.
I took a programming test, and my teacher took points off for diving by 3 instead of 3.0 , even though it appears I get the same answer.
volu = PI*(BASE*BASE)*(HEIGHT/3);
Does the .0 even matter in a c++ program, if so why?
Thank you!
EDIT: I used doubles for PI and BASE
(For what it's worth I find the way you have it to be rather prudent.)
If HEIGHT was an integral type then it would have been absolutely essential that you wrote 3.0 rather than 3. (3.0 is a literal of type double, cf. 3 which is an int.)
Otherwise the division would have taken place in integer arithmetic, causing the result to go off.
Personally I'd remove all the redundant parentheses and write it as
PI * BASE * BASE * HEIGHT / 3;
Yes, two integers will be divided using euclidean division. Adding the .0 changes it into a normal division, because you're no longer dividing two integers but an integer and a double.
Teaching myself C and finding that when I do an equation for a temp conversion it won't work unless I change the fraction to a decimal. ie,
tempC=(.555*(tempF-32)) will work but tempC=((5/9)*(tempF-32)) won't work.
Why?
According to the book "C Primer Plus" it should work as I'm using floats for both tempC and tempF.
It looks like you have integer division in the second case:
tempC=((5/9)*(tempF-32))
The 5 / 9 will get truncated to zero.
To fix that, you need to make one of them a floating-point type:
tempC=((5./9.)*(tempF-32))
When you do 5/9, 5 and 9 are both integers and integer division happens. The result of integer division is an integer and it is the quotient of the two operands. So, the quotient in case of 5/9 is 0 and since you multiply by 0, tempC comes out to be 0. In order to not have integer division, atleast one of the two operands must be float.
E.g. if you use 5.0/9 or 5/9.0 or 5.0/9.0, it will work as expected.
5/9 is an integer division not a floating point division. That's why you are getting wrong result.
Make 5 or 9 floating point variable and you will get correct answer.
Like 5.0/9 OR 5/9.0
5/9 is an integer expression, as such it gets truncated to 0. your compiler should warn you about this, else you should look into enabling warnings.
If you put 5/9 in parenthesis, this will be calculated first, and since those are two integers, it will be done by integer division and the result will be 0, before the rest of the expression is evaluated.
You can rearrange your expression so that the conversion to float occurs first:
tempC=((5/9)*(tempF-32)); → tempC=(5*(tempF-32))/9;
or of course, as the others say, use floating point constants.
In the Fortran95 code below the variable NMOM is an integer and always equal to 3.
What would the value of P0 evaluate to, 1 or -1? The divide by 2 then multiply by 2 bit has me confused, I'm not sure why you would do this, but this was written by a non-programmer scientist back in the 90s who is no longer around to ask.
P0=1
IF(NMOM-NMOM/2*2.EQ.1)P0=-1
The code is compiled with lf95 on a Linux machine.
I don't know Fortran, but my guess is that it is testing if NMOM is odd or even. First, rewrite with parens to simulate operator precedence:
IF(NMOM-((NMOM/2)*2).EQ.1)P0=-1
And if we are using integer math then (NMOM/2)*2 == NMOM if it's even else it will equal NMOM-1 if it's odd.