Order of multiplication matters?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Order of multiplication matters?

  1. #1
    Join Date
    Dec 2010
    Posts
    10

    Order of multiplication matters?

    I've been fooling around with C++ over the Winter break to grow my knowledge when I came across this....

    Say I have a function that takes a scientific constant, a coefficient and a variable like pV=nRT using the universal gas constant or using PI for some trig function.

    If I define pi using #define, get the radius as a param and the coeff is hard coded, I have to perform the math like so: diameter = 2(coeff) * radius(passed param) * pi(literal);

    If I switch the order, I get an invalid type argument for unary * exception.

    What exactly is going on here that causes this type of behavior?

    Thanks.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,446

    Re: Order of multiplication matters?

    First, please post real code that shows the problem. Descriptions of code isn't good enough -- we would like to see the actual code that you're attempting to run.

    Secondly, order can matter, even though the different ordering should give you an equivalent answer if you calculated by hand. If you were to do the research, you will see many classical shool-book formulas have to be rewritten so that computing machines do not produce zero-divides, singularities, etc.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Apr 2009
    Posts
    598

    Re: Order of multiplication matters?

    an invalid type argument for unary * exception
    The star is used for two purposes in C:

    - a multliplication of two numbers (a binary *)
    - a reference to the content pointed by an address (a unary *)

    Here, it seems the compiler is considering that the star is used for the second purpose, for some reasons I dont know until I can see the code (my guess is that there is a ";" at the end of #define PI ..., and this is not important if PI is used at the end of the formula, but it matters if PI is used before the end).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center