www.digitalmars.com         C & C++   DMDScript  

D - How does one do exponentiation?

reply Charles Hixson <charleshixsn earthlink.net> writes:
The only way I can see is via a log transform, and that seems clumsy.

     ----

Alternative question:  How does one return the hash value of a 
string?  data.toHash doesn't work, and neither does 
string.getHash(data).  I suspect one working answer will turn out to 
be something like:
   cast(Object [])data.toHash

(I haven't yet tried that variation), but it seems like it should 
have a simpler answer.  So what's the *proper* way to do it?
Oct 02 2003
next sibling parent Maik Zumstrull <Maik.Zumstrull gmx.de> writes:
Charles Hixson schrieb:

 The only way I can see is via a log transform, and that seems clumsy.
D doesn't seem to have an operator for it, and it's not in the math library. (Or: I'm really dumb and overlooked it in the docs.) Maybe you can work with this: version (standalone) { import conv; import string; import stream; } bool isOdd(uint i) { return (i & 1) == 1; } uint exp(uint b, uint e) { if (e == 0) return 1; int r = 1; int y = b; while (e > 1) { if (isOdd(e)) r *= y; e = e >> 1; y *= y; } r *= y; return r; } version (standalone) { int main(char[][] args) { stream.stdout.writeLine(toString(exp(toUint(args[1]),toUint(args[2])))); return 0; } }
Oct 02 2003
prev sibling next sibling parent reply "Vathix" <vathix dprogramming.com> writes:
"Charles Hixson" <charleshixsn earthlink.net> wrote in message
news:blib9f$ke2$1 digitaldaemon.com...
 Alternative question:  How does one return the hash value of a
 string?
This seems to be the only standard way: import stream; uint dataHash = (new MemoryStream(cast(ubyte[])data)).toHash() Note: if data is a string literal and you don't want to use wchar's, cast it to char[] first.
Oct 02 2003
parent Charles Hixson <charleshixsn earthlink.net> writes:
Vathix wrote:
 "Charles Hixson" <charleshixsn earthlink.net> wrote in message
 news:blib9f$ke2$1 digitaldaemon.com...
 
Alternative question:  How does one return the hash value of a
string?
This seems to be the only standard way: import stream; uint dataHash = (new MemoryStream(cast(ubyte[])data)).toHash() Note: if data is a string literal and you don't want to use wchar's, cast it to char[] first.
Thanks. Considering the periginations I would appearantly have to go through to use exponentiation, that saves a bunch of trouble.
Oct 02 2003
prev sibling next sibling parent Helmut Leitner <helmut.leitner chello.at> writes:
Charles Hixson wrote:
 
 The only way I can see is via a log transform, and that seems clumsy.
 
Perhaps something like: import math; double x,y,z; z=pow(x,y); (untested) -- Helmut Leitner leitner hls.via.at Graz, Austria www.hls-software.com
Oct 03 2003
prev sibling parent reply Olaf Rogalsky <olaf.rogalsky theorie1.physik.uni-erlangen.de> writes:
Charles Hixson wrote:
 
 The only way I can see is via a log transform, and that seems clumsy.
extern (C) double pow(double b, double e); then link with -lm. -- +----------------------------------------------------------------------+ I Dr. Olaf Rogalsky Institut f. Theo. Physik I I I Tel.: 09131 8528440 Univ. Erlangen-Nuernberg I I Fax.: 09131 8528444 Staudtstrasse 7 B3 I I rogalsky theorie1.physik.uni-erlangen.de D-91058 Erlangen I +----------------------------------------------------------------------+
Oct 06 2003
parent reply "Sean L. Palmer" <palmer.sean verizon.net> writes:
Ideally we wouldn't have to depend on the C runtime library at all.

Sean

"Olaf Rogalsky" <olaf.rogalsky theorie1.physik.uni-erlangen.de> wrote in
message news:3F81544F.A2386DA9 theorie1.physik.uni-erlangen.de...
 extern (C) double pow(double b, double e);
Oct 06 2003
parent Olaf Rogalsky <olaf.rogalsky theorie1.physik.uni-erlangen.de> writes:
"Sean L. Palmer" wrote:
 
 Ideally we wouldn't have to depend on the C runtime library at all.
Agreed! Personally I would like to see an internal '**' power operator, overloaded for the basic numeric types. Why internal? `cause then the compiler could optimize constant expressions. Why a new '**' operator? Why not. It's concise, and eyecatching. The greatest disadvantage in the lisp family of languages is not having too many parentheses (for experienced programmers they become opaque), but the uniform syntax. Machines are good at parsing a language with uniform syntax, humans are not! Olaf -- +----------------------------------------------------------------------+ I Dr. Olaf Rogalsky Institut f. Theo. Physik I I I Tel.: 09131 8528440 Univ. Erlangen-Nuernberg I I Fax.: 09131 8528444 Staudtstrasse 7 B3 I I rogalsky theorie1.physik.uni-erlangen.de D-91058 Erlangen I +----------------------------------------------------------------------+
Oct 07 2003