英文:
Why is C confusing my multiplicative operations with pointer assignment?
问题
int hashedValue = n * ((int)((k * A%1)));
被标记为指针错误expression must have an integral type
。但我不理解这个错误,因为*
与后缀而不是前缀操作一起使用。请帮忙解释。谢谢!
英文:
int multiplicationMethod(int k, int n)
{
if(n > SIZE || n < SIZE)
{
printf("IndexOutOfBounds Error. Please Try different slot number.");
}
else
{
int N = rand();
float A = N/RAND_MAX;
int hashedValue = n * ((int)((k * A%1)));
HashTable[hashedValue] = k;
return hashedValue;
}
}
int hashedValue = n * ((int)((k * A%1)));
is being tagged with a pointer error expression must have an integral type
I don't understand this though because the *
is used with the post NOT prefix operation. Please help. Thanks!
答案1
得分: 3
k * A%1
:
你不能这样做。A 是一个浮点数;而 % 只接受整数参数。
这对于 C 语言来说是个尴尬之处。对于这个限制,现在已经没有好的理由了。只需在遇到这个运算符时自动调用 fmod
。
应该写成 k * fmod(A, 1)
。
正如评论中指出的,你需要将 float A = N/RAND_MAX;
改为 float A = N/(float)RAND_MAX;
才能使你的代码正常工作;否则 A 将永远为 0。
英文:
k * A%1
You can't do that. A is a float; and % only takes integer arguments.
This is an embarrassment to C. There's no good for this restriction anymore. Just auto-call fmod
when you see that operator.
Should read k * fmod(A, 1)
As pointed out in comments, you need to change float A = N/RAND_MAX;
to float A = N/(float)RAND_MAX;
for your code to work; otherwise A will always be 0.
答案2
得分: 0
srand((unsigned int)time(NULL));
float A = (float)(rand()) / (float)(RAND_MAX);
int B = 1;
int hashedValue = (int)((n * (k * (modf(A, &B)))));
HashTable[hashedValue] = k;
"srand() 生成一个随机种子,以在每次调用方法时生成一个随机数。rand() 方法中的随机整数生成原本产生一个整数,但被强制转换为浮点数,然后除以一个极大的浮点数。这样的结果是一个介于 0 和 1 之间的随机浮点数。
接下来的过程相对容易理解。但需要注意的是,modf 解析了浮点数 A 和整数 B 的内存地址。"
<details>
<summary>英文:</summary>
srand((unsigned int)time(NULL));
float A = (float)(rand()) / (float)(RAND_MAX);
int B = 1;
int hashedValue = (int)((n* (k* (modf(A,&B)))));
HashTable[hashedValue] = k;
sreand() generates a random seed to generate a random number upon each subsequent call to method. Rand int generation in rand() method originally produces an int but is typecasted to a float and then divided by an incredibly large float. The result of this is a random float between 0 and 1.
The next process is relatively self-explanatory. However, it is important to note that modf parses the float a and the memory address to B.
</details>
# 答案3
**得分**: -3
根据DaveS的说法,当尝试计算浮点数的模数时,需要使用fmod()而不是%取模运算符。
以下是您的示例代码,经过一些调整。需要指出的重要事项是,您需要非常小心您的命名约定,使用单个字母字符很容易,但在循环之外使用会影响代码的可读性。此外,方法名称应该具有描述性,"multiplicationMethod" 并没有提供关于方法实际功能的真正信息。
```cpp
#include <iostream>
#include <unordered_map>
#include <math.h>
const int SIZE = 20;
class TestClass
{
std::unordered_map<int, std::string> hashTable;
public:
int multiplicationMethod(int value, int slotNum)
{
if (slotNum > SIZE)
{
printf("IndexOutOfBounds 错误。请尝试不同的槽号。");
}
else
{
int N = rand();
float A = N / RAND_MAX;
int hashedValue = slotNum * ((int)(value * fmod(A, 1)));
hashTable[hashedValue] = value;
printf(std::to_string(hashedValue).c_str());
return hashedValue;
}
}
};
int main()
{
TestClass t;
t.multiplicationMethod(4, 4);
}
希望这有助于您理解代码的内容。
英文:
As DaveS stated you need to use fmod() rather than the % modulo operator when trying to work out the modulus of a float.
Here is your example with some tweaks. Big thing I have to point out is that you need to be very careful with your naming conventions it is easy to use one letter characters but outside of a loop it will affect the readability of your code. Also method names should be descriptive multiplicationMethod gives no real information as to what the method actually does.
#include <iostream>
#include <unordered_map>
#include <math.h>
const int SIZE = 20;
class TestClass
{
std::unordered_map<int, std::string> hashTable;
public:
int multiplicationMethod(int value,int slotNum)
{
if(slotNum > SIZE)
{
printf("IndexOutOfBounds Error. Please Try different slot number.");
}
else
{
int N = rand();
float A = N/RAND_MAX;
int hashedValue = slotNum * ((int)(value * fmod(A,1)));
hashTable[hashedValue] = value;
printf(std::to_string(hashedValue).c_str());
return hashedValue;
}
}
};
int main()
{
TestClass t;
t.multiplicationMethod(4,4);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论