我已经实现了一种计算R3中凸四边形区域的方法。该方法工作正常,但我在小数点后8位有数值精度问题。看一下方法:
internal static double GetTriangleArea(double ax, double ay, double az,
double bx, double by, double bz,
double cx, double cy, double cz
)
{
/**
* AB = B-A = (ux, uy, uz)
* AC = C-A = (wx, wy, wz)
*
* S = 0.5*sqrt{(uy*wz - uz*wy)² + (uz*wx - ux*wz)² + (ux*wy - uy*wx)²}
* */
var ux = bx - ax;
var uy = by - ay;
var uz = bz - az;
var wx = cx - ax;
var wy = cy - ay;
var wz = cz - az;
var t1 = uy*wz - uz*wy;
var t2 = uz*wx - ux*wz;
var t3 = ux*wy - uy*wx;
var s = 0.5*Math.Sqrt(t1*t1 + t2*t2 + t3*t3);
return s;
}
internal static double GetConvexQuadrilateralArea(double ax, double ay, double az,
double bx, double by, double bz,
double cx, double cy, double cz,
double dx, double dy, double dz)
{
var triangle1 = GetTriangleArea(ax, ay, az, bx, by, bz, cx, cy, cz);
var triangle2 = GetTriangleArea(ax, ay, az, cx, cy, cz, dx,dy,dz);
return triangle1 + triangle2;
}
这是测试:
[TestMethod]
public void ParallelogramOfBaseBAndHeightHMustHaveAreaEqualToBTimesH()
{
var random = new Random(1);
const double scale = 10000;
for (var counter = 0; counter < 1000; counter++)
{
double baseLength = random.NextDouble() * scale;
double height = random.NextDouble() * scale;
double dx = random.NextDouble()*scale;
var a = new[] { 0, 0, 0 };
var b = new[] { baseLength, 0, 0 };
var c = new[] { baseLength+dx, height, 0 };
var d = new[] { 0F+dx, height, 0 };
double expected = baseLength * height;
var result = MathUtils.GetConvexQuadrilateralArea(a[0], a[1], a[2], b[0], b[1], b[2], c[0], c[1], c[2],
d[0], d[1], d[2]);
Assert.AreEqual(expected, result, Epsilon*scale,
string.Format("sideA: {0}, height: {1}, dx: {2}", baseLength, height, dx));
}
}
此测试失败,并显示以下消息:预期差异不大于&lt; 1E-09&gt;预期值74813926.2967871与实际值74813926.2967871之间。 sideA:8552.44307245707,身高:8747.66726454146,dx:4721.64729829954。
我的问题是:有没有办法在仍然使用双精度数字时提高实现的数值精度?
答案 0 :(得分:2)
双打有52位分数。在十进制中,大约是15.7位数。您的数字在点的左侧有八位数字,因此您只能期望在点的右侧有七位正确 - 这只是由于表示,甚至没有考虑计算产生的任何累积误差
所以,答案是:不,没有办法。您必须使用更精确的格式。