Exercises
1
IEEE标准下 float 类型浮点数 32 位,1位符号位,8位指数位(总共28个数),23位尾数位(总共223个数)
在图形学中正负无穷是有效数,而 NaN 是无效数
正负无穷:指数阶码全为1,尾数部分为0
NaN:指数阶码全为1,尾数部分不为0
故而 the cardinality of the floats = 2 ✖ (28 ✖ 223 - (223 - 1))
2
在64位整数中都能找到32位整数的映射匹配值,但是在32位整数中不一定能找到64位整数的映射匹配值。
3
4
logbx = logex / logeb
log函数的底为负数我是想不到的…
5
6
7
Show that the two forms of the quadratic formulaon page 17 are equivalent (assuming exact arithmetic) and explain how to choose one for each root in order to avoid subtracting nearly equal floating point numbers, which leads to loss of precision.
当B为负数时,-B为正数,为了不减去浮点数损失精度,可以用如下形式求根:(B为正数则为另外两种形式)
8
9
10
(2x, 1, -9z2)
11
12
13
struct Point // 点
{
double x, y;
Point() { }
Point(int a, int b)
{
x = a;
y = b;
}
};
bool Judge(Point &a, Point &b, Point &c, Point &d)
{
/* 跨立实验:如果两条线段相交,那么必须跨立
就是以一条线段为基准,另一条线段的两端点一定在这条线段的两端
利用了向量的叉乘
*/
double u, v, w, z; // 分别记录两个向量
u = (c.x - a.x) * (b.y - a.y) - (b.x - a.x) * (c.y - a.y);
v = (d.x - a.x) * (b.y - a.y) - (b.x - a.x) * (d.y - a.y);
w = (a.x - c.x) * (b.y - a.y) - (b.x - a.x) * (a.y - c.y);
z = (b.x - c.x) * (d.y - c.y) - (d.x - c.x) * (b.y - c.y);
return (u * v <= 0.00000001 && w * z <= 0.00000001) // 由于 double 浮点数的精度问题,不能直接像整数一样比较大小
}
14
struct Point // 点
{
double x, y;
Point() { }
Point(int a, int b)
{
x = a;
y = b;
}
};
Point Search_Barycentric(Point &a, Point &b, Point &c)
{
Point d;
d.x = (a.x + b.x + c.x) / 3;
d.y = (a.y + b.y + c.y) / 3;
return d;
}