口袋社区-Poke The BBS's Archiver

youd 发表于 2009-3-1 17:06

关于C++的运算结果。。。

[code]
#include <iostream>
#include <math.h>
using namespace std;
int main (int argc, char *argv[])
{
  float x1,x2,x3,x4,y1,y2,y3,y4,x,y,t,d;
  
  cout << "输入起点 x1:" <<endl;
  cin >> x1;
  cout << "输入起点 y1:" <<endl;
  cin >> y1;
  system("cls");
  cout << "输入控制点 x2:" <<endl;
  cin >> x2;
  cout << "输入控制点 y2:" <<endl;
  cin >> y2;
  system("cls");
  cout << "输入控制点 x3:" <<endl;
  cin >> x3;
  cout << "输入控制点 y3:" <<endl;
  cin >> y3;
  system("cls");
  cout << "输入终点 x4:" <<endl;
  cin >> x4;
  cout << "输入终点 y4:" <<endl;
  cin >> y4;
  system("cls");
  cout << "输入点集精度值:" <<endl;
  cin >> d;
  system("cls");
  
  t=0;
  while(t<=1+d){
    x=pow((1-t),3)*x1+3*t*pow((1-t),2)*x2+3*pow(t,2)*(1-t)*x3+pow(t,3)*x4;
    y=pow((1-t),3)*y1+3*t*pow((1-t),2)*y2+3*pow(t,2)*(1-t)*y3+pow(t,3)*y4;
    cout << "(" << x << "," << y << ")" << endl;
    t+=d;
  }
  
  system("pause");
  return 0;
}
[/code]

以上为程序代码

输入的数据是
[code]
x1=0;
y1=0;
x5=1;
y5=5;
x3=2;
y3=5;
x4=4;
y4=0;
0.1
[/code]

结果是
[code](0,0)
(0.301,1.35)
(0.608,2.4)
(0.927,3.15)
(1.264,3.6)
(1.625,3.75)
(2.016,3.6)
(2.443,3.15)
(2.912,2.4)
(3.429,1.35)
(4,-1.78814e-006)
请按任意键继续. . .[/code]

问题出现在最后一个坐标上。按理计算结果应该是(4,0),可是Y值居然是个浮点数……这叫我很疑惑。。
希望有牛人解答。

*使用Dev-C++4.9.9.2

Zoe 发表于 2009-3-4 20:14

我在cout << "(" << x << "," << y << ")" << endl;这句前加了一句
cout <<(1-t)<<endl;
t=0时输出的是-1.19209e-007
所以我猜测应该是由于结果为0时精度不足造成的
解决方法未知.....

Ghostbird 发表于 2009-5-4 18:41

鉴定程序需要高精度计算完毕

高精度除法很难写

手里有份Pascal的(无压位无四舍五入),y大要我可以在这里贴出来

话说没考古么?

虽然这里有很那啥的考古现象- -

页: [1]

Powered by Discuz! Archiver 6.1.0F  © 2001-2007 Comsenz Inc.