我是否正确使用了 BigDecimal 类?我真的可以找人仔细检查我的代码,以确保我不会犯菜鸟错误
我已经为此工作了几天。在出现可笑的双重错误之后,似乎使用 BigDecimal 是正确的选择。这是代码: http://pastebin.com/rpbNJnHH
我以前从未使用过 BigDecimal,所以我希望这里有人可以检查一下并确保没有任何愚蠢的错误。我已经尽可能多地进行了检查和检查。
编辑:抱歉,这很模糊。所以,我使用的是 double 基元类型,但当然,执行类似 for(double i = 0; i < 1; i += .05) 的操作会给 i 提供不精确的值(你会得到类似 .0499999999 或.249999999999)。所以我改用BigDecimal。然而,我以前从未使用过 BD,所以我希望有人可以检查我的代码并确保我正确使用它(即我没有在某个地方失去精度)。谢谢
I've been working on this for a few days. After getting ridiculous double errors, it seemed using BigDecimal was the way to go. Here's the code: http://pastebin.com/rpbNJnHH
I've never had to use BigDecimal before, so I was hoping someone here could give it a look over and make sure there weren't any stupid mistakes. I've gone through and checked as much as I could be sure of though.
Edit: Sorry, this is vague. So, I was using the double primitive type, but of course doing something like for(double i = 0; i < 1; i += .05) will give imprecise values for i (you'll get things like .0499999999 or .249999999999). So I switched to BigDecimal. However, I never used BD before, so I was hoping someone could look over my code and make sure I was using it correctly (i.e. I wasn't losing precision somewhere). Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定这段代码是如何编译的。
x 和 t 必须是 int 值并且在范围内。
我建议您从一些非常简单的可以正确编译和运行的东西开始,然后慢慢添加。如果你试图一次改变太多,你很可能会陷入一片混乱。
我不会使用 double 或 BigDecimal,而是使用 int 值,因为这是您最终需要的类型,除非您的屏幕横向或向下有超过 20 亿像素,否则您不需要 long、double 或 BigDecimal。
I am not sure how this code even compiles.
x and t have to be
int
values and in scope.I recommend you start with something very simple which compiles and runs correctly, and then add to it slowly. If you try to change too much at once you are likely to end with an overwhelming mess.
Rather than using double or BigDecimal, I would use int values because this is the type you need at the end of the day and unless your screen has more than 2 billion pixels across or down, you don't need long, double or BigDecimal.