Work on exception handling, especially around ratio arithmetic
Much simplified but will break things!
This commit is contained in:
parent
d2101dbd47
commit
70d176982b
14 changed files with 298 additions and 258 deletions
|
|
@ -390,3 +390,38 @@ struct cons_pointer integer_to_string( struct cons_pointer int_pointer,
|
|||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* true if a and be are both integers whose value is the same value.
|
||||
*/
|
||||
bool equal_integer_integer(struct cons_pointer a, struct cons_pointer b) {
|
||||
bool result = false;
|
||||
|
||||
if (integerp(a) && integerp(b)){
|
||||
struct cons_space_object *cell_a = &pointer2cell( a );
|
||||
struct cons_space_object *cell_b = &pointer2cell( b );
|
||||
|
||||
result = cell_a->payload.integer.value == cell_b->payload.integer.value;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* true if `a` is an integer, and `b` is a real number whose value is the
|
||||
* value of that integer.
|
||||
*/
|
||||
bool equal_integer_real(struct cons_pointer a, struct cons_pointer b) {
|
||||
bool result = false;
|
||||
|
||||
if (integerp(a) && realp(b))
|
||||
{
|
||||
long double bv = pointer2cell(b).payload.real.value;
|
||||
|
||||
if (floor(bv) == bv) {
|
||||
result = pointer2cell(a).payload.integer.value == (int64_t)bv;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
|
@ -11,15 +11,19 @@
|
|||
#ifndef __integer_h
|
||||
#define __integer_h
|
||||
|
||||
struct cons_pointer make_integer( int64_t value, struct cons_pointer more );
|
||||
struct cons_pointer make_integer(int64_t value, struct cons_pointer more);
|
||||
|
||||
struct cons_pointer add_integers( struct cons_pointer a,
|
||||
struct cons_pointer b );
|
||||
struct cons_pointer add_integers(struct cons_pointer a,
|
||||
struct cons_pointer b);
|
||||
|
||||
struct cons_pointer multiply_integers( struct cons_pointer a,
|
||||
struct cons_pointer b );
|
||||
struct cons_pointer multiply_integers(struct cons_pointer a,
|
||||
struct cons_pointer b);
|
||||
|
||||
struct cons_pointer integer_to_string( struct cons_pointer int_pointer,
|
||||
int base );
|
||||
struct cons_pointer integer_to_string(struct cons_pointer int_pointer,
|
||||
int base);
|
||||
|
||||
bool equal_integer_integer(struct cons_pointer a, struct cons_pointer b);
|
||||
|
||||
bool equal_integer_real(struct cons_pointer a, struct cons_pointer b);
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -86,8 +86,7 @@ bool is_negative( struct cons_pointer arg ) {
|
|||
return result;
|
||||
}
|
||||
|
||||
struct cons_pointer absolute( struct cons_pointer frame_pointer,
|
||||
struct cons_pointer arg ) {
|
||||
struct cons_pointer absolute( struct cons_pointer arg ) {
|
||||
struct cons_pointer result = NIL;
|
||||
struct cons_space_object cell = pointer2cell( arg );
|
||||
|
||||
|
|
@ -99,9 +98,7 @@ struct cons_pointer absolute( struct cons_pointer frame_pointer,
|
|||
cell.payload.integer.more );
|
||||
break;
|
||||
case RATIOTV:
|
||||
result = make_ratio( frame_pointer,
|
||||
absolute( frame_pointer,
|
||||
cell.payload.ratio.dividend ),
|
||||
result = make_ratio( absolute( cell.payload.ratio.dividend ),
|
||||
cell.payload.ratio.divisor );
|
||||
break;
|
||||
case REALTV:
|
||||
|
|
@ -210,7 +207,7 @@ int64_t to_long_int( struct cons_pointer arg ) {
|
|||
struct cons_pointer lisp_absolute( struct stack_frame
|
||||
*frame, struct cons_pointer frame_pointer, struct
|
||||
cons_pointer env ) {
|
||||
return absolute( frame_pointer, frame->arg[0] );
|
||||
return absolute( frame->arg[0] );
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -251,7 +248,7 @@ struct cons_pointer add_2( struct stack_frame *frame,
|
|||
break;
|
||||
case RATIOTV:
|
||||
result =
|
||||
add_integer_ratio( frame_pointer, arg1, arg2 );
|
||||
add_integer_ratio( arg1, arg2 );
|
||||
break;
|
||||
case REALTV:
|
||||
result =
|
||||
|
|
@ -272,10 +269,10 @@ struct cons_pointer add_2( struct stack_frame *frame,
|
|||
break;
|
||||
case INTEGERTV:
|
||||
result =
|
||||
add_integer_ratio( frame_pointer, arg2, arg1 );
|
||||
add_integer_ratio( arg2, arg1 );
|
||||
break;
|
||||
case RATIOTV:
|
||||
result = add_ratio_ratio( frame_pointer, arg1, arg2 );
|
||||
result = add_ratio_ratio( arg1, arg2 );
|
||||
break;
|
||||
case REALTV:
|
||||
result =
|
||||
|
|
@ -384,7 +381,7 @@ struct cons_pointer multiply_2( struct stack_frame *frame,
|
|||
break;
|
||||
case RATIOTV:
|
||||
result =
|
||||
multiply_integer_ratio( frame_pointer, arg1,
|
||||
multiply_integer_ratio( arg1,
|
||||
arg2 );
|
||||
break;
|
||||
case REALTV:
|
||||
|
|
@ -409,12 +406,12 @@ struct cons_pointer multiply_2( struct stack_frame *frame,
|
|||
break;
|
||||
case INTEGERTV:
|
||||
result =
|
||||
multiply_integer_ratio( frame_pointer, arg2,
|
||||
multiply_integer_ratio( arg2,
|
||||
arg1 );
|
||||
break;
|
||||
case RATIOTV:
|
||||
result =
|
||||
multiply_ratio_ratio( frame_pointer, arg1, arg2 );
|
||||
multiply_ratio_ratio( arg1, arg2 );
|
||||
break;
|
||||
case REALTV:
|
||||
result =
|
||||
|
|
@ -496,8 +493,7 @@ struct cons_pointer lisp_multiply( struct
|
|||
* return a cons_pointer indicating a number which is the
|
||||
* 0 - the number indicated by `arg`.
|
||||
*/
|
||||
struct cons_pointer negative( struct cons_pointer frame,
|
||||
struct cons_pointer arg ) {
|
||||
struct cons_pointer negative( struct cons_pointer arg ) {
|
||||
struct cons_pointer result = NIL;
|
||||
struct cons_space_object cell = pointer2cell( arg );
|
||||
|
||||
|
|
@ -514,9 +510,7 @@ struct cons_pointer negative( struct cons_pointer frame,
|
|||
result = TRUE;
|
||||
break;
|
||||
case RATIOTV:
|
||||
result = make_ratio( frame,
|
||||
negative( frame,
|
||||
cell.payload.ratio.dividend ),
|
||||
result = make_ratio( negative( cell.payload.ratio.dividend ),
|
||||
cell.payload.ratio.divisor );
|
||||
break;
|
||||
case REALTV:
|
||||
|
|
@ -571,7 +565,7 @@ struct cons_pointer subtract_2( struct stack_frame *frame,
|
|||
break;
|
||||
case INTEGERTV:{
|
||||
struct cons_pointer i =
|
||||
negative( frame_pointer, arg2 );
|
||||
negative( arg2 );
|
||||
inc_ref( i );
|
||||
result = add_integers( arg1, i );
|
||||
dec_ref( i );
|
||||
|
|
@ -579,11 +573,11 @@ struct cons_pointer subtract_2( struct stack_frame *frame,
|
|||
break;
|
||||
case RATIOTV:{
|
||||
struct cons_pointer tmp =
|
||||
make_ratio( frame_pointer, arg1,
|
||||
make_ratio( arg1,
|
||||
make_integer( 1, NIL ) );
|
||||
inc_ref( tmp );
|
||||
result =
|
||||
subtract_ratio_ratio( frame_pointer, tmp, arg2 );
|
||||
subtract_ratio_ratio( tmp, arg2 );
|
||||
dec_ref( tmp );
|
||||
}
|
||||
break;
|
||||
|
|
@ -606,16 +600,16 @@ struct cons_pointer subtract_2( struct stack_frame *frame,
|
|||
break;
|
||||
case INTEGERTV:{
|
||||
struct cons_pointer tmp =
|
||||
make_ratio( frame_pointer, arg2,
|
||||
make_ratio( arg2,
|
||||
make_integer( 1, NIL ) );
|
||||
inc_ref( tmp );
|
||||
result =
|
||||
subtract_ratio_ratio( frame_pointer, arg1, tmp );
|
||||
subtract_ratio_ratio( arg1, tmp );
|
||||
dec_ref( tmp );
|
||||
}
|
||||
break;
|
||||
case RATIOTV:
|
||||
result = subtract_ratio_ratio( frame_pointer, arg1, arg2 );
|
||||
result = subtract_ratio_ratio( arg1, arg2 );
|
||||
break;
|
||||
case REALTV:
|
||||
result =
|
||||
|
|
@ -687,11 +681,11 @@ struct cons_pointer lisp_divide( struct
|
|||
break;
|
||||
case INTEGERTV:{
|
||||
struct cons_pointer unsimplified =
|
||||
make_ratio( frame_pointer, frame->arg[0],
|
||||
make_ratio( frame->arg[0],
|
||||
frame->arg[1] );
|
||||
/* OK, if result may be unsimplified, we should not inc_ref it
|
||||
* - but if not, we should dec_ref it. */
|
||||
result = simplify_ratio( frame_pointer, unsimplified );
|
||||
result = simplify_ratio( unsimplified );
|
||||
if ( !eq( unsimplified, result ) ) {
|
||||
dec_ref( unsimplified );
|
||||
}
|
||||
|
|
@ -700,10 +694,10 @@ struct cons_pointer lisp_divide( struct
|
|||
case RATIOTV:{
|
||||
struct cons_pointer one = make_integer( 1, NIL );
|
||||
struct cons_pointer ratio =
|
||||
make_ratio( frame_pointer, frame->arg[0], one );
|
||||
make_ratio( frame->arg[0], one );
|
||||
inc_ref( ratio );
|
||||
result =
|
||||
divide_ratio_ratio( frame_pointer, ratio,
|
||||
divide_ratio_ratio( ratio,
|
||||
frame->arg[1] );
|
||||
dec_ref( ratio );
|
||||
}
|
||||
|
|
@ -729,10 +723,10 @@ struct cons_pointer lisp_divide( struct
|
|||
struct cons_pointer one = make_integer( 1, NIL );
|
||||
inc_ref( one );
|
||||
struct cons_pointer ratio =
|
||||
make_ratio( frame_pointer, frame->arg[1], one );
|
||||
make_ratio( frame->arg[1], one );
|
||||
inc_ref( ratio );
|
||||
result =
|
||||
divide_ratio_ratio( frame_pointer, frame->arg[0],
|
||||
divide_ratio_ratio( frame->arg[0],
|
||||
ratio );
|
||||
dec_ref( ratio );
|
||||
dec_ref( one );
|
||||
|
|
@ -740,7 +734,7 @@ struct cons_pointer lisp_divide( struct
|
|||
break;
|
||||
case RATIOTV:
|
||||
result =
|
||||
divide_ratio_ratio( frame_pointer, frame->arg[0],
|
||||
divide_ratio_ratio( frame->arg[0],
|
||||
frame->arg[1] );
|
||||
break;
|
||||
case REALTV:
|
||||
|
|
|
|||
|
|
@ -19,13 +19,11 @@
|
|||
|
||||
bool zerop( struct cons_pointer arg );
|
||||
|
||||
struct cons_pointer negative( struct cons_pointer frame,
|
||||
struct cons_pointer arg );
|
||||
struct cons_pointer negative( struct cons_pointer arg );
|
||||
|
||||
bool is_negative( struct cons_pointer arg );
|
||||
|
||||
struct cons_pointer absolute( struct cons_pointer frame_pointer,
|
||||
struct cons_pointer arg );
|
||||
struct cons_pointer absolute( struct cons_pointer arg );
|
||||
|
||||
long double to_long_double( struct cons_pointer arg );
|
||||
|
||||
|
|
@ -46,8 +44,7 @@ struct cons_pointer
|
|||
lisp_multiply( struct stack_frame *frame,
|
||||
struct cons_pointer frame_pointer, struct cons_pointer env );
|
||||
|
||||
struct cons_pointer negative( struct cons_pointer frame,
|
||||
struct cons_pointer arg );
|
||||
struct cons_pointer negative( struct cons_pointer arg );
|
||||
|
||||
struct cons_pointer subtract_2( struct stack_frame *frame,
|
||||
struct cons_pointer frame_pointer,
|
||||
|
|
|
|||
|
|
@ -43,52 +43,52 @@ int64_t least_common_multiple( int64_t m, int64_t n ) {
|
|||
return m / greatest_common_divisor( m, n ) * n;
|
||||
}
|
||||
|
||||
/**
|
||||
* return a cons_pointer indicating a number which is of the
|
||||
* same value as the ratio indicated by `arg`, but which may
|
||||
* be in a simplified representation.
|
||||
* @exception If `arg` isn't a ratio, will return an exception.
|
||||
*/
|
||||
struct cons_pointer simplify_ratio( struct cons_pointer frame_pointer,
|
||||
struct cons_pointer arg ) {
|
||||
struct cons_pointer result = arg;
|
||||
struct cons_pointer simplify_ratio( struct cons_pointer pointer) {
|
||||
struct cons_pointer result = pointer;
|
||||
struct cons_space_object cell = pointer2cell(pointer);
|
||||
struct cons_space_object dividend = pointer2cell(cell.payload.ratio.dividend);
|
||||
struct cons_space_object divisor = pointer2cell(cell.payload.ratio.divisor);
|
||||
|
||||
if ( ratiop( arg ) ) {
|
||||
int64_t ddrv =
|
||||
pointer2cell( pointer2cell( arg ).payload.ratio.dividend ).
|
||||
payload.integer.value, drrv =
|
||||
pointer2cell( pointer2cell( arg ).payload.ratio.divisor ).
|
||||
payload.integer.value, gcd = greatest_common_divisor( ddrv, drrv );
|
||||
if (divisor.payload.integer.value == 1)
|
||||
{
|
||||
result = pointer2cell(pointer).payload.ratio.dividend;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (ratiop(pointer))
|
||||
{
|
||||
int64_t ddrv = dividend.payload.integer.value,
|
||||
drrv = divisor.payload.integer.value,
|
||||
gcd = greatest_common_divisor(ddrv, drrv);
|
||||
|
||||
if ( gcd > 1 ) {
|
||||
if ( drrv / gcd == 1 ) {
|
||||
result = make_integer( ddrv / gcd, NIL );
|
||||
} else {
|
||||
result =
|
||||
make_ratio( frame_pointer, make_integer( ddrv / gcd, NIL ),
|
||||
make_integer( drrv / gcd, NIL ) );
|
||||
if (gcd > 1)
|
||||
{
|
||||
if (drrv / gcd == 1)
|
||||
{
|
||||
result = make_integer(ddrv / gcd, NIL);
|
||||
}
|
||||
else
|
||||
{
|
||||
result =
|
||||
make_ratio(make_integer(ddrv / gcd, NIL),
|
||||
make_integer(drrv / gcd, NIL));
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
result =
|
||||
throw_exception( make_cons( c_string_to_lisp_string
|
||||
( L"Shouldn't happen: bad arg to simplify_ratio" ),
|
||||
arg ), frame_pointer );
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* return a cons_pointer indicating a number which is the sum of
|
||||
* the ratios indicated by `arg1` and `arg2`.
|
||||
* @exception will return an exception if either `arg1` or `arg2` is not a
|
||||
* rational number.
|
||||
*/
|
||||
struct cons_pointer add_ratio_ratio( struct cons_pointer frame_pointer,
|
||||
struct cons_pointer arg1,
|
||||
struct cons_pointer add_ratio_ratio( struct cons_pointer arg1,
|
||||
struct cons_pointer arg2 ) {
|
||||
struct cons_pointer r, result;
|
||||
|
||||
|
|
@ -116,18 +116,17 @@ struct cons_pointer add_ratio_ratio( struct cons_pointer frame_pointer,
|
|||
m1, m2 );
|
||||
|
||||
if ( dr1v == dr2v ) {
|
||||
r = make_ratio( frame_pointer,
|
||||
make_integer( dd1v + dd2v, NIL ),
|
||||
r = make_ratio( make_integer( dd1v + dd2v, NIL ),
|
||||
cell1.payload.ratio.divisor );
|
||||
} else {
|
||||
struct cons_pointer dd1vm = make_integer( dd1v * m1, NIL ),
|
||||
dr1vm = make_integer( dr1v * m1, NIL ),
|
||||
dd2vm = make_integer( dd2v * m2, NIL ),
|
||||
dr2vm = make_integer( dr2v * m2, NIL ),
|
||||
r1 = make_ratio( frame_pointer, dd1vm, dr1vm ),
|
||||
r2 = make_ratio( frame_pointer, dd2vm, dr2vm );
|
||||
r1 = make_ratio( dd1vm, dr1vm ),
|
||||
r2 = make_ratio( dd2vm, dr2vm );
|
||||
|
||||
r = add_ratio_ratio( frame_pointer, r1, r2 );
|
||||
r = add_ratio_ratio( r1, r2 );
|
||||
|
||||
/* because the references on dd1vm, dr1vm, dd2vm and dr2vm were
|
||||
* never incremented except when making r1 and r2, decrementing
|
||||
|
|
@ -136,7 +135,7 @@ struct cons_pointer add_ratio_ratio( struct cons_pointer frame_pointer,
|
|||
dec_ref( r2 );
|
||||
}
|
||||
|
||||
result = simplify_ratio( frame_pointer, r );
|
||||
result = simplify_ratio( r );
|
||||
if ( !eq( r, result ) ) {
|
||||
dec_ref( r );
|
||||
}
|
||||
|
|
@ -146,7 +145,7 @@ struct cons_pointer add_ratio_ratio( struct cons_pointer frame_pointer,
|
|||
( L"Shouldn't happen: bad arg to add_ratio_ratio" ),
|
||||
make_cons( arg1,
|
||||
make_cons( arg2, NIL ) ) ),
|
||||
frame_pointer );
|
||||
NIL );
|
||||
}
|
||||
|
||||
debug_print( L" => ", DEBUG_ARITH );
|
||||
|
|
@ -163,16 +162,16 @@ struct cons_pointer add_ratio_ratio( struct cons_pointer frame_pointer,
|
|||
* `ratarg`.
|
||||
* @exception if either `intarg` or `ratarg` is not of the expected type.
|
||||
*/
|
||||
struct cons_pointer add_integer_ratio( struct cons_pointer frame_pointer,
|
||||
struct cons_pointer intarg,
|
||||
struct cons_pointer add_integer_ratio( struct cons_pointer intarg,
|
||||
struct cons_pointer ratarg ) {
|
||||
struct cons_pointer result;
|
||||
|
||||
if ( integerp( intarg ) && ratiop( ratarg ) ) {
|
||||
// TODO: not longer works
|
||||
struct cons_pointer one = make_integer( 1, NIL ),
|
||||
ratio = make_ratio( frame_pointer, intarg, one );
|
||||
ratio = make_ratio( intarg, one );
|
||||
|
||||
result = add_ratio_ratio( frame_pointer, ratio, ratarg );
|
||||
result = add_ratio_ratio( ratio, ratarg );
|
||||
|
||||
dec_ref( one );
|
||||
dec_ref( ratio );
|
||||
|
|
@ -183,7 +182,7 @@ struct cons_pointer add_integer_ratio( struct cons_pointer frame_pointer,
|
|||
make_cons( intarg,
|
||||
make_cons( ratarg,
|
||||
NIL ) ) ),
|
||||
frame_pointer );
|
||||
NIL );
|
||||
}
|
||||
|
||||
return result;
|
||||
|
|
@ -195,15 +194,14 @@ struct cons_pointer add_integer_ratio( struct cons_pointer frame_pointer,
|
|||
* @exception will return an exception if either `arg1` or `arg2` is not a
|
||||
* rational number.
|
||||
*/
|
||||
struct cons_pointer divide_ratio_ratio( struct cons_pointer frame_pointer,
|
||||
struct cons_pointer arg1,
|
||||
struct cons_pointer divide_ratio_ratio( struct cons_pointer arg1,
|
||||
struct cons_pointer arg2 ) {
|
||||
struct cons_pointer i = make_ratio( frame_pointer,
|
||||
pointer2cell( arg2 ).payload.
|
||||
// TODO: this now has to work if `arg1` is an integer
|
||||
struct cons_pointer i = make_ratio( pointer2cell( arg2 ).payload.
|
||||
ratio.divisor,
|
||||
pointer2cell( arg2 ).payload.
|
||||
ratio.dividend ), result =
|
||||
multiply_ratio_ratio( frame_pointer, arg1, i );
|
||||
multiply_ratio_ratio( arg1, i );
|
||||
|
||||
dec_ref( i );
|
||||
|
||||
|
|
@ -216,9 +214,10 @@ struct cons_pointer divide_ratio_ratio( struct cons_pointer frame_pointer,
|
|||
* @exception will return an exception if either `arg1` or `arg2` is not a
|
||||
* rational number.
|
||||
*/
|
||||
struct cons_pointer multiply_ratio_ratio( struct cons_pointer frame_pointer, struct
|
||||
struct cons_pointer multiply_ratio_ratio( struct
|
||||
cons_pointer arg1, struct
|
||||
cons_pointer arg2 ) {
|
||||
// TODO: this now has to work if arg1 is an integer
|
||||
struct cons_pointer result;
|
||||
|
||||
debug_print( L"multiply_ratio_ratio( arg1 = ", DEBUG_ARITH );
|
||||
|
|
@ -241,9 +240,9 @@ struct cons_pointer multiply_ratio_ratio( struct cons_pointer frame_pointer, str
|
|||
ddrv = dd1v * dd2v, drrv = dr1v * dr2v;
|
||||
|
||||
struct cons_pointer unsimplified =
|
||||
make_ratio( frame_pointer, make_integer( ddrv, NIL ),
|
||||
make_ratio( make_integer( ddrv, NIL ),
|
||||
make_integer( drrv, NIL ) );
|
||||
result = simplify_ratio( frame_pointer, unsimplified );
|
||||
result = simplify_ratio( unsimplified );
|
||||
|
||||
if ( !eq( unsimplified, result ) ) {
|
||||
dec_ref( unsimplified );
|
||||
|
|
@ -252,7 +251,7 @@ struct cons_pointer multiply_ratio_ratio( struct cons_pointer frame_pointer, str
|
|||
result =
|
||||
throw_exception( c_string_to_lisp_string
|
||||
( L"Shouldn't happen: bad arg to multiply_ratio_ratio" ),
|
||||
frame_pointer );
|
||||
NIL );
|
||||
}
|
||||
|
||||
return result;
|
||||
|
|
@ -264,15 +263,15 @@ struct cons_pointer multiply_ratio_ratio( struct cons_pointer frame_pointer, str
|
|||
* `ratarg`.
|
||||
* @exception if either `intarg` or `ratarg` is not of the expected type.
|
||||
*/
|
||||
struct cons_pointer multiply_integer_ratio( struct cons_pointer frame_pointer,
|
||||
struct cons_pointer intarg,
|
||||
struct cons_pointer multiply_integer_ratio( struct cons_pointer intarg,
|
||||
struct cons_pointer ratarg ) {
|
||||
struct cons_pointer result;
|
||||
|
||||
if ( integerp( intarg ) && ratiop( ratarg ) ) {
|
||||
// TODO: no longer works; fix
|
||||
struct cons_pointer one = make_integer( 1, NIL ),
|
||||
ratio = make_ratio( frame_pointer, intarg, one );
|
||||
result = multiply_ratio_ratio( frame_pointer, ratio, ratarg );
|
||||
ratio = make_ratio( intarg, one );
|
||||
result = multiply_ratio_ratio( ratio, ratarg );
|
||||
|
||||
dec_ref( one );
|
||||
dec_ref( ratio );
|
||||
|
|
@ -280,7 +279,7 @@ struct cons_pointer multiply_integer_ratio( struct cons_pointer frame_pointer,
|
|||
result =
|
||||
throw_exception( c_string_to_lisp_string
|
||||
( L"Shouldn't happen: bad arg to multiply_integer_ratio" ),
|
||||
frame_pointer );
|
||||
NIL );
|
||||
}
|
||||
|
||||
return result;
|
||||
|
|
@ -293,11 +292,10 @@ struct cons_pointer multiply_integer_ratio( struct cons_pointer frame_pointer,
|
|||
* @exception will return an exception if either `arg1` or `arg2` is not a
|
||||
* rational number.
|
||||
*/
|
||||
struct cons_pointer subtract_ratio_ratio( struct cons_pointer frame_pointer,
|
||||
struct cons_pointer arg1,
|
||||
struct cons_pointer subtract_ratio_ratio( struct cons_pointer arg1,
|
||||
struct cons_pointer arg2 ) {
|
||||
struct cons_pointer i = negative( frame_pointer, arg2 ),
|
||||
result = add_ratio_ratio( frame_pointer, arg1, i );
|
||||
struct cons_pointer i = negative( arg2),
|
||||
result = add_ratio_ratio( arg1, i );
|
||||
|
||||
dec_ref( i );
|
||||
|
||||
|
|
@ -311,8 +309,7 @@ struct cons_pointer subtract_ratio_ratio( struct cons_pointer frame_pointer,
|
|||
* `frame_pointer`.
|
||||
* @exception if either `dividend` or `divisor` is not an integer.
|
||||
*/
|
||||
struct cons_pointer make_ratio( struct cons_pointer frame_pointer,
|
||||
struct cons_pointer dividend,
|
||||
struct cons_pointer make_ratio( struct cons_pointer dividend,
|
||||
struct cons_pointer divisor ) {
|
||||
struct cons_pointer result;
|
||||
if ( integerp( dividend ) && integerp( divisor ) ) {
|
||||
|
|
@ -326,10 +323,30 @@ struct cons_pointer make_ratio( struct cons_pointer frame_pointer,
|
|||
result =
|
||||
throw_exception( c_string_to_lisp_string
|
||||
( L"Dividend and divisor of a ratio must be integers" ),
|
||||
frame_pointer );
|
||||
NIL );
|
||||
}
|
||||
debug_dump_object( result, DEBUG_ARITH );
|
||||
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* True if a and be are identical ratios, else false.
|
||||
*/
|
||||
bool equal_ratio_ratio(struct cons_pointer a, struct cons_pointer b)
|
||||
{
|
||||
bool result = false;
|
||||
|
||||
if (ratiop(a) && ratiop(b))
|
||||
{
|
||||
struct cons_space_object *cell_a = &pointer2cell(a);
|
||||
struct cons_space_object *cell_b = &pointer2cell(b);
|
||||
|
||||
result = equal_integer_integer(cell_a->payload.ratio.dividend,
|
||||
cell_b->payload.ratio.dividend) &&
|
||||
equal_integer_integer(cell_a->payload.ratio.divisor,
|
||||
cell_b->payload.ratio.divisor);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
|
@ -11,36 +11,29 @@
|
|||
#ifndef __ratio_h
|
||||
#define __ratio_h
|
||||
|
||||
struct cons_pointer simplify_ratio( struct cons_pointer frame_pointer,
|
||||
struct cons_pointer arg );
|
||||
struct cons_pointer simplify_ratio( struct cons_pointer arg );
|
||||
|
||||
struct cons_pointer add_ratio_ratio( struct cons_pointer frame_pointer,
|
||||
struct cons_pointer arg1,
|
||||
struct cons_pointer add_ratio_ratio( struct cons_pointer arg1,
|
||||
struct cons_pointer arg2 );
|
||||
|
||||
struct cons_pointer add_integer_ratio( struct cons_pointer frame_pointer,
|
||||
struct cons_pointer intarg,
|
||||
struct cons_pointer add_integer_ratio( struct cons_pointer intarg,
|
||||
struct cons_pointer ratarg );
|
||||
|
||||
struct cons_pointer divide_ratio_ratio( struct cons_pointer frame_pointer,
|
||||
struct cons_pointer arg1,
|
||||
struct cons_pointer divide_ratio_ratio( struct cons_pointer arg1,
|
||||
struct cons_pointer arg2 );
|
||||
|
||||
struct cons_pointer multiply_ratio_ratio( struct cons_pointer frame_pointer, struct
|
||||
cons_pointer arg1, struct
|
||||
struct cons_pointer multiply_ratio_ratio( struct cons_pointer arg1, struct
|
||||
cons_pointer arg2 );
|
||||
|
||||
struct cons_pointer multiply_integer_ratio( struct cons_pointer frame_pointer,
|
||||
struct cons_pointer intarg,
|
||||
struct cons_pointer multiply_integer_ratio( struct cons_pointer intarg,
|
||||
struct cons_pointer ratarg );
|
||||
|
||||
struct cons_pointer subtract_ratio_ratio( struct cons_pointer frame_pointer,
|
||||
struct cons_pointer arg1,
|
||||
struct cons_pointer subtract_ratio_ratio( struct cons_pointer arg1,
|
||||
struct cons_pointer arg2 );
|
||||
|
||||
struct cons_pointer make_ratio( struct cons_pointer frame_pointer,
|
||||
struct cons_pointer dividend,
|
||||
struct cons_pointer make_ratio( struct cons_pointer dividend,
|
||||
struct cons_pointer divisor );
|
||||
|
||||
bool equal_ratio_ratio(struct cons_pointer a, struct cons_pointer b);
|
||||
|
||||
#endif
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue