Skip to content

Instantly share code, notes, and snippets.

@pedro-w
Created August 12, 2020 11:39
Show Gist options
  • Save pedro-w/88608faa328fad2929fd44064a8b0874 to your computer and use it in GitHub Desktop.
Save pedro-w/88608faa328fad2929fd44064a8b0874 to your computer and use it in GitHub Desktop.
float compare in C
#include <math.h>
#include <stdio.h>
struct v3d {
float x, y, z;
};
struct v3d make(float x, float y, float z) {
struct v3d ans;
ans.x = x;
ans.y = y;
ans.z = z;
return ans;
}
struct v3d normalize(struct v3d in) {
float d2 = in.x * in.x + in.y * in.y + in.z * in.z;
float d = sqrtf(d2);
in.x /= d;
in.y /= d;
in.z /= d;
return in;
}
void assert_equal(float a , float b) {
if (a == b) {
printf("%f and %f are equal\n", a, b);
} else {
printf("%f and %f are not equal\n", a, b);
}
}
int main() {
struct v3d u = make(3, 1, 2);
struct v3d v = make( 0.8017837, 0.2672612, 0.5345224);
struct v3d n = normalize(u);
assert_equal(n.x, v.x);
assert_equal(n.y, v.y);
assert_equal(n.z, v.z);
return 0;
}
0.801784 and 0.801784 are equal
0.267261 and 0.267261 are not equal
0.534522 and 0.534522 are not equal
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment