module scheme import math fn parent_complex_value(mut vm VM, source string) Value { return vm.eval(source) or { panic('${source}: ${err}') } } fn test_exact_angle_keeps_imaginary_sign_when_ratio_underflows_to_zero() { mut vm := new() for source in [ '(angle (make-rectangular (- (expt 2 1100)) 1))', '(angle (make-rectangular -1 (/ 1 (expt 2 1100))))', '(angle (make-rectangular -1 0))', '(angle (- (expt 2 1100)))', ] { assert parent_complex_value(mut vm, source).as_real() or { panic(err) } == math.pi, source } for source in [ '(angle (make-rectangular (- (expt 2 1100)) -1))', '(angle (make-rectangular -1 (- (/ 1 (expt 2 1100)))))', ] { assert parent_complex_value(mut vm, source).as_real() or { panic(err) } == -math.pi, source } assert vm.eval_alt('angle(make-rectangular(-1,1 / expt(2,1100)))') or { panic(err) }.as_real() or { panic(err) } == math.pi } fn test_complex_components_work_with_strict_arithmetic_and_canonical_numeric_hashing() { mut vm := new() result := parent_complex_value(mut vm, '(import (rnrs arithmetic fixnums) (rnrs arithmetic flonums) (rnrs arithmetic bitwise) (scheme complex)) (define z (make-rectangular (bitwise-arithmetic-shift-left 1 120) (fx+ 20 22))) (define table (make-eqv-hashtable)) (hashtable-set! table z 42) (list (fx+ (real-part (make-rectangular 20 22)) 22) (fl+ (real-part (make-rectangular 20.0 22)) 22.0) (= (bitwise-and (real-part z) (- (expt 2 121) 1)) (expt 2 120)) (hashtable-ref table (make-rectangular (expt 2 120) 42) #f))') assert write(result) or { panic(err) } == '(42 42.0 #t 42)' } fn test_strict_arithmetic_rejects_nonreal_values_and_recovers() { mut vm := new() for source in ['(fx+ 1+2i 1)', '(fl+ 1.0 1+2i)', '(bitwise-and 0 1+2i)', '(real->flonum 1+2i)'] { if value := vm.eval(source) { assert false, '${source}: ${value.kind()}' } assert vm.active.len == 0 assert parent_complex_value(mut vm, '(fx+ 20 22)').whole() == 42 } } fn test_complex_atan_extremes_preserve_subnormal_imaginary_parts_and_quadrant_symmetry() { mut vm := new() make := vm.lookup('make-rectangular') or { panic(err.msg()) } atan := vm.lookup('atan') or { panic(err.msg()) } for size, expected in { 1e308: 5e-309 1e154: 5e-155 } { for xsign in [-1.0, 1.0] { for ysign in [-1.0, 1.0] { input := vm.call(make, [real(size * xsign), real(size * ysign)]) or { panic(err.msg()) } answer := inexact_complex(vm.call(atan, [input]) or { panic(err.msg()) }) or { panic(err.msg()) } assert answer.re == xsign * math.pi / 2 assert math.abs(answer.im - ysign * expected) <= math.max(math.abs(expected) * 4e-15, math.f64_from_bits(4)) assert answer.im != 0 } } } assert (vm.eval_alt('imag-part(atan(make-rectangular(1e308,1e308)))') or { panic(err.msg()) }).as_real() or { panic(err.msg()) } == 5e-309 } fn test_complex_atan_underflow_tie_retains_a_representable_minimum_subnormal() { mut vm := new() make := vm.lookup('make-rectangular') or { panic(err.msg()) } atan := vm.lookup('atan') or { panic(err.msg()) } y := math.f64_from_bits(0x0040000000000001) for xsign in [-1.0, 1.0] { for ysign in [-1.0, 1.0] { input := vm.call(make, [real(268435456.0 * xsign), real(y * ysign)]) or { panic(err.msg()) } answer := inexact_complex(vm.call(atan, [input]) or { panic(err.msg()) }) or { panic(err.msg()) } assert answer.im == ysign * math.f64_from_bits(1) } } }