Merge pull request #524 from yorickhardy/master

Implement r7rs round to even behaviour for half integers
This commit is contained in:
Justin Ethier 2024-02-02 21:31:52 -05:00 committed by GitHub
commit 5ea2fae5f8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 11 additions and 2 deletions

View file

@ -504,6 +504,7 @@ int Cyc_have_mstreams();
} \ } \
return_closcall1(data, cont, &d) return_closcall1(data, cont, &d)
double round_to_nearest_even(double);
void Cyc_exact(void *data, object cont, object z); void Cyc_exact(void *data, object cont, object z);
object Cyc_exact_no_cps(void *data, object ptr, object z); object Cyc_exact_no_cps(void *data, object ptr, object z);

View file

@ -8765,6 +8765,11 @@ int num2ratio(double x, double *numerator, double *denominator)
return 0; return 0;
} }
double round_to_nearest_even(double x)
{
return x-remainder(x,1.0);
}
/** /**
* Receive a Scheme number and pass requested portion of a rational number to * Receive a Scheme number and pass requested portion of a rational number to
* the continuation `cont`. Pass numerator if `numerator` is true, else the * the continuation `cont`. Pass numerator if `numerator` is true, else the

View file

@ -1372,9 +1372,9 @@
" return_double_op_no_cps(data, ptr, trunc, z);") " return_double_op_no_cps(data, ptr, trunc, z);")
(define-c round (define-c round
"(void *data, int argc, closure _, object k, object z)" "(void *data, int argc, closure _, object k, object z)"
" return_double_op(data, k, round, z); " " return_double_op(data, k, round_to_nearest_even, z); "
"(void *data, object ptr, object z)" "(void *data, object ptr, object z)"
" return_double_op_no_cps(data, ptr, round, z);") " return_double_op_no_cps(data, ptr, round_to_nearest_even, z);")
(define-c exact (define-c exact
"(void *data, int argc, closure _, object k, object z)" "(void *data, int argc, closure _, object k, object z)"
" Cyc_exact(data, k, z); " " Cyc_exact(data, k, z); "

View file

@ -91,6 +91,9 @@
(test 4.0 (ceiling 3.5)) (test 4.0 (ceiling 3.5))
(test 3.0 (truncate 3.5)) (test 3.0 (truncate 3.5))
(test 4.0 (round 3.5)) (test 4.0 (round 3.5))
(test 2.0 (round 2.5))
(test -4.0 (round -3.5))
(test -2.0 (round -2.5))
(test 4.0 (round 7/2)) ;; Rationals not supported, so result is inexact (test 4.0 (round 7/2)) ;; Rationals not supported, so result is inexact
(test 7 (round 7)) (test 7 (round 7))