(5+6%4)

Pages: 1234
Oct 18, 2014 at 7:13am
I think the value of the above expression should be 6.5, but apparently this isn't correct. Can some one explain how to evaluate this correctly?
Oct 18, 2014 at 7:21am
% is modulo, not division.
For most cases, you can think of it as returning the remainder of the first number divided by the second (6 / 4), which would be 2 (4 goes into 6 one time, with 2 left over).

and 5 + 2 = 7
Last edited on Oct 18, 2014 at 7:22am
Oct 18, 2014 at 4:50pm
// you have write this way
1
2
3
4
5
6
7
#include<iostream>
int main() {
   int a=5, b=6, c=4;
   std::cout << a+ double(b)/c;   //6.5, without double it will be 6	
   std::cin.ignore();	
   return 0;
} 
Last edited on Oct 18, 2014 at 4:51pm
Oct 18, 2014 at 4:56pm
closed account (1CfG1hU5)
by operator precedence the 6 % 4 is happening first. remainder is 2, add 5, result is 7.

1.5 is the quotient of 6 divided by 4 to a remainder of zero. 1.5 + 5 = 6.5 is incorrect

% does remainder up to decimal point. 98 % 6 is good example. remainder is 2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdio.h>

int main()
{

int x = 0;

x = (5 + 6 % 4); // as topic, parentheses not needed

printf("6 % 4 + 5 is %d\n", x);

return 0;

}


iostream version:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;

int main()
{

int x = 0;

x = (5 + 6 % 4); // as topic, parentheses not needed

cout << "6 modulus 4 + 5 is " << x;

return 0;

}


% modulus causes a division, remainder is result. do not confuse % with /. results are not same.

Results

(op1 * op2) Product of the two operands
(op1 / op2) Quotient of (op1 divided by op2)
(op1 % op2) Remainder of (op1 divided by op2)

