Thread overview | |||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
March 08, 2005 operator ** for 'power' | ||||
---|---|---|---|---|
| ||||
How about adding an operator ** for 'to the power' ? 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'. Maybe 'x**n' should simply always return a float? Definately not important, I know. L. |
March 08, 2005 Re: operator ** for 'power' | ||||
---|---|---|---|---|
| ||||
Posted in reply to Lionello Lunesu | On Tue, 8 Mar 2005 17:32:01 +0200, Lionello Lunesu <lio@lunesu.removethis.com> wrote: > How about adding an operator ** for 'to the power' ? > > 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'. > > Maybe 'x**n' should simply always return a float? > > Definately not important, I know. > > L. > > I think there's a problem disambiguating x**n - it could be 'x to the power n' or x * *n - 'x times the contents of pointer n' -- Using Opera's revolutionary e-mail client: http://www.opera.com/m2/ |
March 08, 2005 Re: operator ** for 'power' | ||||
---|---|---|---|---|
| ||||
Posted in reply to Lionello Lunesu | Lionello Lunesu wrote: > How about adding an operator ** for 'to the power' ? *Could* break some existing code.... > 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'. > > Maybe 'x**n' should simply always return a float? Not sure what to say about the return type. Possibilities: - define it by the same rules as the other arithmetic operators - return ulong if applied to unsigned integers, otherwise long if applied to integers, otherwise usual rules - return ulong if applied to unsigned integers, otherwise long if applied to integers, otherwise real - always return a real 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? Whatever we do, it should be capable of generating an exact result within the range of long/ulong (and cent/ucent when we get them). Stewart. -- My e-mail is valid but not my primary mailbox. Please keep replies on the 'group where everyone may benefit. |
March 08, 2005 Re: operator ** for 'power' | ||||
---|---|---|---|---|
| ||||
Posted in reply to Lionello Lunesu | please think about pointer syntax. "Lionello Lunesu" <lio@lunesu.removethis.com> дÈëÏûÏ¢ÐÂÎÅ:d0kghi$2meh$1@digitaldaemon.com... > How about adding an operator ** for 'to the power' ? > > 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'. > > Maybe 'x**n' should simply always return a float? > > Definately not important, I know. > > L. > |
March 09, 2005 Re: operator ** for 'power' | ||||
---|---|---|---|---|
| ||||
Posted in reply to Alex Stevenson | "Alex Stevenson" <ans104@cs.york.ac.uk> skrev i en meddelelse news:opsnbu9fmu08qma6@mjolnir.spamnet.local... >> How about adding an operator ** for 'to the power' ? > I think there's a problem disambiguating x**n - it could be 'x to the power n' or x * *n - 'x times the contents of pointer n' That is not really a problem. If wanted, the lexer could identify "**" as a power operator just as "--" is identified as a decrement operator; ie. it is no worse than: x - -n compared to x--n Regards, Martin |
March 09, 2005 Re: operator ** for 'power' | ||||
---|---|---|---|---|
| ||||
Posted in reply to Lionello Lunesu | "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 Martin M. Pedersen | "Martin M. Pedersen" <martin@moeller-pedersen.dk> wrote in message news:d0lhcv$p72$1@digitaldaemon.com... > "Alex Stevenson" <ans104@cs.york.ac.uk> skrev i en meddelelse news:opsnbu9fmu08qma6@mjolnir.spamnet.local... >>> How about adding an operator ** for 'to the power' ? >> I think there's a problem disambiguating x**n - it could be 'x to the power n' or x * *n - 'x times the contents of pointer n' > > That is not really a problem. If wanted, the lexer could identify "**" as a power operator just as "--" is identified as a decrement operator; ie. it is no worse than: > > x - -n > > compared to > > x--n > > Regards, > Martin > 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'? Making 'x**n' mean "raised to a power" would be just like giving a special meaning to 'x--n', which *would* cause confusion with 'x - -n'. |
March 09, 2005 Re: operator ** for 'power' | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nick Sabalausky | Nick Sabalausky wrote: <snip> > 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'? <snip> We'd need to either make the lexer dependent on the parser, or explicitly define AddExpression :: AddExpression -- MulExpression This suffers from the same old ambiguity for as long as the old C cast syntax remains in the language. But even when this is got rid of, a -- * b could mean either (a--) * b or a -- (*b) Moreover, why would one want to write 'x--n' in preference to 'x+n'? And even if a class defines them to be different, it goes without saying that you'll need a space. 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 Stewart Gordon | 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). > 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. L. |
March 09, 2005 Re: operator ** for 'power' | ||||
---|---|---|---|---|
| ||||
Posted in reply to uframer | "uframer" <uframer@sina100.com.cn> wrote in message news:d0kns7$2uhm$1@digitaldaemon.com... > please think about pointer syntax. Yes, you're right, but 'n**x' can't really be interpreted any other way, since you'll get an error if 'x' is a pointer ('can't raise to the power of a pointer') L. |
Copyright © 1999-2021 by the D Language Foundation