mirror of
https://github.moeyy.xyz/https://github.com/TheAlgorithms/C.git
synced 2023-10-11 15:56:24 +08:00
use fabs and explicitly mark variables as floating point
This commit is contained in:
parent
956e87fcce
commit
0f541b2604
@ -158,8 +158,10 @@ vec_3d unit_vec(const vec_3d *a)
|
|||||||
vec_3d n = {0};
|
vec_3d n = {0};
|
||||||
|
|
||||||
float norm = vector_norm(a);
|
float norm = vector_norm(a);
|
||||||
if (fabsf(norm) < EPSILON) // detect possible divide by 0
|
if (fabsf(norm) < EPSILON)
|
||||||
|
{ // detect possible divide by 0
|
||||||
return n;
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
if (norm != 1.F) // perform division only if needed
|
if (norm != 1.F) // perform division only if needed
|
||||||
{
|
{
|
||||||
@ -206,21 +208,21 @@ static void test()
|
|||||||
|
|
||||||
d = vector_norm(&a);
|
d = vector_norm(&a);
|
||||||
// printf("|a| = %.4g\n", d);
|
// printf("|a| = %.4g\n", d);
|
||||||
assert(fabs(d - 3.742) < 0.01);
|
assert(fabsf(d - 3.742f) < 0.01);
|
||||||
d = vector_norm(&b);
|
d = vector_norm(&b);
|
||||||
// printf("|b| = %.4g\n", d);
|
// printf("|b| = %.4g\n", d);
|
||||||
assert(fabs(d - 1.732) < 0.01);
|
assert(fabsf(d - 1.732f) < 0.01);
|
||||||
|
|
||||||
d = dot_prod(&a, &b);
|
d = dot_prod(&a, &b);
|
||||||
// printf("Dot product: %f\n", d);
|
// printf("Dot product: %f\n", d);
|
||||||
assert(fabs(d - 6.f) < 0.01);
|
assert(fabsf(d - 6.f) < 0.01);
|
||||||
|
|
||||||
vec_3d c = vector_prod(&a, &b);
|
vec_3d c = vector_prod(&a, &b);
|
||||||
// printf("Vector product ");
|
// printf("Vector product ");
|
||||||
// printf("%s", print_vector(&c, "c"));
|
// printf("%s", print_vector(&c, "c"));
|
||||||
assert(fabs(c.x - (-1)) < 0.01);
|
assert(fabsf(c.x - (-1.f)) < 0.01);
|
||||||
assert(fabs(c.y - (2)) < 0.01);
|
assert(fabsf(c.y - (2.f)) < 0.01);
|
||||||
assert(fabs(c.z - (-1)) < 0.01);
|
assert(fabsf(c.z - (-1.f)) < 0.01);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user