Last active
March 3, 2019 04:46
-
-
Save a2gs/22a9adef741af9d8ca72acdf428018dc to your computer and use it in GitHub Desktop.
Floating-point error
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> | |
int main(int argc, char *argv[]) | |
{ | |
double x = 516.80, y = 0; | |
long resp1 = 0, resp2 = 0; | |
resp1 =(long) (x * 100.0); | |
y = x * 100.0; | |
resp2 = (long)y; | |
printf("x[%f] y[%f] resp1[%ld] resp2[%ld] [%f]\n", x, y, resp1, resp2, (x * 100.0)); | |
return(0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
cc -o floatError floatError.c -std=c11 -D_XOPEN_SOURCE=700 -D_POSIX_C_SOURCE=200809L -D_POSIX_SOURCE=1 -D_DEFAULT_SOURCE=1 -D_GNU_SOURCE=1 -lm
Output:
x[516.800000] * 100 = [51680 or 51680.000000]
Returned to float: [516.800000 or 516.800000]