x的平方的平方根等于x [英] square root of square of x equals x
问题描述
给出一个双精度浮点(非负)数字 x
,它的平方根是否总是等于它的平方根?
Given a double precision floating-point (non-negative) number x
, does square root of its square always equals to itself?
换句话说,如果执行以下操作,是否会损失精度:
In other words, is there any loss of precision if doing the following:
x = <non-negative double>
y = x^2
z = sqrt(y)
这样:
x == z
我对平方变为无穷大或为零的情况不感兴趣,只是适合双精度的数字.
I'm not interested in the case when the square becomes infinity or zero, just numbers that fit the double-precision.
推荐答案
#include <stdio.h>
#include <math.h>
int main(void) {
double x = 1.0000000000000001E-160;
double square = x*x;
double root = sqrt(square);
if (root != x) {
printf("%.20g\n", x);
printf("%.20g\n", root);
}
}
输出
1.0000000000000001466e-160
9.9999443357584897793e-161
这里发生的是 x
足够大以至于其平方非零,但又足够小以至于其平方只能表示为非正规数,从而降低了可用精度.
What's happening here is that x
is large enough that its square is non-zero, but small enough that its square is only representable as a denormalised number, which reduces the available precision.
我觉得@MarkDickinson对@LưuVĩnhPhúc的回答的评论在很大程度上是正确的.如果 x
和 x * x
均为正归一化数字,那么我将无法找到 x!= sqrt(x * x)
即使使用快速蛮力(在很小的范围内),也不应将其视为证明.
I get the impression that @MarkDickinson's comment on @LưuVĩnhPhúc's answer is largely correct though. If both x
and x*x
are positive normalised numbers, then I'm not able to find examples where x != sqrt(x*x)
even with a quick brute force (over a few small ranges), though this should not be considered proof.
这篇关于x的平方的平方根等于x的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!