D
damian birchler
I wrote a function that should test if a given number is a palindrom,
but when I ran it in a little testprogram it behaves strange.
#include <stdio.h>
#include <math.h>
#define MAXSIZE 100000000
int is_palindrom(double n) {
double m = 0;
double x;
double y;
double i;
for(i = 10; i <= MAXSIZE; i *= 10) {
x = modf(n/i, &y);
printf("x = modf(%lf, &y)\n\tx == %lf, y == %lf\n", n/i, x, y);
modf((x*10), &y);
printf("modf(%lf, &y)\n\ty == %lf\n", x*10, y);
m += y;
printf("is_palindrom: m currently is %lf\n", m);
m *= 10;
}
while(modf((m/10), &x) == 0)
m /= 10;
printf("is_palindrom: m finally is %lf\n", m);
return n == m;
}
void print_wether_pal(double n) {
printf("\ntesting wheter %lf is a palindrom...\n", n);
printf("\t%lf %s a palindrom\n", n, is_palindrom(n) ? "is" :
"isn't");
}
int main(int argc, char *argv[]) {
print_wether_pal(666);
print_wether_pal(404);
print_wether_pal(3);
print_wether_pal(40);
print_wether_pal(99999999);
return 0;
}
Every first time the second modf in the for-loop in is_palindrom is
called, it returns a number too small by one. This is a snippet of the
program's output:
testing wheter 666.000000 is a palindrom...
x = modf(66.600000, &y)
x == 0.600000, y == 66.000000
modf(6.000000, &y)
y == 5.000000 <<< should be 6
is_palindrom: m currently is 5.000000
x = modf(6.660000, &y)
x == 0.660000, y == 6.000000
modf(6.600000, &y)
y == 6.000000 <<< now is 6!
I know that this isn't really a bug and that the problem results lies
in the internal representation of floating point numbers in the
computer, but is there a way to overcome that problem?
but when I ran it in a little testprogram it behaves strange.
#include <stdio.h>
#include <math.h>
#define MAXSIZE 100000000
int is_palindrom(double n) {
double m = 0;
double x;
double y;
double i;
for(i = 10; i <= MAXSIZE; i *= 10) {
x = modf(n/i, &y);
printf("x = modf(%lf, &y)\n\tx == %lf, y == %lf\n", n/i, x, y);
modf((x*10), &y);
printf("modf(%lf, &y)\n\ty == %lf\n", x*10, y);
m += y;
printf("is_palindrom: m currently is %lf\n", m);
m *= 10;
}
while(modf((m/10), &x) == 0)
m /= 10;
printf("is_palindrom: m finally is %lf\n", m);
return n == m;
}
void print_wether_pal(double n) {
printf("\ntesting wheter %lf is a palindrom...\n", n);
printf("\t%lf %s a palindrom\n", n, is_palindrom(n) ? "is" :
"isn't");
}
int main(int argc, char *argv[]) {
print_wether_pal(666);
print_wether_pal(404);
print_wether_pal(3);
print_wether_pal(40);
print_wether_pal(99999999);
return 0;
}
Every first time the second modf in the for-loop in is_palindrom is
called, it returns a number too small by one. This is a snippet of the
program's output:
testing wheter 666.000000 is a palindrom...
x = modf(66.600000, &y)
x == 0.600000, y == 66.000000
modf(6.000000, &y)
y == 5.000000 <<< should be 6
is_palindrom: m currently is 5.000000
x = modf(6.660000, &y)
x == 0.660000, y == 6.000000
modf(6.600000, &y)
y == 6.000000 <<< now is 6!
I know that this isn't really a bug and that the problem results lies
in the internal representation of floating point numbers in the
computer, but is there a way to overcome that problem?