小数点以下2桁に切り捨てられた2つのdouble値があります。それぞれは、ラジアンサークル上の点を表し、目標値を見つけるまで、0.01
で増減すると考えられています。ここでは、コードです:切り捨てられた倍精度浮動小数点型(forループ内のJavaで長い小数点以下を取得する
BigDecimal negPath = new BigDecimal(roundAndConvert(pendA.getTheta()));
BigDecimal posPath = new BigDecimal(roundAndConvert(pendA.getTheta()));
BigDecimal target = new BigDecimal(roundAndConvert(pendB.getTheta()));
for (negPath = negPath; ((negPath.doubleValue() > -0.01) && (!negPath.equals(target))); negPath = negPath.subtract(new BigDecimal(0.01))) {}
for (posPath = posPath; ((posPath.doubleValue() < 6.29) && (!posPath.equals(target))); posPath = posPath.add(new BigDecimal(0.01))) {}
//here's the method that rounds the values
public double roundAndConvert(double value)
{
double holder = (int)(value*100)/100.0;
if (holder < 0)
{
while (holder < -6.28) {holder += 6.28;}
holder += 6.28;
}
else
{
while (holder > 6.28) {holder -= 6.28;}
}
return holder;
}
私がいる問題は、2つのパスの値は、長い小数とダブルスを返すように似ていることである:彼らは好きではないされて返す値を
pos = 6.299999999999944
neg = -0.01999999999996656
これはforループの前にあるので、増分/減分と関係していると思います。
EDIT:doubleをBigDecimalsに置き換えようとしましたが、プログラムはforループでフリーズしています。パスの値としてstringCacheが表示されますが、それでも長い小数点があります。
EDIT:今度はBigDecimal
のすべてのオブジェクトとforループのequals
を使用してコードを変更しましたが、私はまだ長い小数です。
EDIT:最後に示されたコーディングで作業しました。受け入れられた回答者は、私が間違っていたことを説明します。
for (negPath = negPath; ((negPath.doubleValue() > -0.01) && (!negPath.equals(new BigDecimal((int)(target.doubleValue()*100)/100.0)))); negPath = negPath.subtract(new BigDecimal(0.01)))
{
negPath = new BigDecimal((int)(negPath.doubleValue()*100)/100.0);
}
for (posPath = posPath; ((posPath.doubleValue() < 6.29) && (!posPath.equals(new BigDecimal((int)(target.doubleValue()*100)/100.0)))); posPath = posPath.add(new BigDecimal(0.01)))
{
negPath = new BigDecimal((int)(negPath.doubleValue()*100)/100.0);
}
この種の問題に特化したウェブサイト*があります:http://floating-point-gui.de/ –