This is the mail archive of the
kawa@sources.redhat.com
mailing list for the Kawa project.
inexact->exact
- From: Marco Vezzoli <marco dot vezzoli at st dot com>
- To: kawa <kawa at sources dot redhat dot com>
- Date: Fri, 08 Nov 2002 10:31:29 +0100
- Subject: inexact->exact
I tried the sci function posted on c.l.s whith some tests (code and
testsuite below); function runs fine for non zero values.
With zeroes inexact->exact throws an ArithmeticException: i traced it
but couldn't manage to repeat it in a simpler example.
----------the trace-----------------
#|kawa:72|# (sci 0.0 -3)
call to inexact->exact (-3)
return from inexact->exact => -3
call to negative? (-3)
return from negative? => #t
call to negative? (0.0)
return from negative? => #f
call to abs (0.0)
return from abs => 0.0
call to log (0.0)
return from log => #i-1/0
call to log (10.0)
return from log => 2.302585092994046
call to inexact->exact (#i-1/0)
return from inexact->exact => -1/0
call to round (#i0/0)
return from round => #i0/0
call to log (#i0/0)
return from log => #i0/0
call to log (10.0)
return from log => 2.302585092994046
call to inexact->exact (#i0/0)
procedure inexact->exact throws exception java.lang.ArithmeticException:
cannot convert NaN to exact rational
-------------the code------------------------------
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Function for representing a number
;; in scientific format.
;;
;; See examples below. It is similar to
;; Python's sci function, except that
;; (sci 2.334 1.45) for example is
;; used as (sci 2.334 1). Python rises
;; an error if your exponent is inexact.
;;
;; (C) Siegfried Gonzi 2002
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (sci f n)
(let* ((n (inexact->exact n))
(n (if (negative? n) 0 n))
(n? (negative? f))
(f (abs f))
(log10 (lambda(x) (/ (log x) (log 10.0))))
(base-f (lambda(x)
(inexact->exact (floor (log10 x))))))
(let* ((exp-n (expt 10.0 n))
(base-old (base-f f))
(f (* f (expt 10.0 (- base-old))))
(f (/ (round (* f exp-n)) exp-n))
(base-new (base-f f))
(base-string (number->string (abs (+ base-old base-new))))
(f-string (if (> n 0)
(number->string (* f (expt 10.0 (- base-new))))
(number->string (inexact->exact (* f (expt 10.0
(- base-new)))))))
(zeros? (if (> n 0)
(- n (- (string-length f-string) 2))
0)))
(string-append (if n? "-" "")
f-string
(make-string zeros? #\0)
"e"
(if (negative? base-old)
"-"
"+")
(make-string (- 3 (string-length base-string)) #\0)
base-string))))
--------------the testsuite-----------------------
;commented tests throw exceptions
(load "sci.scm")
(load "testing.scm")
(test-init "Fix" 17)
;(test-init "Sci" 17)
(test "1.0e-001" sci 0.09999 1)
(test "2.0e+000" sci 1.9999 1)
(test "1.0e+001" sci 9.999 1)
(test "9.99999000e-004" sci 0.000999999 8)
;(test "0e+000" sci 0.0 0 )
(test "-1.0e-001" sci -0.0999 1)
(test "1.9999000e+000" sci 1.9999 7)
(test "-2e-004" sci -0.0002 0)
(test "2e-003" sci 0.002 -3)
;(test "0e+000" sci 0.0 -3)
(test "-1.0e-002" sci -0.01 1)
(test
"-1.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000e-002"
sci -0.01 100)
(test "2.33e+021" sci 2334443414433223531144.922343 2)
(test "3.000e+004" sci 3e4 3)
(test "2.33445552555440000000000000000000000000000000000000000000e+021"
sci 2334455525554435345543.43324434334 56)
(test
"2.343340000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000e+236"
sci 234.334e234 123)
;(test "0e+000" sci -0e3 0)
(test-report)
--
Marco Vezzoli tel. +39 039 603 6852
STMicroelectronics fax. +39 039 603 5055