How to redefine arithmetic operators.

Discussion in 'C Programming' started by Raghu, Nov 29, 2005.

  1. Raghu

    Raghu Guest

    Hello all,

    I need your help on how to redefine teh function of arithmetic operators in
    C.

    for example : if there is an equation c = a/b;

    i want to execute my own algorithm for division operator.
    I am compelled to use the native C , instead of calling functions or using
    macros for doing arithmetic operations.
    I mean i was compelled not to use c =MYDIV(a,b) or somewthing like that.

    This i needeed to simulate floating point unit of MPC5554 on PC.In MPC the
    way the rounding , overflow,errors dealt is different from those used in P4
    or AMD.

    so i need to simulate the floating point arithmetic of MPC5554 , when i want
    to simualte the code designed for it.
    I hope i could able express my question clearly.

    Your help and advice is sought after at the earliest.

    Thanks and Best Regards
    Raghu.
    Raghu, Nov 29, 2005
    #1
    1. Advertising

  2. Raghu

    Marc Boyer Guest

    Le 29-11-2005, Raghu <> a écrit :
    > I need your help on how to redefine teh function of arithmetic operators in
    > C.
    >
    > for example : if there is an equation c = a/b;
    >
    > i want to execute my own algorithm for division operator.
    > I am compelled to use the native C , instead of calling functions or using
    > macros for doing arithmetic operations.
    > I mean i was compelled not to use c =MYDIV(a,b) or somewthing like that.


    The difference between c=MYDIV(a,b) and c=a/b is only 'syntaxic
    sugar'.
    If you really need to overload operators, you can do it in C++,
    but there is no way to do it in C.

    Marc Boyer
    Marc Boyer, Nov 29, 2005
    #2
    1. Advertising

  3. Raghu

    jacob navia Guest

    Raghu a écrit :
    > Hello all,
    >
    > I need your help on how to redefine teh function of arithmetic operators in
    > C.
    >
    > for example : if there is an equation c = a/b;
    >
    > i want to execute my own algorithm for division operator.
    > I am compelled to use the native C , instead of calling functions or using
    > macros for doing arithmetic operations.
    > I mean i was compelled not to use c =MYDIV(a,b) or somewthing like that.
    >
    > This i needeed to simulate floating point unit of MPC5554 on PC.In MPC the
    > way the rounding , overflow,errors dealt is different from those used in P4
    > or AMD.
    >
    > so i need to simulate the floating point arithmetic of MPC5554 , when i want
    > to simualte the code designed for it.
    > I hope i could able express my question clearly.
    >
    > Your help and advice is sought after at the earliest.
    >
    > Thanks and Best Regards
    > Raghu.
    >
    >
    >

    I am afraid to start a new flame war here, but if you accept
    to use *non-standard* C, the lcc-win32 compiler allows you to
    do operator overloading without leaving the framework of C.

    Of course this is an extension, but it is compatible with the standard.
    No new keywords are used.

    http://www.cs.virinia.edu/~lcc-win32
    jacob navia, Nov 29, 2005
    #3
  4. jacob navia <> writes:
    > Raghu a écrit :
    >> I need your help on how to redefine teh function of arithmetic
    >> operators in
    >> C.
    >> for example : if there is an equation c = a/b;

    [snip]
    >>

    > I am afraid to start a new flame war here, but if you accept
    > to use *non-standard* C, the lcc-win32 compiler allows you to
    > do operator overloading without leaving the framework of C.
    >
    > Of course this is an extension, but it is compatible with the
    > standard. No new keywords are used.


    In what sense is this "without leaving the framework of C"?

    You don't really *need* operator overloading; anything you can do with
    it, you can do without it (with ordinary function calls).

    Some languages support operator overloading: C++, Ada, the extended
    C-like language supported by lcc-win32, and others. Standard C itself
    does not. (And of course lcc-win32, as the name implies, is limited
    to 32-bit Windows systems; see comp.compilers.lcc for more
    information.)

    Note that a conforming implementation may have extensions (including
    additional library functions), provided they do not alter the behavior
    of any strictly conforming program, so lcc-win32 may still qualify as
    a C compiler.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Nov 29, 2005
    #4
  5. Raghu

    jacob navia Guest

    Keith Thompson a écrit :
    >
    > You don't really *need* operator overloading; anything you can do with
    > it, you can do without it (with ordinary function calls).


    This is of course true.

    It is just a matter of measure.

    Consider this:
    qfloat a;
    qfloat q = (sqrt(a+1)/sqrt(a-1))*(cos(a+1)/cos(a-1));

    compared with:

    tmp1 = qadd(a,1);
    tmp2 = sqrtq(tmp1);
    tmp3 = qsub(a,1);
    tmp4 = sqrt(tmp3);
    tmp5 = qdiv(tmp1,tmp3);
    tmp6 = cosq(tmp1);
    tmp7 = cosq(tmp3);
    tmp8 = qdiv(tmp6,tmp7);
    result = qmul(tmp8,tmp5);

    This does the *same* stuff but which expression would
    you prefer???

    And above all, which expression would you like to MAINTAIN?

    Of course operator overloading is just "syntactic sugar".
    But sugar is an essential component of meals !!!

    Of course you can abuse it, (as you can abuse *real* sugar!)
    but for *many* applications is the only way to go!

    Being able to define new types of numbers is one of them.
    Otherwise, the C syntax is totally awkward.

    jacob
    jacob navia, Nov 29, 2005
    #5
  6. Raghu

    Randy Howard Guest

    jacob navia wrote
    (in article <438c2df2$0$29185$>):

    > I am afraid to start a new flame war here,


    Actually, you seem to revel in it.

    > but if you accept to use *non-standard* C,

    (thereby leaving the 'framework of C')

    > the lcc-win32 compiler allows you to
    > do operator overloading without leaving the framework of C.


    Incorrect. You are using extensions so non-standard that they
    ONLY work on a single compiler, for a single operating system.

    Even the folks that use gcc extensions are far better off than
    that, nevermind the POSIX crowd, which has it golden in
    comparison.

    > ... but it is compatible with the standard.


    No, it isn't. Try compiling it on a compiler from anyone else.
    So much for that theory.

    --
    Randy Howard (2reply remove FOOBAR)
    "The power of accurate observation is called cynicism by those
    who have not got it." - George Bernard Shaw
    Randy Howard, Nov 29, 2005
    #6
  7. Raghu

    Jack Klein Guest

    On Tue, 29 Nov 2005 11:31:19 +0100, jacob navia
    <> wrote in comp.lang.c:

    > Raghu a écrit :
    > > Hello all,
    > >
    > > I need your help on how to redefine teh function of arithmetic operators in
    > > C.
    > >
    > > for example : if there is an equation c = a/b;
    > >
    > > i want to execute my own algorithm for division operator.
    > > I am compelled to use the native C , instead of calling functions or using
    > > macros for doing arithmetic operations.
    > > I mean i was compelled not to use c =MYDIV(a,b) or somewthing like that.
    > >
    > > This i needeed to simulate floating point unit of MPC5554 on PC.In MPC the
    > > way the rounding , overflow,errors dealt is different from those used in P4
    > > or AMD.
    > >
    > > so i need to simulate the floating point arithmetic of MPC5554 , when i want
    > > to simualte the code designed for it.
    > > I hope i could able express my question clearly.
    > >
    > > Your help and advice is sought after at the earliest.
    > >
    > > Thanks and Best Regards
    > > Raghu.
    > >
    > >
    > >

    > I am afraid to start a new flame war here, but if you accept
    > to use *non-standard* C, the lcc-win32 compiler allows you to
    > do operator overloading without leaving the framework of C.
    >
    > Of course this is an extension, but it is compatible with the standard.
    > No new keywords are used.
    >
    > http://www.cs.virinia.edu/~lcc-win32


    Jacob, as much as I admire lcc-win32, I was not aware that you had a
    cross compiler that output PowerPC executables.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Nov 29, 2005
    #7
  8. Raghu

    Richard Bos Guest

    jacob navia <> wrote:

    > Consider this:
    > qfloat a;
    > qfloat q = (sqrt(a+1)/sqrt(a-1))*(cos(a+1)/cos(a-1));
    >
    > compared with:


    > This does the *same* stuff but which expression would
    > you prefer???


    Neither. I would prefer not to use superfluous not-C. Even more than
    that I would prefer not-C not to be discussed here.

    Richard
    Richard Bos, Nov 29, 2005
    #8
  9. Raghu

    jacob navia Guest

    Jack Klein wrote:
    > On Tue, 29 Nov 2005 11:31:19 +0100, jacob navia
    > <> wrote in comp.lang.c:
    >
    >
    >>Raghu a écrit :
    >>
    >>>Hello all,
    >>>
    >>>I need your help on how to redefine teh function of arithmetic operators in
    >>>C.
    >>>
    >>>for example : if there is an equation c = a/b;
    >>>
    >>>i want to execute my own algorithm for division operator.
    >>>I am compelled to use the native C , instead of calling functions or using
    >>>macros for doing arithmetic operations.
    >>>I mean i was compelled not to use c =MYDIV(a,b) or somewthing like that.
    >>>
    >>>This i needeed to simulate floating point unit of MPC5554 on PC.In MPC the
    >>>way the rounding , overflow,errors dealt is different from those used in P4
    >>>or AMD.
    >>>
    >>>so i need to simulate the floating point arithmetic of MPC5554 , when i want
    >>>to simualte the code designed for it.
    >>>I hope i could able express my question clearly.
    >>>
    >>>Your help and advice is sought after at the earliest.
    >>>
    >>>Thanks and Best Regards
    >>>Raghu.
    >>>
    >>>
    >>>

    >>
    >>I am afraid to start a new flame war here, but if you accept
    >>to use *non-standard* C, the lcc-win32 compiler allows you to
    >>do operator overloading without leaving the framework of C.
    >>
    >>Of course this is an extension, but it is compatible with the standard.
    >>No new keywords are used.
    >>
    >>http://www.cs.virinia.edu/~lcc-win32

    >
    >
    > Jacob, as much as I admire lcc-win32, I was not aware that you had a
    > cross compiler that output PowerPC executables.
    >


    AAAAArgh!!

    You are right. My fault.

    jacob
    jacob navia, Nov 29, 2005
    #9
  10. In article <4all.nl>,
    Richard Bos <> wrote:
    >jacob navia <> wrote:
    >
    >> Consider this:
    >> qfloat a;
    >> qfloat q = (sqrt(a+1)/sqrt(a-1))*(cos(a+1)/cos(a-1));
    >>
    >> compared with:

    >
    >> This does the *same* stuff but which expression would
    >> you prefer???

    >
    >Neither. I would prefer not to use superfluous not-C. Even more than
    >that I would prefer not-C not to be discussed here.
    >
    >Richard


    Because we all know how dangerous thoughts and words can be to an
    established order.
    Kenny McCormack, Nov 29, 2005
    #10
  11. jacob navia <> writes:
    > Keith Thompson a écrit :
    >> You don't really *need* operator overloading; anything you can do
    >> with
    >> it, you can do without it (with ordinary function calls).

    >
    > This is of course true.
    >
    > It is just a matter of measure.
    >
    > Consider this:
    > qfloat a;
    > qfloat q = (sqrt(a+1)/sqrt(a-1))*(cos(a+1)/cos(a-1));
    >
    > compared with:
    >
    > tmp1 = qadd(a,1);
    > tmp2 = sqrtq(tmp1);
    > tmp3 = qsub(a,1);
    > tmp4 = sqrt(tmp3);
    > tmp5 = qdiv(tmp1,tmp3);
    > tmp6 = cosq(tmp1);
    > tmp7 = cosq(tmp3);
    > tmp8 = qdiv(tmp6,tmp7);
    > result = qmul(tmp8,tmp5);
    >
    > This does the *same* stuff but which expression would
    > you prefer???


    I prefer the one that's valid C, and that I can use on systems other
    than Win32.

    In the context of this newsgroup (now listen carefully), *C does not
    support operator overloading*. Conversely, a language that does
    support operator overloading is not C. I've used languages that do
    support operator overloading, and I've found it useful when used
    carefully -- but those languages have their own newsgroups, as does
    the lcc compiler.

    And jacob, please don't send me e-mail copies of Usenet followups.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Nov 29, 2005
    #11
  12. In article <> jacob navia <> writes:
    ....
    > Consider this:
    > qfloat a;
    > qfloat q = (sqrt(a+1)/sqrt(a-1))*(cos(a+1)/cos(a-1));
    >
    > compared with:
    >
    > tmp1 = qadd(a,1);
    > tmp2 = sqrtq(tmp1);
    > tmp3 = qsub(a,1);
    > tmp4 = sqrt(tmp3);
    > tmp5 = qdiv(tmp1,tmp3);
    > tmp6 = cosq(tmp1);
    > tmp7 = cosq(tmp3);
    > tmp8 = qdiv(tmp6,tmp7);
    > result = qmul(tmp8,tmp5);
    >
    > This does the *same* stuff but which expression would
    > you prefer???


    This is obviously a distortion of what is possible, how many of those
    tmp's do you really need?
    aplus1 = qadd(a, 1);
    asub1 = qsub(a, 1);
    result = qmul(qdiv(qsqrt(aplus1),qsqrt(asub1)),
    qdiv(qcos(aplus1),qcos(asub1)));
    which has the added advantage that it is correct.

    Does your compiler calculate a+1 and a-1 only once? I have no idea, and
    cannot check. I have no access to a win32 machine.
    --
    dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
    home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
    Dik T. Winter, Nov 30, 2005
    #12
  13. Raghu

    Eric Sosman Guest

    Dik T. Winter wrote:

    > In article <> jacob navia <> writes:
    > ...
    > > Consider this:
    > > qfloat a;
    > > qfloat q = (sqrt(a+1)/sqrt(a-1))*(cos(a+1)/cos(a-1));
    > >
    > > compared with:
    > >
    > > tmp1 = qadd(a,1);
    > > tmp2 = sqrtq(tmp1);
    > > tmp3 = qsub(a,1);
    > > tmp4 = sqrt(tmp3);
    > > tmp5 = qdiv(tmp1,tmp3);
    > > tmp6 = cosq(tmp1);
    > > tmp7 = cosq(tmp3);
    > > tmp8 = qdiv(tmp6,tmp7);
    > > result = qmul(tmp8,tmp5);
    > >
    > > This does the *same* stuff but which expression would
    > > you prefer???

    >
    > This is obviously a distortion of what is possible, how many of those
    > tmp's do you really need?
    > aplus1 = qadd(a, 1);
    > asub1 = qsub(a, 1);
    > result = qmul(qdiv(qsqrt(aplus1),qsqrt(asub1)),
    > qdiv(qcos(aplus1),qcos(asub1)));
    > which has the added advantage that it is correct.
    >
    > Does your compiler calculate a+1 and a-1 only once? I have no idea, and
    > cannot check. I have no access to a win32 machine.


    It's very old-fashioned of me, I know, but I cringe at
    code that calculates a product or quotient of square roots.

    Other spine-tinglers include products and quotients of
    power functions and exponentials, sums of logarithms, and
    trig functions applied to arctangents. All such expressions
    ought to be simplified unless there's a *very* good reason
    not to, usually having to do with extraordinary care about
    precision: sqrt(x)/sqrt(y) need not equal sqrt(x/y) exactly.
    But if that sort of thing is a problem, the code needs an
    extensive comment to explain why the expression should not
    be simplified; the default behavior should be to simplify
    such things, always.

    --
    Eric Sosman
    lid
    Eric Sosman, Nov 30, 2005
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?ISO-8859-1?Q?Christian_Brechb=FChler?=

    Enhancing valarray with "normal" arithmetic operators

    =?ISO-8859-1?Q?Christian_Brechb=FChler?=, Sep 12, 2003, in forum: C++
    Replies:
    6
    Views:
    947
    =?ISO-8859-1?Q?Christian_Brechb=FChler?=
    Sep 14, 2003
  2. Lionel B
    Replies:
    5
    Views:
    449
    Gianni Mariani
    Mar 3, 2005
  3. Trent Buck

    variadic arithmetic, boolean operators

    Trent Buck, Jan 1, 2005, in forum: C Programming
    Replies:
    3
    Views:
    399
    Mysidia
    Jan 2, 2005
  4. joshc
    Replies:
    5
    Views:
    544
    Keith Thompson
    Mar 31, 2005
  5. Calum
    Replies:
    7
    Views:
    338
    Calum
    Mar 14, 2006
Loading...

Share This Page