Last active
November 5, 2019 15:23
-
-
Save laanwj/f971bfbe018e39c19677a21ff954d0c7 to your computer and use it in GitHub Desktop.
Test fixed-point number formatting
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 <stdint.h> | |
#include <stdlib.h> | |
#include <limits.h> | |
void print_number_f(double x) { | |
double y = x; | |
int c = 0; | |
if (y < 0.0) { | |
y = -y; | |
} | |
while (y > 0 && y < 100.0) { | |
y *= 10.0; | |
c++; | |
} | |
printf("%.*f", c, x); | |
} | |
#define FP_EXP (6) | |
#define FP_MULT (1000000LL) | |
/* Format fixed point number. */ | |
void print_number(const int64_t x) { | |
int64_t x_abs, y; | |
int c, i, rounding; | |
size_t ptr; | |
char buffer[30]; | |
if (x == INT64_MIN) { | |
/* Prevent UB. */ | |
printf("ERR"); | |
return; | |
} | |
x_abs = x < 0 ? -x : x; | |
/* Determine how many decimals we want to show (more than FP_EXP makes no | |
* sense). */ | |
y = x_abs; | |
c = 0; | |
while (y > 0LL && y < 100LL * FP_MULT && c < FP_EXP) { | |
y *= 10LL; | |
c++; | |
} | |
/* Round to 'c' decimals. */ | |
y = x_abs; | |
rounding = 0; | |
for (i = c; i < FP_EXP; ++i) { | |
rounding = (y % 10) >= 5; | |
y /= 10; | |
} | |
y += rounding; | |
/* Format and print the number. */ | |
ptr = sizeof(buffer) - 1; | |
buffer[ptr] = 0; | |
if (c != 0) { | |
for (i = 0; i < c; ++i) { | |
buffer[--ptr] = '0' + (y % 10); | |
y /= 10; | |
} | |
buffer[--ptr] = '.'; | |
} | |
do { | |
buffer[--ptr] = '0' + (y % 10); | |
y /= 10; | |
} while (y != 0); | |
if (x < 0) { | |
buffer[--ptr] = '-'; | |
} | |
printf("%s", &buffer[ptr]); | |
} | |
int main() | |
{ | |
printf("[comparison]\n"); | |
print_number_f(0.0); | |
printf(" "); | |
print_number(0LL); | |
printf("\n"); | |
print_number_f(0.01); | |
printf(" "); | |
print_number(10000LL); | |
printf("\n"); | |
print_number_f(0.0999); | |
printf(" "); | |
print_number(99900LL); | |
printf("\n"); | |
print_number_f(0.09999); | |
printf(" "); | |
print_number(99990LL); | |
printf("\n"); | |
print_number_f(0.1); | |
printf(" "); | |
print_number(100000LL); | |
printf("\n"); | |
print_number_f(1.0); | |
printf(" "); | |
print_number(1000000LL); | |
printf("\n"); | |
print_number_f(15.0); | |
printf(" "); | |
print_number(15000000LL); | |
printf("\n"); | |
print_number_f(150.0); | |
printf(" "); | |
print_number(150000000LL); | |
printf("\n"); | |
print_number_f(-0.01); | |
printf(" "); | |
print_number(-10000LL); | |
printf("\n"); | |
print_number_f(-150.0); | |
printf(" "); | |
print_number(-150000000LL); | |
printf("\n"); | |
print_number_f(-150.51); | |
printf(" "); | |
print_number(-150510000LL); | |
printf("\n"); | |
printf("[special values]\n"); | |
print_number(1); | |
printf(" "); | |
print_number(-1); | |
printf(" "); | |
print_number(INT64_MAX); | |
printf(" "); | |
print_number(INT64_MIN); | |
printf(" "); | |
print_number((int64_t)-INT_MAX * FP_MULT); | |
printf(" "); | |
print_number((int64_t)INT_MAX * FP_MULT); | |
printf("\n"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment