Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/209.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我的函数每次返回一个不同的值,即使没有随机值_Java_Android_Corruption - Fatal编程技术网

Java 我的函数每次返回一个不同的值,即使没有随机值

Java 我的函数每次返回一个不同的值,即使没有随机值,java,android,corruption,Java,Android,Corruption,我有以下代码。它用于计算OpenGL内部使用的纹理位置,但是,这并不重要。它第一次就可以完美地工作,但只有第一次 public static float[] createTexturePositions(final int width, final int height) { float[] positions = new float[width * height * 12]; // Range: 0 - 1 float dx = 1f / width; flo

我有以下代码。它用于计算OpenGL内部使用的纹理位置,但是,这并不重要。它第一次就可以完美地工作,但只有第一次

public static float[] createTexturePositions(final int width, final int height)
{
    float[] positions = new float[width * height * 12];

    // Range: 0 - 1
    float dx = 1f / width;
    float dy = 1f / height;

    float y = 0f;

    int i = 0;

    for (int indexY = 0; indexY < height; indexY++)
    {
        float x = 0f;

        for (int indexX = 0; indexX < width; indexX++)
        {
            float top    = y;
            float left   = x;
            float right  = x + dx;
            float bottom = y + dy;

            // p1   p2
            // p3

            positions[i     ] = left;
            positions[i +  1] = top;

            positions[i +  2] = right;
            positions[i +  3] = top;

            positions[i +  4] = left;
            positions[i +  5] = bottom;

            //      p4
            // p5   p6

            positions[i +  6] = right;
            positions[i +  7] = top;

            positions[i +  8] = left;
            positions[i +  9] = bottom;

            positions[i + 10] = right;
            positions[i + 11] = bottom;

            x += dx;
            i += 12;
        }

        y += dy;
    }

    return positions;
}
publicstaticfloat[]createTexturePositions(最终整型宽度,最终整型高度)
{
浮动[]位置=新浮动[宽度*高度*12];
//范围:0-1
浮动dx=1f/宽度;
浮子dy=1f/高度;
浮动y=0f;
int i=0;
对于(int-indexY=0;indexY
现在,所发生的一切毫无意义。 -即使输入完全相同,此函数也会返回不同的值。 -错误是随机发生的 -错误不会在第一次出现,但通常在第四次呼叫前后,数据与前一次呼叫不匹配。 -数值相差很大,这里我得到:指数I=565时,0.85714287对1.1417698E35。我的输入值是7,7。 -在上述情况下,第二个值是无意义的(e^35)。但这是刚刚从零开始产生的价值。也就是说,它不应该被修改。 -我只返回数组的副本,从而确保被测试的是我认为是的那个!。 -舍入误差应该无关紧要,因为它们每次都应该是相同的,即使它们不同,它们的值至少应该是相似的

在我看来,这似乎是一个数据损坏问题。好像有人或什么东西在修改我的数据!这里是否存在某种堆栈冲突问题或一些我不知道的奇怪情况

更新1:

我发现添加以下代码:

            testValue( top    );
            testValue( left   );
            testValue( right  );
            testValue( bottom );

private static void testValue(float value)
{
    if (value < 0f || value > 1.01f)    throw new RuntimeException("Error! Value is Invalid");
}
testValue(顶部);
testValue(左);
testValue(右);
测试值(底部);
私有静态void测试值(浮点值)
{
如果(值<0f | |值>1.01f)抛出新的运行时异常(“错误!值无效”);
}
防止问题发生!testValue函数当然放在原始函数之后,对testValue的调用直接发生在确定值之后

更新2:


我可以通过注释/取消注释testValue代码来打开/关闭问题。再一次,这是没有意义的,我想我会把这段代码留在那里,因为它正在为我解决问题

听起来问题不在于函数本身,而在于如何使用它。如果您想确保,为什么不每次从函数内部打印位置[565]。如果您在第四次调用时看到它发生了变化,那么它就是函数。否则,看看其他地方。我已经检查了20次,但值是相同的。。。其0.85714287位于565位置。我确信这是你的代码错误…我知道,这毫无意义。请检查我的更新。我可以通过尝试在源代码处调试一个无意义的错误来停止这个问题。。。对我来说毫无意义。