Adding support for complex literals in exponential format.

This commit is contained in:
Alex Shinn 2012-11-11 12:26:03 +09:00
parent 9b0f9c73f2
commit 44c643c7cf
2 changed files with 28 additions and 7 deletions

27
sexp.c
View file

@ -2320,16 +2320,37 @@ sexp sexp_read_float_tail (sexp ctx, sexp in, double whole, int negp) {
for (; c==SEXP_PLACEHOLDER_DIGIT; c=sexp_read_char(ctx, in), scale*=0.1) for (; c==SEXP_PLACEHOLDER_DIGIT; c=sexp_read_char(ctx, in), scale*=0.1)
val += sexp_placeholder_digit_value(10)*scale; val += sexp_placeholder_digit_value(10)*scale;
#endif #endif
val += whole;
if (negp) val *= -1;
if (is_precision_indicator(c)) { if (is_precision_indicator(c)) {
c2 = sexp_read_char(ctx, in); c2 = sexp_read_char(ctx, in);
if (c2 != '+') sexp_push_char(ctx, c2, in); if (c2 != '+') sexp_push_char(ctx, c2, in);
exponent = sexp_read_number(ctx, in, 10); exponent = sexp_read_number(ctx, in, 10);
if (sexp_exceptionp(exponent)) return exponent; if (sexp_exceptionp(exponent)) {
sexp_gc_release1(ctx);
return exponent;
}
#if SEXP_USE_COMPLEX
if (sexp_complexp(exponent)) {
res = exponent;
exponent = (sexp_complex_real(res) == SEXP_ZERO ? sexp_complex_imag(res) : sexp_complex_real(res));
}
#endif
e = (sexp_fixnump(exponent) ? sexp_unbox_fixnum(exponent) e = (sexp_fixnump(exponent) ? sexp_unbox_fixnum(exponent)
: sexp_flonump(exponent) ? sexp_flonum_value(exponent) : 0.0); : sexp_flonump(exponent) ? sexp_flonum_value(exponent) : 0.0);
#if SEXP_USE_COMPLEX
if (sexp_complexp(res)) {
if (sexp_complex_real(res) == SEXP_ZERO) {
sexp_complex_imag(res) = sexp_make_flonum(ctx, val * pow(10, e));
} else {
sexp_complex_real(res) = sexp_make_flonum(ctx, val * pow(10, e));
}
sexp_gc_release1(ctx);
return res;
}
#endif
} }
val = (whole + val) * pow(10, e); if (e != 0.0) val *= pow(10, e);
if (negp) val *= -1;
#if SEXP_USE_FLONUMS #if SEXP_USE_FLONUMS
res = sexp_make_flonum(ctx, val); res = sexp_make_flonum(ctx, val);
#else #else

View file

@ -1546,10 +1546,10 @@
;; Decimal-notation complex numbers (rectangular notation) ;; Decimal-notation complex numbers (rectangular notation)
("1.0+2i" (make-rectangular 1.0 2) "1.0+2.0i" "1.0+2i" "1.+2i" "1.+2.i") ("1.0+2i" (make-rectangular 1.0 2) "1.0+2.0i" "1.0+2i" "1.+2i" "1.+2.i")
("1+2.0i" (make-rectangular 1 2.0) "1.0+2.0i" "1+2.0i" "1.+2.i" "1+2.i") ("1+2.0i" (make-rectangular 1 2.0) "1.0+2.0i" "1+2.0i" "1.+2.i" "1+2.i")
;; ("1e2+1.0i" (make-rectangular 100.0 1.0) "100.0+1.0i" "100.+1.i") ("1e2+1.0i" (make-rectangular 100.0 1.0) "100.0+1.0i" "100.+1.i")
;; ("1s2+1.0i" (make-rectangular 100.0 1.0) "100.0+1.0i" "100.+1.i") ("1s2+1.0i" (make-rectangular 100.0 1.0) "100.0+1.0i" "100.+1.i")
;; ("1.0+1e2i" (make-rectangular 1.0 100.0) "1.0+100.0i" "1.+100.i") ("1.0+1e2i" (make-rectangular 1.0 100.0) "1.0+100.0i" "1.+100.i")
;; ("1.0+1s2i" (make-rectangular 1.0 100.0) "1.0+100.0i" "1.+100.i") ("1.0+1s2i" (make-rectangular 1.0 100.0) "1.0+100.0i" "1.+100.i")
;; Fractional complex numbers (rectangular notation) ;; Fractional complex numbers (rectangular notation)
("1/2+3/4i" (make-rectangular (/ 1 2) (/ 3 4))) ("1/2+3/4i" (make-rectangular (/ 1 2) (/ 3 4)))
;; Mixed fractional/decimal notation complex numbers (rectangular notation) ;; Mixed fractional/decimal notation complex numbers (rectangular notation)