使用以下代码,
#include <stdio.h>
int main(void){
float x;
x=(float)3.3==3.3;
printf("%f",x);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出为0.00000
但当数字3.3被3.5替换时,
#include <stdio.h>
int main(void){
float x;
x=(float)3.5==3.5;
printf("%f",x);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出为1.0000
为什么输出有差异?
我有两个大小接近 1000 的整数向量,我要做的是检查这两个向量的平方整数之和是否相同。所以我写了以下代码:
std::vector<int> array1;
std::vector<int> array2;
... // initialize array1 and array2, and in the experiment all elements
// in the two vectors are the same but the sequence of elements may be different.
// For example: array1={1001, 2002, 3003, ....}
// array2={2002, 3003, 1001, ....}
assert(array1.size() == array2.size());
float sum_array1 = 0;
float sum_array2 = 0;
for(int i=0; i<array1.size(); i++)
sum_array1 +=array1[i]*array1[i];
for(int i=0; i<array2.size(); i++)
sum_array2 +=array2[i]*array2[i];
Run Code Online (Sandbox Code Playgroud)
我希望这sum_array1应该等于sum_array2,但实际上在我的应用程序中我发现它们不同sum_array1 = 1.2868639e+009而sum_array2 …
我想确定一个点是否在圆圈内.所以我这样做:
(x - center_x)^2 + (y - center_y)^2 < radius^2
但我的坐标是double,我想我应该用epsilon做,所以
fabs ((x - center_x)^2 + (y - center_y)^2 - radius^2 ) < EPS更好吗?
可能重复:
与float文字的float比较中的奇怪输出
float a = 0.7;
if (a < 0.7) ;
Run Code Online (Sandbox Code Playgroud)
为什么这里的表达式评估为真?
在我的引擎中,我有一个用于编写脚本的Lua VM.在脚本中,我写的内容如下:
stage = stage + 1
if (stage == 5) then ... end
Run Code Online (Sandbox Code Playgroud)
和
objnum = tonumber("5")
if (stage == objnum)
Run Code Online (Sandbox Code Playgroud)
根据Lua的消息来源,Lua在比较它使用的内部数字类型的双精度时使用了一个简单的相等运算符.
我知道处理浮点值时的精度问题,所以我想知道比较是否安全,也就是说,使用Lua的默认'=='操作简单地比较这些数字会有什么问题吗?如果是这样,是否有任何对策我可以确保1 + 2总是比较等于3?将值转换为字符串是否有效?
由于比较浮点数是一个如此困难和争论的问题,我很惊讶地看到CGPointEqualToPoint和其他CG比较方法只是这样做:
CG_INLINE bool
__CGPointEqualToPoint(CGPoint point1, CGPoint point2)
{
return point1.x == point2.x && point1.y == point2.y;
}
Run Code Online (Sandbox Code Playgroud)
在某些情况下,这不会导致问题吗?或者Objective-C是否与fabs,epsilon以及所有这些进行内置的浮点比较?我想知道为什么开发人员选择在这里使用直接相等测试.
虽然用户@skrebbel在SO上发表了这篇文章,但他表示谷歌测试框架在比较浮动和双打方面做得很好而且快速.所以我编写了下面的代码来检查代码的有效性,显然我似乎在这里遗漏了一些东西,因为我希望在这里输入几乎相等的部分这是我的代码
float left = 0.1234567;
float right= 0.1234566;
const FloatingPoint<float> lhs(left), rhs(right);
if (lhs.AlmostEquals(rhs))
{
std::cout << "EQUAL"; //Shouldnt it have entered here ?
}
Run Code Online (Sandbox Code Playgroud)
任何建议都会受到赞赏.
以这个问题为基础,众所周知,我们不应该将equals比较运算应用于十进制变量,因为数字错误(它没有绑定到编程语言):
bool CompareDoubles1 (double A, double B)
{
return A == B;
}
Run Code Online (Sandbox Code Playgroud)
它的代码是不对的.我的问题是:
例如:
bool CompareDoubles1 (double A, double B)
{
double a = round(A,4);
double b = round(B,4)
return a == b;
}
Run Code Online (Sandbox Code Playgroud)
它是正确的?
编辑
我正在考虑round是一个采用double(number)和int(precition)的方法:
bool round (float number, int precision);
Run Code Online (Sandbox Code Playgroud)
编辑 我认为用这个比较方法可以更好地理解我对这个问题的意思:
bool CompareDoubles1 (double A, double B, int precision)
{
//precition could be the error expected when rounding
double a = round(A,precision);
double b = round(B,precision)
return a == b;
}
Run Code Online (Sandbox Code Playgroud) 为了节省调用性能sin,并处理整数角度(更便于操作和保存),而不是使用浮点作为角度,我正在构建一个sin查找函数,其中 4096 个单位等于 2pi 弧度。为了节省内存,我只存储前 1024 个 sin 值,它们相当于sin( [0, pi/2) ).
static const float SinTable[1024] = {0, 0.00153398, ..., 0.999995, 0.999999};
Run Code Online (Sandbox Code Playgroud)
为了处理第三象限和第四象限的角度,我简单地有条件地否定:
return Angle&2048 ? -UnsignedSin : UnsignedSin;
Run Code Online (Sandbox Code Playgroud)
UnsignedSin查找到的 sin 值位于 之间的位置[0, 2048)。但我该如何处理第二象限和第四象限呢?如何通过检查角度是否位于第二或第四象限(例如 )[0, 1)来有条件地正确映射 存储的 sin 值?我尝试了这个,但这不太正确,因为角度的结果是 0.999999 而不是它应该是的 1。[1, 0)Angle&10241024
const float UnsignedSin = SinTable[(Angle&1024 ? ~A : A)&1023];
Run Code Online (Sandbox Code Playgroud)
1 的值永远不会存储在 sin 表中,所以我假设 a1-SinTable[...]是必需的?但我不能完全正确地理解它。
我必须编写一个程序,它接受两个正整数,start和end,其中(1 < start < end).然后程序将在此范围内[ 开始,结束 ]并计算3的幂数.
因此,例如,如果start为2且end为10,则输出为2.(3和9为3的幂).
以下是我的代码:
#include <stdio.h>
#include <math.h>
int main(void) {
int start, end, i, count = 0;
printf("Enter start and end: ");
scanf("%d %d", &start, &end);
for (i = start; i <= end; i++) {
if ((log(i) / log(3)) == floor((log(i) / log(3)))) {
printf("%d\n", i);
count++;
}
}
printf("Answer = %d\n", count);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试运行其中一个测试用例[3,1000]时,输出为5,当它应为6时.
3
9
27
81
729
Answer …Run Code Online (Sandbox Code Playgroud) c++ ×5
c ×4
double ×2
equality ×2
algorithm ×1
cgfloat ×1
cgpoint ×1
compare ×1
comparison ×1
lua ×1
numbers ×1
objective-c ×1
performance ×1
trigonometry ×1