Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.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 浮动罐';不能超过256?_Java_Lwjgl - Fatal编程技术网

Java 浮动罐';不能超过256?

Java 浮动罐';不能超过256?,java,lwjgl,Java,Lwjgl,我在做一个游戏,在游戏中我用一个浮点数设置一个位置,问题是浮点数不会更高吗? 这就是我如何让浮子升得更高的原因: 1.添加方法: public Location add(float x, float y, float z){ this.x += x; this.y += y; this.z += z; return this; } 二,。更新方法 public void update(){ float speed = 0.00001f; floa

我在做一个游戏,在游戏中我用一个浮点数设置一个位置,问题是浮点数不会更高吗? 这就是我如何让浮子升得更高的原因: 1.添加方法:

public Location add(float x, float y, float z){
    this.x += x; this.y += y; this.z += z;
    return this;
}
二,。更新方法

public void update(){
        float speed = 0.00001f;
        float rotspeed = 0.00001f;
        if (Keyboard.isKeyDown(Keyboard.KEY_UP)) Main.renderer.rotate(-rotspeed, 0, 0);
        if (Keyboard.isKeyDown(Keyboard.KEY_DOWN)) Main.renderer.rotate(rotspeed, 0, 0);
        if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) Main.renderer.rotate(0, rotspeed, 0);
        if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)) Main.renderer.rotate(0, -rotspeed, 0);

        if (Keyboard.isKeyDown(Keyboard.KEY_W)) Main.renderer.renderLocation.add(0, 0, speed);
        if (Keyboard.isKeyDown(Keyboard.KEY_S)) Main.renderer.renderLocation.add(0, 0, -speed);
        if (Keyboard.isKeyDown(Keyboard.KEY_D)) Main.renderer.renderLocation.add(-speed, 0, 0);
        if (Keyboard.isKeyDown(Keyboard.KEY_A)) Main.renderer.renderLocation.add(speed, 0, 0);

        if (Keyboard.isKeyDown(Keyboard.KEY_SPACE)) Main.renderer.renderLocation.add(0, -speed, 0);
        if (Keyboard.isKeyDown(Keyboard.KEY_LSHIFT)) Main.renderer.renderLocation.add(0, speed, 0);
        fixRenderRotation();
    }
正在子线程中的while循环中调用更新

这就是我用浮子的目的

public void update(){
    System.out.println("render_location "+renderLocation.x+" "+renderLocation.y+" "+renderLocation.z+" rotation "+rotation.x+" "+rotation.y+" "+rotation.z);
    GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
    GL11.glLoadIdentity();
    GL11.glTranslatef(renderLocation.x, renderLocation.y, renderLocation.z);
    //GL11.glTranslatef(renderLocation.x, renderLocation.y, renderLocation.z);
    GL11.glRotatef(rotation.x, 1F, 0F, 0F);
    GL11.glRotatef(rotation.y, 0F, 1F, 0F);
    GL11.glRotatef(rotation.z, 0F, 0F, 1F);
    ((RenderObject.UnknownRenderInformation) collection.getRenderInformation()).act();
}
这是
renderLocaiton
变量。
我不会阻止它上升。

float
的精度只有~6位。我建议使用
int
除以100000或使用
double

1e-5f
添加到
256f
时,答案是
256f
,因为这是最接近的可表示值。你有同样的问题,但在一个更高的点。如果将
1e-5
添加到
137438953472
中,则在超出
double
精度的情况下也会发生同样的情况

如果使用int,则会出现不同的问题。将
1
添加到
2147483647
中,
int
将溢出到
-2147483648

顺便说一句,在达到256之前,你有一个浮动问题。由于表示错误
128f+1e-5f==128.00002
,我怀疑这不是您想要的

事实上,如果您继续添加1e-5f,则整数值/整数之间的增量只需65536倍,而不是您可能期望的100000倍


了解你的类型的限制是很有用的。

除了彼得的答案之外,这是一个必读内容:


基本上,256.00001f四舍五入到256f(另一方面,255.00001f四舍五入到255.00002f)。您可以存储256.00002f,但将四舍五入到256.00003f。第一次向下舍入发生在256.00001f,这是您达到极限的地方,因此任何其他0.00001加法都不会产生任何效果(每次您添加它时,它将由于舍入而被丢弃)。正如您可能已经注意到的,由于指数和尾数的工作方式,浮点的第五位小数并不特别可靠,因此如果您想要更高的精度,请使用双精度,但也不要期望它们具有完美的精度。

有些是,有些不是浮点的内部表示形式。@DaveNewton您确定吗?我想知道
float
是否足够精确,可以表示
255.00001f
255f
@LouisWasserman不同,当然,但这不是问题所在——OP说它不会超过256。是的,如果
256.00001f==256f
,这将是一个副作用。