Check fib() overflow in bellard.otccex.c
Whereas fact() (factorial) is an overflow if n > 12 the Fibonacci is an overflow if n > 48. In this case it reports an empty string - when it finally finishes. For instance if you use n = 99 it takes a long time to finish. Since n == 49 prints an empty value we check for overflow just like the author did for factorial. Add compilation of bellard.otccex.c to Makefile; this can be compiled as 64-bit so we no longer specify -m32 in ARCH but instead we do that explicitly for bellard.c. This way users can at least enjoy the supplementary program even if not by the bellard.c directly compiling and executing it.
This commit is contained in:
1 parent
9990fe2abd
commit
5c4f7ac088
3 files changed
+27
-14
No files matched your search
@@ -55,7 +55,7 @@ CSTD= -std=gnu99
|
||||
# Some entries require 32-bitness: ARCH= -m32
|
||||
# Some entries require 64-bitness: ARCH= -m64
|
||||
#
|
||||
ARCH= -m32
|
||||
ARCH=
|
||||
|
||||
# Defines that are needed to compile
|
||||
#
|
||||
@@ -131,6 +131,11 @@ all: data ${TARGET}
|
||||
supernova deep_magic magic charon pluto
|
||||
|
||||
${PROG}: ${PROG}.c
|
||||
@echo "WARNING: ${PROG}.c must be compiled as 32-bit to use with the entry itself;"
|
||||
@echo "if you cannot use -m32 this will NOT work!"
|
||||
${CC} ${CFLAGS} -m32 $< -o $@ ${LIBS}
|
||||
|
||||
${PROG}.otccex: ${PROG}.otccex.c
|
||||
${CC} ${CFLAGS} $< -o $@ ${LIBS}
|
||||
|
||||
# alternative executable
|
||||
|
||||
+10
-5
@@ -24,17 +24,22 @@ linux or else some skill in resolving the portability issues. As it is it
|
||||
segfaults in 64-bit linux (and as expected macOS). He fixed an earlier segfault
|
||||
so that at least the file can be opened (but perhaps that will be wrong in i386
|
||||
linux?) and he also changed some of the macro used to what they evaluate to but
|
||||
mostly he kept it the same. He also fixed [bellard.otccex.c](bellard.otccex.c)
|
||||
so it does not segfault and seemingly works okay (it did not work at all) but
|
||||
still this appears to not work in 64-bit linux. See below portability notes.
|
||||
Thank you Cody for your assistance!
|
||||
mostly he kept it the same.
|
||||
|
||||
Cody also fixed [bellard.otccex.c](bellard.otccex.c) so it does not segfault and
|
||||
seemingly works okay (it did not work at all). The main problem was that some
|
||||
ints were being used as pointers. Also the Fibonacci sequence (`fib()`) will
|
||||
overflow at `n > 48` so this is checked prior to running the function. Either
|
||||
way unfortunately this entry seems to not work in 64-bit linux or macOS. See
|
||||
below portability notes. Thank you Cody for your assistance!
|
||||
|
||||
|
||||
### Portability notes:
|
||||
|
||||
With a tip from [Yusuke Endoh](/winners.html#Yusuke_Endoh) we rediscovered the
|
||||
author's [web page for this program](https://bellard.org/otcc/) where it is
|
||||
stated that this will only work in i386 linux. The author also stated in the
|
||||
remarks in this document that they used that they used [gcc
|
||||
remarks in this document that they used [gcc
|
||||
2.95.2](https://ftp.gnu.org/gnu/gcc/gcc-2.95.2/gcc-everything-2.95.2.tar.gz) but
|
||||
we don't know if that's relevant or not.
|
||||
|
||||
|
||||
@@ -35,6 +35,7 @@ fact(n)
|
||||
/* local variables can be declared. Only 'int' type is supported */
|
||||
int i, r;
|
||||
r = 1;
|
||||
|
||||
/* 'while' and 'for' loops are supported */
|
||||
for(i=2;i<=n;i++)
|
||||
r = r * i;
|
||||
@@ -60,9 +61,9 @@ print_num(n, b)
|
||||
*p = c;
|
||||
p++;
|
||||
n = n / b;
|
||||
/* 'break' is supported */
|
||||
if (n == 0)
|
||||
break;
|
||||
/* 'break' is supported */
|
||||
if (n == 0)
|
||||
break;
|
||||
}
|
||||
while (p != tab) {
|
||||
p--;
|
||||
@@ -100,11 +101,13 @@ main(int argc, char **argv)
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
printf("fib(%d) = ", n);
|
||||
print_num(fib(n), base);
|
||||
printf("\n");
|
||||
|
||||
printf("fact(%d) = ", n);
|
||||
printf("fib(%d) with base %d = ", base, n);
|
||||
if (n > 48) {
|
||||
printf("Overflow\n");
|
||||
} else {
|
||||
print_num(fib(n), base);
|
||||
}
|
||||
printf("fact(%d) with base %d = ", base, n);
|
||||
if (n > 12) {
|
||||
printf("Overflow");
|
||||
} else {
|
||||
|
||||
Reference in new issue
Block a user