March 09, 2005 Re: operator ** for 'power' | ||||
---|---|---|---|---|
| ||||
Posted in reply to Martin M. Pedersen | I KNEW I'd get at least one such response :-) And yeah, you're right. In fact, I got the ** idea when I was defining some constant, 1024*1024, and thought 'it would be nice to do 1024**2' and then thought 'I should simply write 1048576' closely followed by the though 'I should write 2<<20'.. L. "Martin M. Pedersen" <martin@moeller-pedersen.dk> wrote in message news:d0lkto$sil$1@digitaldaemon.com... > "Lionello Lunesu" <lio@lunesu.removethis.com> skrev i en meddelelse news:d0kghi$2meh$1@digitaldaemon.com... >> I know there's no single CPU instruction to do 'x to the power n' with integers (only floats, right?) but it seems silly that you need a library function to be able to do 'to the power'. > > Real programmers think binary and does not need any other power operator than '<<' :-) > > Regards, > Martin > > |
March 09, 2005 Re: operator ** for 'power' | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nick Sabalausky | "Nick Sabalausky" <z@a.a> skrev i en meddelelse news:d0n2rd$2e6b$1@digitaldaemon.com... >> x - -n >> compared to >> x--n > Is that really a valid analogy, though? 'x--n' is an invalid way of expressing the decrement operator (it amounts to either '(x--)n' or 'x(--n)', both of which are invalid), so wouldn't that make it easier for the compiler to determine that it means 'x - -n'? It is perfectly good analogy. '--' is a token in its own right, and there is no way you can convince the compiler that you mean binary-substract + unary-minus using it. This is a consequence of the "maximal munch technique" described at http://www.digitalmars.com/d/lex.html - the same technique also emplyed by C and C++ compilers. It you look at http://www.digitalmars.com/d/expression.html#UnaryExpression you will see, that unary-minus ('-') and dereference ('*') are placed just besides each other in the grammer. Likewise, binary-substract and binary-multiply are placed almost at the same level in the grammer - just a slight difference to give multiply a slightly higher priority than substract. So, it is basically the same, and we have already lived happily with the ambiguity for decades. Regards, Martin |
March 09, 2005 Re: operator ** for 'power' | ||||
---|---|---|---|---|
| ||||
Posted in reply to Lionello Lunesu | Lionello Lunesu wrote: > Hey, > > >>>How about adding an operator ** for 'to the power' ? >> >>*Could* break some existing code.... > > > Not really, n**x with 'x' being a pointer to a float/int would 'cause a compile error, complaining that you can't raise 'n' to the power of 'a pointer'. And a new feature causing an error doesnot really break existing code, since you'll have to fix the incompatibility (using the hint in the error message). Before you consider operator overloads.... >> Of course, exponentiating integers doesn't produce an integer if the exponent is negative. But we discard the fractional part with integer division, so why not? > > That's right. Good point. The thing is that in code it will always use 'fpow' meaning that'll always return a float/real internally and then cast them to integers if called on integers. Might need a performance warning. Always? We certainly shouldn't have the loss of precision on integers. For this to work, every long and ulong would need to be expressible exactly as a real - I'm not sure if this is true of the 80-bit reals, but it won't be true on platforms where real is only 64-bit. Moreover, are the platforms on which we will support cent going to have a 160-bit real to be a superset of it? Stewart. -- My e-mail is valid but not my primary mailbox. Please keep replies on the 'group where everyone may benefit. |
March 09, 2005 Re: operator ** for 'power' | ||||
---|---|---|---|---|
| ||||
Posted in reply to Martin M. Pedersen | "Martin M. Pedersen" <martin@moeller-pedersen.dk> skrev i en meddelelse news:d0n7uk$2kj4$1@digitaldaemon.com... > So, it is basically the same, and we have already lived happily with the ambiguity for decades. Now I think of it, it might introduce slight problems after all. Noone uses two successive '-' meaning unary-minus + unary-minus, simply because it would be a silly expression to write. Therefore, there is no conflict with '--' as decrement. However, two successive '*' meaning double dereference is quite common, and introducing '**' would break existing code. The fix is easy though - just insert a space. Regards, Martin |
March 09, 2005 Re: operator ** for 'power' | ||||
---|---|---|---|---|
| ||||
Posted in reply to Martin M. Pedersen | "Martin M. Pedersen" <martin@moeller-pedersen.dk> wrote in message news:d0lkto$sil$1@digitaldaemon.com... > "Lionello Lunesu" <lio@lunesu.removethis.com> skrev i en meddelelse news:d0kghi$2meh$1@digitaldaemon.com... >> I know there's no single CPU instruction to do 'x to the power n' with integers (only floats, right?) but it seems silly that you need a library function to be able to do 'to the power'. > > Real programmers think binary and does not need any other power operator than '<<' :-) > > Regards, > Martin > I'm aware of the multipy/divide tricks with bitshifting, but could you enlighten me as to how that power one works? |
March 09, 2005 Re: operator ** for 'power' | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nick Sabalausky | Nick Sabalausky wrote:
>>>I know there's no single CPU instruction to do 'x to the power n' with integers (only floats, right?) but it seems silly that you need a library function to be able to do 'to the power'.
>>
>>Real programmers think binary and does not need any other power operator than '<<' :-)
>>
> I'm aware of the multipy/divide tricks with bitshifting, but could you enlighten me as to how that power one works?
assert(x == 2); ? ;-)
--anders
|
March 09, 2005 Re: operator ** for 'power' | ||||
---|---|---|---|---|
| ||||
Posted in reply to Lionello Lunesu | "Lionello Lunesu" <lio@lunesu.removethis.com> skrev i en meddelelse news:d0n7nn$2kc7$1@digitaldaemon.com... >I KNEW I'd get at least one such response :-) And yeah, you're right. > In fact, I got the ** idea when I was defining some constant, 1024*1024, and thought 'it would be nice to do 1024**2' and then thought 'I should simply write 1048576' closely followed by the though 'I should write 2<<20'.. Now I see why '**' would have some value to you :-) Regards, Martin |
March 09, 2005 Re: operator ** for 'power' | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nick Sabalausky | "Nick Sabalausky" <z@a.a> skrev i en meddelelse news:d0n8n5$2lek$1@digitaldaemon.com... I'm aware of the multipy/divide tricks with bitshifting, but could you > enlighten me as to how that power one works? 2 ** n is the same as 1 << n At least if 'n' is a non-negative integer. It would be interesting to have fractional shifts :-) Regards, Martin |
March 09, 2005 Re: operator ** for 'power' | ||||
---|---|---|---|---|
| ||||
Posted in reply to Martin M. Pedersen | "Martin M. Pedersen" <martin@moeller-pedersen.dk> wrote in message news:d0n9r3$2ml4$1@digitaldaemon.com... > "Nick Sabalausky" <z@a.a> skrev i en meddelelse > news:d0n8n5$2lek$1@digitaldaemon.com... > I'm aware of the multipy/divide tricks with bitshifting, but could you >> enlighten me as to how that power one works? > > 2 ** n > > is the same as > > 1 << n > > At least if 'n' is a non-negative integer. It would be interesting to have fractional shifts :-) > > Regards, > Martin > Oh, lol, for some reason I was confusing left-shift and right-shift. It makes sense now ;) |
March 09, 2005 Re: operator ** for 'power' | ||||
---|---|---|---|---|
| ||||
Posted in reply to Martin M. Pedersen | (nothing)**(pointer) would mean a double dereference, but (identifier)**(identifier) can't possibly be anything else than 'to the power' ? Or doesn't the parsing work this way? Probably not context-free this way, right? L. |
Copyright © 1999-2021 by the D Language Foundation