Skip to content

Instantly share code, notes, and snippets.

@abatilo
Created July 12, 2016 20:58
Show Gist options
  • Save abatilo/90b01aafffd9493bcd17b21ab429ebd8 to your computer and use it in GitHub Desktop.
Save abatilo/90b01aafffd9493bcd17b21ab429ebd8 to your computer and use it in GitHub Desktop.
Timing different abs implementations for Aaron
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <math.h>
double absd(double n) {
unsigned long *gg = (unsigned long int *)&n;
*(gg) &= 0x7FFFFFFFFFFFFFFFull;
return n;
}
int main() {
// Random seed
srand(0);
double sum = 0;
// Generate 2,000,000,000 random numbers
for (int i = 0; i < 2000000000; ++i) {
// sum += absd((double) (rand() - RAND_MAX) / RAND_MAX);
sum += fabs((double) (rand() - RAND_MAX) / RAND_MAX);
}
printf("%f\n", sum);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment