C中的数组索引,它必须是整数吗?可以是浮点数吗?
正如标题所示...我需要使用浮点作为数组索引,但 GCC 编译器吐出一个错误抱怨。
基本上我有一个数学函数 F(x,t),其中该函数具有变量 x 和 t。我遇到的问题是我试图增加 float 类型中的 x 和 t,以便我可以在不同的 x 和 t 处计算函数的不同值。所以很自然地我会有两个 for 循环:
for (x = x_min; x < x_max; x += dx) {
for (t = t_min; t < t_min; t += dt) {
f[x][t] = 10*x + 10*t; // Over simplified.. but you get the idea
}
}
// And then perform some fourier transform
fft(f[x][t], 256, 1);
所以是的,这就是为什么我想知道是否可以获取浮点作为数组索引。
as the title suggests... I need to use floating points as array indexes, but the GCC compiler spits out an error complaining.
Basically I have a mathematical function say F(x,t) where the function has variables x and t. The problem I'm having is that I'm trying to increment x and t in type float, so that I can calculate different values for the function at different x, and t. So naturally I would have two for loops:
for (x = x_min; x < x_max; x += dx) {
for (t = t_min; t < t_min; t += dt) {
f[x][t] = 10*x + 10*t; // Over simplified.. but you get the idea
}
}
// And then perform some fourier transform
fft(f[x][t], 256, 1);
So yea, that is why I was wondering if it is possible to get floating points as the array index.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
是的,它必须是一个整数,因为您本质上是在执行指针算术,其中
&array[0]
是指向数组开头的指针(嗯,从技术上讲,它必须是< /em> 一个整数,因为规范就是这么说的,但这就是原因)。在这种情况下,从基指针向上移动对象大小的一部分是没有意义的。您几乎可以保证不会指向元素的开头。
这样看:
既然我一开始就觉得这是一个奇怪的问题,也许您可以向我们提供更多有关您实际想要完成的任务的信息,而不是您提出的解决方案?在这种情况下,我们或许可以为您提供更有帮助的答案。
Yes, it has to be an integer because you are essentially performing pointer arithmetic where
&array[0]
is a pointer to the beginning of the array (well, technically it has to be an integer because that's what the spec says, but this is why).In that context it makes no sense to move from the base pointer up some fraction of the size of an object. You are pretty much guaranteeing that you won't be pointing to the beginning of an element.
Look at it this way:
Since this strikes me as an odd question to begin with, perhaps you could give us more information regarding what you are actually trying to accomplish rather than your proposed solution? We can probably give you more helpful answers in that case.
如果您只是将整数存储在浮点变量中,则将值转换或以其他方式转换为整数类型应该可以正常工作。例如:
如果你真的想用非整数索引来索引一个数组,你将不得不自己设计一个更高级别的数据结构,并且它可能不会是一个时间意义上的“数组” -效率特性。
If you're just storing whole numbers in floating point variables, casting or otherwise converting the values to an integer type should work just fine. For instance:
If you really want to index an array with non-integral indices, you're going to have to design yourself a higher-level data structure, and it will probably not be an "array" in the sense of its time-efficiency properties.
是的。来自 C99 标准 §6.5.2.1(数组下标):
如果要使用浮点数作为数组索引,则需要将其转换为整数。这通常是一个坏主意,因为计算过程中任何轻微的舍入错误都可能很容易导致数组索引在截断后偏移 1。
Yes. From the C99 standard §6.5.2.1 (Array Subscripting):
If you want to use a floating-point number as an array index, you need to cast it to an integer. This is often a bad idea, because any slight rounding errors during your calculations could easily result in the array index being off by 1 after the truncation.
正如您所发现的,数组索引必须是整型。为了达到您想要的效果,您可以按浮点增量缩放和偏移整数索引:
As you have discovered, the array indices must be integral types. To achieve the effect you want, you can scale and offset the integer index by the floating point deltas:
C 中的数组索引必须是整数
Array indices in C must be integral
根据您真正想要执行的操作,您可能可以使用浮点数的缩放偏移版本作为“索引”:
Depending on what it is you really want to do, you might be able to use a scaled an offset version of your float as an "index":
是的,我们可以,但是每个存储字节将花费 64 KiByte 内存,
浮点数存储在 4 个字节 = 32 位中,
您可以使用整数指针“读取”它。
不转换/转换/舍入为整数,而是“读取”原始字节。
在c代码中:
使用所有浮点数作为数组索引,
我们需要 2**32 Byte = 4 GiByte 的内存
来减少内存使用,我们可以使用
bfloat16
类型,这只是一个“截断”版本(有损压缩)
32 位浮点数,
只有 7 位尾数:
bfloat16
开始大肆宣传“机器学习”,但也用于传感器数据。
一些奇特的处理器具有对 bfloat16 的硬件支持。
float32 和 bfloat16 之间的转换很简单,
只是由于不同的字节顺序(字节顺序)而变得复杂,
bfloat16.c
每个字节 64 KiByte 太多了吗?
我们可以将键空间减少为“小的正浮点数”
但我们需要边界检查来防止段错误。
yes we can, but it will cost 64 KiByte memory per stored byte
a float is stored in 4 bytes = 32 bits,
which you can "read" with an integer pointer.
not convert/cast/round to integer, but "read" the raw bytes.
in c code:
to use all floats as array index,
we would need 2**32 Byte = 4 GiByte of memory
to reduce memory use, we can use the
bfloat16
type,which is simply a "truncated" version (lossy compression)
of the 32 bit float,
with only 7 bit mantissa:
bfloat16
came to hype with "machine learning",but is also used for sensor data.
some exotic processors have hardware support for bfloat16.
the conversion between float32 and bfloat16 is trivial,
only complicated by different byte order (endianness)
bfloat16.c
64 KiByte per Byte is too much?
we can reduce the key space to "small positive floats"
but then we need boundary checks to prevent segfaults.