0

Okay so basically, what I want to do, is convert the decimal part of number(678.5123) which is 0.5123 into a whole number(5123). However, if you run the code yourself, you'll see that it stops and it outputs 51229999999 etc. Basically, it's wrong, and I don't know why. Any insights as to where I screwed up and why would be very much appreciated.

#include <stdio.h>
#include <math.h>
int main()
{
int omg, numTimes = 10;
double thisIs, holderThis, number = 678.5123;
thisIs = number - floor(number);
printf("%lf\n", thisIs);
int x = (floor(thisIs) == thisIs);
while (x != 1)
{
    x = (floor(thisIs) == thisIs);
    thisIs *= 10;
    printf("%lf %lf\n", thisIs, floor(thisIs);
}
while(number - floor(number) != 0)
    number *= 10.00;
printf("%lf", thisIs);
return 0;
}
  • Print out `number` with high precision and you'll find out that it isn't exactly 678.5123 either. – M Oehm Apr 14 '16 at 10:35
  • @EOF Wow. Seriously?? Dear Lord God. What cost me a spot in my college's programming league wasn't a code error, or a logical error, but this? REST IN PIECES. HAHAHAHAHAHAHAHAH :'( – Jonn Ralge Yuvallos Apr 14 '16 at 10:37
  • 1
    @JonnRalgeYuvallos: Well, it *is* an error to assume floating-point numbers are real numbers. They can't be, there are finitely many floating-point numbers in a given floating-point format, but uncountably infinitely many real numbers. – EOF Apr 14 '16 at 10:41
  • There is no binary floating point number with a decimal part of 0.5123. Your whole problem is not well-defined from the start. Even then, since 678.05123 or 678.005123 should give the same answer 5123, the whole problem seems rather pointless as well without a _very good_ explanation why you'd want this. – gnasher729 Apr 14 '16 at 11:31

0 Answers0