For / and %, op2 must be nonzero; op2 = 0 results in an
error. (You can't divide by zero.)
Last edited on Oct 19, 2014 at 2:30am
Oct 18, 2014 at 4:59pm
closed account (1CfG1hU5)
information about modulus from a dos compiler:

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
  Multiplicative operators   ( *  /  % )
There are three multiplicative operators:

  *  (multiplication)
  /  (division)
  %  (modulus or remainder)

Syntax:
  multiplicative-expr
     cast-expr
     multiplicative-expr * cast-expr
     multiplicative-expr / cast-expr
     multiplicative-expr % cast-expr

  Operands

 *  arithmetical type
 /  arithmetical type
 %  integral type

The usual arithmetic conversions are made on the operands.

  Results

 (op1 * op2)  Product of the two operands
 (op1 / op2)  Quotient of (op1 divided by op2)
 (op1 % op2)  Remainder of (op1 divided by op2)

For / and %, op2 must be nonzero; op2 = 0 results in an
error. (You can't divide by zero.)

When op1 and op2 are integers and the quotient is not an
integer:

 1. If op1 and op2 have the same sign,
    op1 / op2 is the largest integer less
    than the true quotient, and op1 % op2
    has the sign of op1.

 2. If op1 and op2 have opposite signs,
    op1 / op2 is the smallest integer greater
    than the true quotient, and op1 % op2 has
    the sign of op1.

  NOTE: Rounding is always toward zero. 
Last edited on Oct 18, 2014 at 4:59pm
Oct 18, 2014 at 5:06pm
closed account (1CfG1hU5)
here is operator precedence from a dos compiler:

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
  Precedence of Operators

In the following table of operator precedence, the Turbo C++
operators are divided into 16 categories.

The #1 category has the highest precedence; category #2
(Unary operators) takes second precedence, and so on to the
Comma operator, which has lowest precedence.

The operators within each category have equal precedence.

The Unary (category #2), Conditional (category #14), and
Assignment (category #15) operators associate right-to-left;
all other operators associate left-to-right.

   #  Category   ³ Operator ³ What it is (or does)

   1. Highest    ³    ()    ³ Function call
                 ³    []    ³ Array subscript
                 ³    ->    ³ C++ indirect component selector
                 ³    ::    ³ C++ scope access/resolution
                 ³     .    ³ C++ direct component selector

   2. Unary      ³     !    ³ Logical negation (NOT)
                 ³     ~    ³ Bitwise (1's) complement
                 ³     +    ³ Unary plus
		 ³     -    ³ Unary minus
                 ³    ++    ³ Preincrement or postincrement
                 ³    --    ³ Predecrement or postdecrement
                 ³     &    ³ Address
                 ³     *    ³ Indirection
                 ³  sizeof  ³ (returns size of operand, in bytes)
                 ³    new   ³ (dynamically allocates C++ storage)
                 ³  delete  ³ (dynamically deallocates C++ storage)

   3. Multipli-  ³     *    ³ Multiply
      cative     ³     /    ³ Divide
                 ³     %    ³ Remainder (modulus)

   4. Member     ³    .*    ³ C++ dereference
      access     ³    ->*   ³ C++ dereference

   5. Additive   ³     +    ³ Binary plus
                 ³     -    ³ Binary minus

   6. Shift      ³    <<    ³ Shift left
                 ³    >>    ³ Shift right

   7. Relational ³     <    ³ Less than
                 ³    <=    ³ Less than or equal to
                 ³     >    ³ Greater than
                 ³    >=    ³ Greater than or equal to

   8. Equality   ³    ==    ³ Equal to
                 ³    !=    ³ Not equal to

   9.            ³     &    ³ Bitwise AND

  10.            ³     ^    ³ Bitwise XOR

  11.            ³     |    ³ Bitwise OR

  12.            ³    &&    ³ Logical AND

  13.            ³    ||    ³ Logical OR

  14. Conditional³    ?:    ³ (a ? x : y  means "if a then x, else y")

  15. Assignment ³     =    ³ Simple assignment
                 ³    *=    ³ Assign product
                 ³    /=    ³ Assign quotient
                 ³    %=    ³ Assign remainder (modulus)
                 ³    +=    ³ Assign sum
                 ³    -=    ³ Assign difference
                 ³    &=    ³ Assign bitwise AND
                 ³    ^=    ³ Assign bitwise XOR
                 ³    |=    ³ Assign bitwise OR
                 ³   <<=    ³ Assign left shift
                 ³   >>=    ³ Assign right shift

  16. Comma      ³     ,    ³ Evaluate

All of the operators in this table can be overloaded except
the following:

       .     C++ direct component selector
      .*     C++ dereference
      ::     C++ scope access/resolution
      ?:     Conditional 
Last edited on Oct 18, 2014 at 6:21pm
Oct 18, 2014 at 5:06pm
closed account (1CfG1hU5)
operator precedence should be same today as was then
Last edited on Oct 18, 2014 at 5:29pm
Oct 18, 2014 at 5:29pm
Thank you!
Oct 18, 2014 at 5:37pm
closed account (1CfG1hU5)
i'm checking math or other headers for modulus define

if i find, will paste
Last edited on Oct 18, 2014 at 5:51pm
Oct 18, 2014 at 6:50pm
closed account (1CfG1hU5)
ironically, division operators are listed under Multiplicative
Oct 18, 2014 at 7:00pm
closed account (1CfG1hU5)
this page will shed some light about Multiplicative operators

http://en.cppreference.com/w/cpp/language/operator_arithmetic#Multiplicative_operators

Last edited on Oct 18, 2014 at 7:15pm
Oct 18, 2014 at 8:33pm
ironically, division operators are listed under Multiplicative

4 / 2 is the same as 4 * 0.5

Same reason why + and - are under additive.

1 - 2 is the same as 1 + (-2)
Last edited on Oct 18, 2014 at 8:34pm
Oct 18, 2014 at 8:52pm

jt1 wrote:
14. Conditional³ ?: ³ (a ? x : y means "if a then x, else y")
15. Assignment ³ = ³ Simple assignment

That's not a C++ precedence table
Oct 18, 2014 at 10:25pm
closed account (1CfG1hU5)
i did not remove some lined characters after pasting text to another text file.
a lot of work to fix. this loooks better. probably won't correct the text above.


1
2
3
4
5
6
7
8
9
10
11
12
13
15. Assignment  

     =     Simple assignment
     *=     Assign product
    /=     Assign quotient
    %=     Assign remainder (modulus)
     +=     Assign sum
    -=     Assign difference
   &=     Assign bitwise AND
   ^=    Assign bitwise XOR
    |=    Assign bitwise OR
 <<=    Assign left shift
 >>=     Assign right shift


the ?:
precedence of operators screen turbo c++ 1.01 dos

not a new c++ precedence table

pain to realign text, not doing more
Last edited on Oct 18, 2014 at 10:27pm
Oct 18, 2014 at 10:29pm
Turbo C++ 1.01 came out in february 1991, seven years before the first version of C++ itself was finalized. Nothing Turbo C++ says is relevant in 2014
Oct 18, 2014 at 10:32pm
closed account (1CfG1hU5)
uh huh. yea sure you're correct.

what about the "=" operator. that's not relevant in the current c++ 2014?

take your ridiculous arguments elsehwere. tired of worthless statements in these forums

int x = 9; // relevant turbo c++ dos. relevant today.

are people here paid to argue? must be making a fortune.
Oct 19, 2014 at 12:08am
closed account (1CfG1hU5)
quoted:
giblit (3375)
ironically, division operators are listed under Multiplicative

4 / 2 is the same as 4 * 0.5

Same reason why + and - are under additive.

1 - 2 is the same as 1 + (-2)

jt1 response:
4 * 0.5 = 2 is not a division. same result yes
4 / 2 = 2 is

would not say 2 * 2 is a division
Last edited on Oct 19, 2014 at 12:08am
Oct 19, 2014 at 4:15am
1
2
3
4
5
6
7
8
 #include <iostream>

int main()
{
std::cout << 5/2 << '\n';      // 2
std::cout << 5*0.5 << '\n';  // 2.5

}
Oct 19, 2014 at 5:25am
I was talking about in math not the difference between int's and floating point numbers. Yes, I know that int's floor. Anyways, if I ever have to divide by 2 and I am using an integer I will simply shift it right 1 bit.

By the way jt1 I never said that 4 * 0.5 is division. I simply stated 4 * 0.5 is the same as 4 / 2 (as in end result).
Last edited on Oct 19, 2014 at 5:27am
Oct 19, 2014 at 6:37am
closed account (1CfG1hU5)
quoted: 2nd line is jt1's line quoted by giblit, ironically...
giblit (3377)
ironically, division operators are listed under Multiplicative

giblit:
4 / 2 is the same as 4 * 0.5

Same reason why + and - are under additive.

1 - 2 is the same as 1 + (-2)

another giblit quote:
giblit (3377)
I was talking about in math not the difference between int's and floating point numbers. Yes, I know that int's floor. Anyways, if I ever have to divide by 2 and I am using an integer I will simply shift it right 1 bit.

By the way jt1 I never said that 4 * 0.5 is division. I simply stated 4 * 0.5 is the same as 4 / 2 (as in end result).

jt1 response:
 
if you never said that 4 * 0.5 is division, why did you say "4 / 2 is the same as 4 * 0.5".

that's saying division is same as multiplying. divisions can have the same result as multipliers.
certainly not the same.

bit shift could be used to get a divisor or multiplier result.

00000010 = 2 from this
00000100 = 4 to this

a bitshift from bit 2 to bit 3 can give the result of 4 as if 2 * 2 = 4 was done
could be useful or the other direction to be like a division was done.

00000100 = 4 from this
00000001 = 1 to this

as if 4 / 4 was done equaling 1. same result
Last edited on Oct 19, 2014 at 6:44am
Pages: 1234