Java Double和Float数学行为异常 [英] Java Double and Float math behaving unexpectedly

查看:62
本文介绍了Java Double和Float数学行为异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于应该是一个非常简单的计算,我得到了一个非常奇怪的输出.我正在尝试根据增量平稳地移动相机.如果您查看下面的CameraMenuAnimation(),xMove和cameraDT会相乘并添加到cameraPosX中. cameraPosX然后吐出1.7832639E11.这没有道理吗? CameraPosY做同样的事情.

I am getting a really strange output for what should be a really simple calculation. I'm trying to move a camera smoothly based on delta. If you look at CameraMenuAnimation() below, xMove and cameraDT are multiplied and added to cameraPosX. cameraPosX then spits out 1.7832639E11. This makes no sense?? CameraPosY does the same thing.

private double currentCameraTime, oldCameraTime, cameraFrameTime, cameraDT;
float cameraPosX  = 1.0f;
float cameraPosY = 1.0f;
xMove = .125f;
yMove = .125f;

private void CameraMenuAnimation(){
            cameraPosX += (xMove * cameraDT);
            cameraPosY += (yMove * cameraDT);
            GameCamera.setCameraPos(cameraPosX, cameraPosY);
            System.out.println("xMove: " + xMove + " cameraDT: " + cameraDT + " cameraPosX: "+ cameraPosX);
        }


    @Override
        public void update() {

        //some accumulator variables up here that I removed
            while (accumulator >= step) {
                currentCameraTime = System.currentTimeMillis();
                cameraFrameTime = currentCameraTime - oldCameraTime;
                cameraDT =  cameraFrameTime;
                CameraMenuAnimation();
                oldCameraTime = currentCameraTime;
                accumulator -= step;
            }

它从上面的println语句中打印出来.根本没有道理.

It print out this from the println statement above. It makes no sense at all.

System.out(8714):xMove:0.125 cameraDT:31.0 cameraPosX:1.7832639E11

System.out(8714): xMove: 0.125 cameraDT: 31.0 cameraPosX: 1.7832639E11

推荐答案

很难说,因为您没有给出完整的示例,但是看起来oldCameraTime尚未初始化,因此第一个cameraDT可能非常大.

Hard to say because you haven't given a complete example, but it looks like oldCameraTime is not initialized, so the very first cameraDT may be very large.

这篇关于Java Double和Float数学行为异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