Skip to content

Instantly share code, notes, and snippets.

@zoeisnowooze
Last active August 14, 2024 14:55
Show Gist options
  • Save zoeisnowooze/42222a7e638fe96543766b849ad68213 to your computer and use it in GitHub Desktop.
Save zoeisnowooze/42222a7e638fe96543766b849ad68213 to your computer and use it in GitHub Desktop.
Experiment with truncation of CPM values
#include <inttypes.h>
#include <stdio.h>
const double EXPECTED_CPM = 0.758630370209851;
const int TRUNCATE_LEN = 28;
int main(int argc, char **argv) {
uint64_t cpm_i = *(uint64_t *)&EXPECTED_CPM;
// Method 1: round up and truncate the lowest TRUNCATE_LEN bits.
uint64_t cpm_i_trunc = ((cpm_i + (1 << TRUNCATE_LEN)) >> TRUNCATE_LEN) << TRUNCATE_LEN;
double cpm = *(double *)&cpm_i_trunc;
// Method 2 (most plausible): convert to 32-bit floating point (causes precision loss), then back to double.
double cpm_f = (double)(float)EXPECTED_CPM;
printf("%.16lf %.16lf %.16f (0x%lx)\n", EXPECTED_CPM, cpm, cpm_f, *(uint64_t *)&cpm_f);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment