Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > order of operations with division and multiplication

Reply
Thread Tools

order of operations with division and multiplication

 
 
Tim923
Guest
Posts: n/a
 
      04-08-2005
The following lines were tried:

x1 = (-b + sqrt(pow(b,2)-(4*a*c))) /(2*a);

x1 = (-b + sqrt(pow(b,2)-(4*a*c))) / 2*a;


I noticed that the last parentheses were optional and didn't change
anything.
Although if I mean 1/(2*5)=0.1 then 1/2*5=2.5 is not the same. What
is the difference?

 
Reply With Quote
 
 
 
 
Pete Becker
Guest
Posts: n/a
 
      04-08-2005
Tim923 wrote:
> The following lines were tried:
>
> x1 = (-b + sqrt(pow(b,2)-(4*a*c))) /(2*a);
>
> x1 = (-b + sqrt(pow(b,2)-(4*a*c))) / 2*a;
>
>
> I noticed that the last parentheses were optional and didn't change
> anything.
> Although if I mean 1/(2*5)=0.1 then 1/2*5=2.5 is not the same. What
> is the difference?
>


It's not order, but grouping. That is, you have to be sure that each
operation gets applied to the correct operands. Generally speaking, math
operators group from left to right, so a/b*c says that a should be
divided by b, and the result multiplied by c. To change this grouping,
add parentheses: a/(b*c).

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
 
Reply With Quote
 
 
 
 
Evan
Guest
Posts: n/a
 
      04-08-2005
It should matter, and does for me. Sure you tried it with something
other than a=1?

 
Reply With Quote
 
marbac
Guest
Posts: n/a
 
      04-08-2005
Tim923 wrote:

> Although if I mean 1/(2*5)=0.1 then 1/2*5=2.5 is not the same. What
> is the difference?
>


The Rank of both operators / and * is the same in the second example,
but the associativity in this case is from left to right. That means
that operator 1/2 is calculated first, after this 1/2 is multiplied by 5.

Due to higher Rank of "()" 2*5 is calculated before the division in the
first example.
 
Reply With Quote
 
Tim923
Guest
Posts: n/a
 
      04-08-2005
Evan wrote:
>It should matter, and does for me. Sure you tried it with something
>other than a=1?


That's it. I think a was 1.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Are multiplication and division of double exactly defined under C99with __STDC_IEC_559__ ? Francois Grieu C Programming 0 06-08-2010 11:59 AM
Re: Unsigned Integer Overflow on Multiplication and Division Keith Thompson C Programming 2 05-13-2010 08:48 PM
Re: Unsigned Integer Overflow on Multiplication and Division Eric Sosman C Programming 1 05-13-2010 08:18 PM
Source of term "multiplication" in matrix multiplication William Hughes C Programming 13 03-15-2010 02:04 PM
need help with large int multiplication and division akickdoe22@hotmail.com C++ 1 01-21-2005 02:46 AM



Advertisments