Skip to content

Instantly share code, notes, and snippets.

@laclefyoshi
Created April 25, 2011 13:55
Show Gist options
  • Save laclefyoshi/940541 to your computer and use it in GitHub Desktop.
Save laclefyoshi/940541 to your computer and use it in GitHub Desktop.
compare rint() to round()
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#define MAX_NUM 1000
int main() {
double x;
double rint_x, round_x;
double total_x, total_rint_x, total_round_x;
int i;
srand((unsigned int)time(NULL));
for(i = 0; i < MAX_NUM; ++i) {
x = rand() % 1000;
x += 0.5;
rint_x = (double)rint(x);
round_x = (double)round(x);
// printf("rint(%.2lf) = %.2lf\n", x, rint_x);
// printf("round(%.2lf) = %.2lf\n", x, round_x);
total_x += x;
total_rint_x += rint_x;
total_round_x += round_x;
}
printf("== result ==\n");
printf("sum(x) = %.2lf\n", total_x);
printf("sum(rint(x)) = %.2lf (diff: %.2lf)\n",
total_rint_x,
total_rint_x - total_x);
printf("sum(round(x)) = %.2lf (diff: %.2lf)\n",
total_round_x,
total_round_x - total_x);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment