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
|
|
@ -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;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue