Skip to main content
GameDev.net gamedev.net
🔒 Locked

Exponent Operator

Started by Rain Dog Apr 7, 2005 at 11:33 AM 11 replies 5.5k views
Original Post
Rain Dog
Rain Dog
How about the ^ operator to act as the powerof. 2^2 = 4 2^5 = 32 etc.
Washu
Washu
Because the ^ operator is the bitwise xor operator.
In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.
Fruny
Fruny
Well, then how about ** as the exponent operator? [grin]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
WitchLord
WitchLord
It would certainly be possible. But is it worth it?
AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - <a href="http://www.angelcode.com/tower" rel
Washu
Washu
Possible? Sure...

But when you consider the fact that the majority of the operators on built in types operate in constant time , a power operator, which not have a constant running time (except for some types), really doesn't make any sense.

Not to mention readability, the ^ operator is about the only one i would recognize as a power operator, ** would just be plain confusing. But if you're interoperating between C++ and AngleScript, ^ could become confusing very quickly. Something that won't happen with pow(x, y).
In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.
WitchLord
WitchLord
I agree with you Washu.

The ^ operator is already taken, otherwise that would be the ideal one for the power-of operation. ** have been used by other languages, and can be used in AngelScript, but it might confuse C/C++ programmer (a large part of the AngelScript users). Also a power-of operator isn't used very often thus it's not too much to ask to make the programmer call a function instead.

The case where the power-of operator is used the most is for the base 2, in which case the << (shift left) operator works just as well, if not better.

I don't think I'll implement the power-of operator unless someone can come up with a really good reason for it.
AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - <a href="http://www.angelcode.com/tower" rel
Fruny
Fruny
Quote:
Original post by WitchLord
I don't think I'll implement the power-of operator unless someone can come up with a really good reason for it.


Go ahead and implement arbitrary user-defined infix operators. [grin]

banana(x,y) pales in front of x banana y.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
WitchLord
WitchLord
That would be an option, except that it reserves keywords that might be used in other locations.
AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - <a href="http://www.angelcode.com/tower" rel
Rain Dog
Rain Dog
Oh yeah. I completely spaced that.

I didnt see the ^ in the list of operators, i must have just missed it.


And Washu, Multiplication is not constant time. It is repeated addition, and while it may be *fast* it is not constant time.

I brought this up because I have been converting a LUA script to AS and there is used a lot the ^ operator for powers.

Something like this: x pow 1456 is easier in my opinion to code than pow(x, 1456)

The only reason i proposed the operator was because in my initial skimming i did not see it currently being used.
Fruny
Fruny
Quote:
Original post by Rain Dog
Something like this: x pow 1456 is easier in my opinion to code than pow(x, 1456)


My mention of arbitrary infix operators was a joke. They're not fun to implement. And pow(x,1456) will be more familiar to most than x pow 1456...
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
WitchLord
WitchLord
Rain dog:

Actually, multiplication is made in constant time. I don't remember the exact mechanics that the ALU units use at the moment (it's been 7 years since I took the course in computer architecture), but I can assure you that it's not a series of adds. I believe MUL is only 1 clock cycle on the x86 processors.

Division, however, is not constant time, as it is repeated until enough precision has been reached. The more precision you need the longer it takes.

If I were to implement a power-of operator, I think I would indeed go with x pow y, as it is easier to remember than x ** y. I already have a few operators like that (and, or, and xor) so it won't be difficult to implement.

Fruny:

You actually gave me the idea to allow the applications to dynamically register operators. It would then be possible to register operators like 'pow', 'cat', and 'mod', as needed. It is an interesting idea, but I don't think I'll implement it, I'm already trying to simplify the library. [wink]

[Edited by - WitchLord on April 10, 2005 10:08:50 AM]
AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - <a href="http://www.angelcode.com/tower" rel
Rain Dog
Rain Dog
Usage:  MUL     src        Modifies flags: CF OF (AF,PF,SF,ZF undefined)        Unsigned multiply of the accumulator by the source.  If "src" is        a byte value, then AL is used as the other multiplicand and the        result is placed in AX.  If "src" is a word value, then AX is        multiplied by "src" and DX:AX receives the result.  If "src" is        a double word value, then EAX is multiplied by "src" and EDX:EAX        receives the result.  The 386+ uses an early out algorithm which        makes multiplying any size value in EAX as fast as in the 8 or 16        bit registers.                                 Clocks                 Size        Operands         808x  286   386   486          Bytes        reg8            70-77   13   9-14  13-18          2        reg16          118-113  21   9-22  13-26          2        reg32             -     -    9-38  13-42         2-4        mem8        (76-83)+EA  16  12-17  13-18         2-4        mem16     (124-139)+EA  24  12-25  13-26         2-4        mem32             -     -   12-21  13-42         2-4        F6 /4 MUL r/m8 Unsigned multiply (AX ¬ AL * r/m8)        F7 /4 MUL r/m16 Unsigned multiply (DX:AX ¬ AX * r/m16)        F7 /4 MUL r/m32 Unsigned multiply (EDX:EAX ¬ EAX * r/m32)




That is taken from the MASM32 OPCODE help file.

Compared with ADD,

ADD - Arithmetic Addition        Usage:  ADD     dest,src        Modifies flags: AF CF OF PF SF ZF        Adds "src" to "dest" and replacing the original contents of "dest".        Both operands are binary.                                 Clocks                 Size        Operands         808x  286   386   486          Bytes        reg,reg           3     2     2     1             2        mem,reg         16+EA   7     7     3            2-4  (W88=24+EA)        reg,mem          9+EA   7     6     2            2-4  (W88=13+EA)        reg,immed         4     3     2     1            3-4        mem,immed       17+EA   7     7     3            3-6  (W88=23+EA)        accum,immed       4     3     2     1            2-3



It does appear that MUL is varying in clock cycles.

[Edited by - Rain Dog on April 9, 2005 4:14:27 PM]
WitchLord
WitchLord
I take back what I said. Thanks for showing me the truth! [smile]
AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - <a href="http://www.angelcode.com/tower" rel

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.