简体   繁体   English

无法从C ++中的多维数组检索正确的浮点值

[英]Cannot retrieve correct float value from multi-dimensional array in C++

I am porintg a script to C++ and I encountered some big problems. 我正在使用C ++编写脚本,但遇到了一些大问题。

I defined a float array with values (this is a very short one, my real array is 100k elements) like this: 我定义了一个带有值的float数组(这是一个很短的数组,我的实际数组是100k个元素),如下所示:

float gRandomPlayerSpawns[4][3] = {
    {2194.7808,1024.5272,79.5547},
    {2099.8562,1158.2679,11.6484},
    {1953.1841,1342.9954,15.3746},
    {2000.6274,1519.7140,17.0625}
};

Now, when I execute the following code: 现在,当我执行以下代码时:

void SetPlayerRandomSpawn(int playerid)
{
    int rnd = rand() % (sizeof(gRandomPlayerSpawns));
    ServerLog::Print(playerid,-1,Functions::string_format(
    "Setting position to: %f %f %f",
    gRandomPlayerSpawns[rnd][0], gRandomPlayerSpawns[rnd][1], gRandomPlayerSpawns[rnd][2]).c_str());
    SetPlayerPos(playerid, gRandomPlayerSpawns[rnd][0], gRandomPlayerSpawns[rnd][1], gRandomPlayerSpawns[rnd][2]); // Warp the player
    SetPlayerFacingAngle(playerid, 45.0);
}

I never get any value which is in the array.. always weird values, or 0,0,0, or something like this: 我从来没有得到数组中的任何值..总是很奇怪的值,或者0,0,0,或者类似这样的东西:

Setting position to: 283969270356831250000000000000.000000 18523600588218255000000000000.000000 72697250258806125000000000000000.000000

Or #INF:00000 / infinity etc.. 或#INF:00000 /无限等。

I also used the array I posted above and added f to the end of each number but it did not help, I still get undefined(?) behaviour, what am I doing wrong? 我还使用了上面发布的数组,并在每个数字的末尾添加了f ,但这并没有帮助,我仍然得到undefined(?)行为,我在做什么错呢?

int rnd = rand() % (sizeof(gRandomPlayerSpawns));

This line is wrong. 这行是错误的。 applying sizeof to this array will give you 4 * 3 * sizeof(float) which is (on my machine) 48 because a float take 4 bytes of memory. 将sizeof应用于此数组将为您提供4 * 3 * sizeof(float)(在我的机器上为48),因为float占用4个字节的内存。 Even if you were to divide by sizeof(float) you would still get 12 which is out of range for your following calculations which expect a range of [0, 4[. 即使您用sizeof(float)除,您仍然会得到12,该值超出了您的以下计算范围,期望范围为[0,4 [。

If I may suggest an alternate approach: 如果我建议替代方法:

    struct PlayerSpawn {
        float x, y, z;
    };

    // There may be a slighting cleaner way of doing this.
    std::vector<PlayerSpawn> spawnsLocations;
    {
        PlayerSpawn spawns[4] = { {2194.7808,1024.5272,79.5547},
            {2099.8562,1158.2679,11.6484},
            {1953.1841,1342.9954,15.3746},
            {2000.6274,1519.7140,17.0625}
        };
        std::copy(&spawns[0], &spawns[4], std::vector<PlayerSpawn>::emplace_back);
    }   // The static array will go out of scope here, it's somewhat of a needless optimization though
    int rand = 0 % spawnsLocations.size();  // call rand here instead of 0, size of the vector should be 4 here

But really you could add the values to the vector directly using push_back, or alternatively initializing the array with a specific size (say 4) and then assign the values to each index (from 0 to 3). 但是实际上,您可以直接使用push_back将值添加到向量,或者以特定大小初始化数组(例如4),然后将值分配给每个索引(从0到3)。 It's up to you. 由你决定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM