/*
"bootstrap by subtraction", the intention of this program is to
demonstrate the possibility to base the arithmetic operations of
addition, multiplication, subtraction and division on the subtraction,
only. Thus, we 'bootstrap' basic arithmetics, with the subtraction
being the axiom.
Copyright (C) 2008 Sebastian Mach (*1983,Germany)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful, but
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Contact:
homepage: http://greenhybrid.net
mail: seb(at)greenhybrid.net
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
"bootstrap by subtraction", the intention of this program is to
demonstrate the possibility to base the arithmetic operations of
addition, multiplication, subtraction and division on the subtraction,
only. Thus, we 'bootstrap' basic arithmetics, with the subtraction
being the axiom.
additionaly, we also forbid loops, and do everything in a functional
way.
TODO: write NEG[ATE]-function and ease code a bit
*/
#include <stdio.h>
#include <stdlib.h>
inline int SUB( int x, int y )
{
return x-y;
}
inline int ADD( int x, int y )
{
return x - SUB(0,y);
}
inline int MUL( int x, int y )
{
return
(y<0) ? SUB( 0, MUL( x, SUB( 0, y ) ) ) :
(y==0) ? 0 :
(y==1) ? x :
ADD( x, MUL( x, y-1 ) );
}
inline int DIV( int x, int y )
{
return
(x<0) ? (y<0) ? DIV( SUB( 0, x ), SUB( 0, y ) )
: SUB( 0, DIV( SUB( 0, x ), y ) )
: (y<0) ? SUB( 0, DIV( x, SUB( 0, y ) ) )
: (x>=y) ? ADD( 1, DIV( SUB(x,y), y ) ) : 0;
}
int main()
{
printf( "bootstrap-by-subtraction\n" );
printf( "Copyright (C)2008 Sebastian Mach (*1983,Germany)\n\n" );
printf( "This program comes with ABSOLUTELY NO WARRANTY;\n\n" );
printf( "THIS PROGRAM IS AN UNSTABLE PROOF-OF-CONCEPT, IT\n" );
printf( "MAY HURT YOUR SYSTEM IF YOU TYPE IN BAD NUMBERS (LACK OF\n" );
printf( "OF FORMAT CHECKING, STACK-PRESSURE ) SO PLEASE TYPE\n" );
printf( "IN ONLY SMALL NUMBERS AROUND -80..+80;\n\n" );
printf( "This is free software, and you are welcome to\n" );
printf( "redistribute it under certain conditions; see:\n" );
printf( "http://www.gnu.org/licenses/gpl-3.0.txt\n\n\n" );
while(1){
int x, y;
printf( "---\ntwo integers, please: " );
scanf( "%i %i", &x, &y );
printf( "---\n" );
printf( "%i + %i = %i\n", x, y, ADD(x,y) );
printf( "%i - %i = %i\n", x, y, SUB(x,y) );
printf( "%i * %i = %i\n", x, y, MUL(x,y) );
printf( "%i / %i = %i\n", x, y, DIV(x,y) );
}
return 0;