Created
April 25, 2011 13:55
-
-
Save laclefyoshi/940541 to your computer and use it in GitHub Desktop.
compare rint() to round()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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