rational numbes and conversions
Hans-Martin Adorf <[email protected]> Wed, 22 Jul 2009 13:23:27 +0200
| Newsgroups | gmane.comp.java.sisc.user |
|---|---|
| Message-ID | <[email protected]> |
--===============7521766970910492839==
Content-Type: multipart/alternative; boundary=0016e64c2740a4103c046f499953
--0016e64c2740a4103c046f499953
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
Folks,
I'm having a hard time with SICS' rational numbers and conversions into
these.
I need to replace all occurences of decimal numbers (real numbers) by true
rational numbers or integers whatever is possible.
Examples for what I need
my-rationalize(1.0) -> 1
my-rationalize(1.2) -> 12/10
my-rationalize(1.3) -> 13/10
Originally I thought that the buildt-in procedure (rationalize exp 0) would
do the job, but that is not the case.
Here are SICS' responses to various procedure invocations:
(integer? 1.0) -> true (which actually is according to the standard)
(integer? 1.1) -> false
(rational? 1.0) -> true
(rational? 1.2) -> true
I find the latter response troublesome. How can I simply distinguish a
rational number from a real number?
(inexact->exact 1.0) -> 1
(inexact->exact 1.1) -> 110000000000000009/100000000000000000
(inexact->exact 1.2 -> 29999999999999999/25000000000000000
Particularly the last two responses are disconcerting in my mind. There are
exact rational representations for the simple real numbers, namely 11/10 and
12/10. Why are they not used?
(rationalize 1.0 0) -> 1.0
(rationalize 1.2 0) -> 1.2
(rationalize 1.3 0) -> 1.3
In order to proceed I have written my own 'my-rationalize' procedure (see
below) which takes the input real number to a string, analyzes it, and
converts the output to a rational number. It works, but is rather clumsy.
The my-rationalize procedure uses an auxiliary procedure' 'string-index-of'
which goes all the way to characters and lists in order to find the position
of the dot in the number string. The whole schlamassel works, but is clumsy.
What I would like to know are answers to the following questions:
(1) How can I solve the rationalization task elegantly in SISC?
(2) Is the solution portable?
(3) Is there a simple procedure to find the start position of a substring in
a given string?
Regards
Hans-Martin
(define (my-rationalize z1 z2)
(if (zero? z2)
(if (and (real? z1)
; (not (rational? z1)) ; SISC thinks that 1.2 is rational
(< -1 (string-index-of (number->string exp) ".")))
(let* ((z1-s (number->string z1))
(pos (string-index-of z1-s "."))
(pre-s (substring z1-s 0 pos))
(post-s (substring z1-s (+ pos 1) (string-length z1-s)))
(pre (string->number pre-s))
(post (string->number post-s))
(denom (string->number (string-append "1" (make-string
(string-length post-s) #\0))))
(result (+ pre (/ post denom))))
; (display "decimal number ") (display z1) (display " -> ")
(display result) (newline)
result)
z1)
(error 'rationalize "not implemented for non-zero second argument")))
--0016e64c2740a4103c046f499953
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Folks,<br><br>I'm having a hard time with SICS' rational numbers an=
d conversions into these.<br><br>I need to replace all occurences of decima=
l numbers (real numbers) by true rational numbers or integers whatever is p=
ossible.<br>
<br>Examples for what I need<br><br>my-rationalize(1.0) -> 1<br>my-ratio=
nalize(1.2) -> 12/10<br>my-rationalize(1.3) -> 13/10<br><br>Originall=
y I thought that the buildt-in procedure (rationalize exp 0) would do the j=
ob, but that is not the case.<br>
<br>Here are SICS' responses to various procedure invocations:<br><br>(=
integer? 1.0) -> true (which actually is according to the standard)<br>(=
integer? 1.1) -> false<br><br>(rational? 1.0) -> true<br>(rational? 1=
.2) -> true<br>
<br>I find the latter response troublesome. How can I simply distinguish a =
rational number from a real number?<br><br>(inexact->exact 1.0) -> 1<=
br>(inexact->exact 1.1) -> 110000000000000009/100000000000000000<br>
(inexact->exact 1.2 -> 29999999999999999/25000000000000000<br><br>Par=
ticularly the last two responses are disconcerting in my mind. There are ex=
act rational representations for the simple real numbers, namely 11/10 and =
12/10. Why are they not used?<br>
<br>(rationalize 1.0 0) -> 1.0<br>
(rationalize 1.2 0) -> 1.2<br>
(rationalize 1.3 0) -> 1.3<br><br>In order to proceed I have written my =
own 'my-rationalize' procedure (see below) which takes the input re=
al number to a string, analyzes it, and converts the output to a rational n=
umber. It works, but is rather clumsy. The my-rationalize procedure uses an=
auxiliary procedure' 'string-index-of' which goes all the way =
to characters and lists in order to find the position of the dot in the num=
ber string. The whole schlamassel works, but is clumsy.<br>
<br>What I would like to know are answers to the following questions:<br><b=
r>(1) How can I solve the rationalization task elegantly in SISC?<br>(2) Is=
the solution portable?<br>(3) Is there a simple procedure to find the star=
t position of a substring in a given string?<br>
<br>Regards<br>Hans-Martin<br><br><br>(define (my-rationalize z1 z2)<br>=A0=
(if (zero? z2)<br>=A0=A0=A0=A0=A0 (if (and (real? z1) <br>;=A0=A0=A0=A0=A0=
=A0=A0=A0=A0=A0=A0=A0=A0=A0 (not (rational? z1)) ; SISC thinks that 1.2 is =
rational<br>=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 (< -1 (string-ind=
ex-of (number->string exp) ".")))<br>
=A0=A0=A0=A0=A0=A0=A0=A0=A0 (let* ((z1-s (number->string z1))<br>=A0=A0=
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 (pos (string-index-of z1-s "=
;."))<br>=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 (pre-s (subs=
tring z1-s 0 pos))<br>=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 (pos=
t-s (substring z1-s (+ pos 1) (string-length z1-s)))<br>
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 (pre (string->number pr=
e-s))<br>=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 (post (string->=
;number post-s))<br>=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 (denom=
(string->number (string-append "1" (make-string (string-lengt=
h post-s) #\0))))<br>
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 (result (+ pre (/ post den=
om))))<br>;=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 (display "decimal number =
") (display z1) (display " -> ") (display result) (newlin=
e) <br>=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 result)<br>=A0=A0=A0=A0=A0=A0=A0=
=A0=A0 z1)<br>=A0=A0=A0=A0=A0 (error 'rationalize "not implemented=
for non-zero second argument")))<br>
--0016e64c2740a4103c046f499953--
--===============7521766970910492839==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
------------------------------------------------------------------------------
--===============7521766970910492839==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
_______________________________________________
Sisc-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/sisc-users
--===============7521766970910492839==--