Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

浮点数的精度到底设多少合适啊,有没有什么方法呀

Posted by tssslu at 2014-10-12 18:31:43 on Problem 1039
虽然这题只输出两位小数,但是1e-3的精度过不了,设成1e-5就过了。

请问大家除了反复提交尝试还有什么方法能够找出浮点数精度吗?

#include <stdio.h>
#define INF 1e10
#define PREC 1e-5 // 1e-3会WA
#define lf_less(lf1, lf2) \
	(lf1<=lf2+PREC)
#define lf_eqaul(lf1, lf2) \
	((lf1-lf2<=PREC) && (lf1-lf2>=-PREC))
#define max(x,y) \
	((x>y)?x:y)
#define init_line(line,x1,y1,x2,y2) \
	line.k = (y2-y1) / (x2-x1); \
	line.b = y1 - line.k * x1
#define line_y_value(line,x) \
	(line.k * x + line.b)
#define line_intsec_x_value(line1, line2) \
	((line2.b - line1.b) / (line1.k - line2.k))
typedef struct str_line {
	double k, b;
} str_line;
int n;
double point[40][2], M;
void check_light(int idx1, int idx2)
{
	int i;
	double tmp_y;
	str_line light, pipe;
	init_line(light, point[idx1][0], point[idx1][1], 
			point[idx2][0], point[idx2][1]);
	for (i = 0; i < 2*n; i++) {
		tmp_y = line_y_value(light, point[i][0]);
		if ((i % 2 == 0 && (lf_less(tmp_y, point[i][1])) ) ||
			(i % 2 == 1 && (lf_less(point[i][1], tmp_y)) )) {
			continue;
		}
		else if (i <= max(idx1, idx2)) {
			return;
		}
		else {
			init_line(pipe, point[i][0], point[i][1],
					point[i-2][0], point[i-2][1]);
			M = max(M, line_intsec_x_value(light, pipe));
			return;
		}
	}
	M = point[2*n-1][0];
}
int main(int argc, const char *argv[])
{
	int i, j;
	while (scanf("%d", &n) && n != 0) {
		M = -INF;
		for (i = 0; i < n; i++) {
			scanf("%lf %lf", 
				&point[2*i][0], &point[2*i][1]); 
			point[2*i+1][0] = point[2*i][0];
			point[2*i+1][1] = point[2*i][1]-1;
		}
		for (i = 0; i < 2*n; i++) {
			for (j = 0; j < 2*n; j++) {
				if (lf_eqaul(point[i][0], point[j][0])) {
					continue;
				}
				check_light(i, j);
			}
		}
		if (lf_eqaul(M, point[2*n-1][0])) {
			printf("Through all the pipe.\n");
		}
		else {
			printf("%.2f\n", M);
		}
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator