help for problem of module with "%"
Author Message
help for problem of module with "%"

Hi experts,

This is question that really bothered for some time when I use hashing
functions.

I have a data with 13 digits(bad luck number!), and I read it in as
"double"
Since "%" require integer, so I casted the double to integer by (int)
a%b.

It seemed working, but not proper. I then treated 13 digits as
characters and the results seems much better.

Who can tell me if double could be use like double%int, i.e. in a
hashing function?

--

Fri, 31 Jan 2003 03:00:00 GMT
help for problem of module with "%"

Quote:

> This is question that really bothered for some time when I use hashing
> functions.
> I have a data with 13 digits(bad luck number!), and I read it in as
> "double"
> Since "%" require integer, so I casted the double to integer by (int)
> a%b.
> It seemed working, but not proper. I then treated 13 digits as
> characters and the results seems much better.
> Who can tell me if double could be use like double%int, i.e. in a
> hashing function?

First of all, % can be applied to floating values.
However, % is *not* a "hashing" operator; it's a "remainder"
operator (modulo operation, for nonnegative operands).
The key to understanding your situation is to understand
how integer remainder can be used in a hashing scheme, and
indeed to understand what the purpose of hashing is; once
you fully understand that, you should be able to figure out
whether and how the % operator could be used in hashing
quantities of types other than integer types.
--

Sat, 01 Feb 2003 12:38:56 GMT
help for problem of module with "%"

Quote:

>I have a data with 13 digits(bad luck number!), and I read it in as
>"double"
>Since "%" require integer, so I casted the double to integer by (int)
>a%b.

>It seemed working, but not proper. I then treated 13 digits as
>characters and the results seems much better.

>Who can tell me if double could be use like double%int, i.e. in a
>hashing function?

You should look at the fmod() function.

But in many implementations, a double has less than 13
significant digits, so this approach will not work for your problem.

-- Gary Culp
--

Sat, 01 Feb 2003 03:00:00 GMT
help for problem of module with "%"

Quote:

> Hi experts,

> This is question that really bothered for some time when I use hashing
> functions.

> I have a data with 13 digits(bad luck number!), and I read it in as
> "double"
> Since "%" require integer, so I casted the double to integer by (int)
> a%b.

> It seemed working, but not proper. I then treated 13 digits as
> characters and the results seems much better.

> Who can tell me if double could be use like double%int, i.e. in a
> hashing function?

There are a few issues here.

AFAIK, without having the standard in front of me, the % operator
can't be applied to floating point operands.  The math library
provides a function named fmod to compute floating point
remainders.

a particularly good idea, as floating point gives you
larger range of values by offering less precision of
larger values.

The reason loading 13 digits as an int is probably failing is
that an int is typically 16 or 32 bits (roughly 5 and 10 digits
respectively).  Reading a 13 digit value into a 10 digit variable
is almost guaranteed to give surprising behaviour as well.
--

Sat, 01 Feb 2003 03:00:00 GMT
help for problem of module with "%"

Quote:
> Hi experts,

> This is question that really bothered for some time when I use hashing
> functions.

> I have a data with 13 digits(bad luck number!), and I read it in as
> "double"
> Since "%" require integer, so I casted the double to integer by (int)
> a%b.

> It seemed working, but not proper. I then treated 13 digits as
> characters and the results seems much better.

Reading the digits into a character array should be fine, it's
just that you'd need a different hashing function than a plain old %.

For example, you could add all the digits together, with something like:

int hash (char *digits, int buckets)
{
int i;
int sum = 0;

for (i=0; i < 13 && digits[i]; ++i)
sum += digits[i];

return sum % buckets;

Quote:
}

--

Sun, 02 Feb 2003 03:00:00 GMT
help for problem of module with "%"

Quote:

> AFAIK, without having the standard in front of me, the % operator
> can't be applied to floating point operands.  The math library
> provides a function named fmod to compute floating point
> remainders.

That's correct.  I think in an earlier response I said that
% could be used with real-floating operands, but that would
be an extension that is not promised by the standard.
--

Sun, 09 Feb 2003 08:41:07 GMT

 Page 1 of 1 [ 6 post ]

Relevant Pages