Re: rational numbes and conversions
"Scott G. Miller" <[email protected]> Thu, 23 Jul 2009 11:55:13 -0500
| Newsgroups | gmane.comp.java.sisc.user |
|---|---|
| Message-ID | <[email protected]> |
--===============1014824350424902772== Content-Type: multipart/alternative; boundary=0016e6464656f356e0046f6259ac --0016e6464656f356e0046f6259ac Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit On Wed, Jul 22, 2009 at 6:23 AM, Hans-Martin Adorf <[email protected]>wrote: > Folks, > > I'm having a hard time with SICS' rational numbers and conversions into > these. > <snip> > > (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? > This is rational by the mathematical definition, ie a number that can be expressed as the quotient of two integers. An irrational number fails this test, like pi or an imaginary 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? > This is due to the inherent inexactness of floating point representations themselves. The calculations used to convert to an exact ratio come up against these limitations and produce results that may not be what you expect. Specifically, internal to SISC, we convert the numerator to an integer using Java's BigDecimal class, and internally it is relying on floating point math. > (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. > Yep, you can work around this using string analysis, but as you say, its clumsy. SISC doesn't bother because we know that inexact numbers are inexact, so we prefer the fast solution to what 'feels' like the correct one. > > 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? > 1) Perhaps not, depending on the definition of elegantly 2) Depends on the solution. :) 3) Look at SRFI-13, which is included in SISC. It has a string-index function that'll do what you want. Cheers, Scott --0016e6464656f356e0046f6259ac Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable On Wed, Jul 22, 2009 at 6:23 AM, Hans-Martin Adorf <span dir=3D"ltr"><<a= href=3D"mailto:[email protected]">[email protected]</a>></spa= n> wrote:<br><div class=3D"gmail_quote"><blockquote class=3D"gmail_quote" s= tyle=3D"border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8e= x; padding-left: 1ex;"> Folks,<br><br>I'm having a hard time with SICS' rational numbers an= d conversions into these.<br></blockquote><div><br><snip> <br></div><= blockquote class=3D"gmail_quote" style=3D"border-left: 1px solid rgb(204, 2= 04, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"> <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></blockquote><div><br>This is ration= al by the mathematical definition, ie a number that can be expressed as the= quotient of two integers.=A0 An irrational number fails this test, like pi= or an imaginary number. <br> =A0<br></div><blockquote class=3D"gmail_quote" style=3D"border-left: 1px so= lid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><br>= (inexact->exact 1.0) -> 1<br>(inexact->exact 1.1) -> 1100000000= 00000009/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> </blockquote><div><br>This is due to the inherent inexactness of floating p= oint representations themselves.=A0 The calculations used to convert to an = exact ratio come up against these limitations and produce results that may = not be what you expect. Specifically, internal to SISC, we convert the nume= rator to an integer using Java's BigDecimal class, and internally it is= relying on floating point math.<br> =A0<br></div><blockquote class=3D"gmail_quote" style=3D"border-left: 1px so= lid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">(rat= ionalize 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> </blockquote><div><br>Yep, you can work around this using string analysis, = but as you say, its clumsy.=A0 SISC doesn't bother because we know that= inexact numbers are inexact, so we prefer the fast solution to what 'f= eels' like the correct one. <br> =A0<br></div><blockquote class=3D"gmail_quote" style=3D"border-left: 1px so= lid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><br>= What I would like to know are answers to the following questions:<br><br>(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 th= e start position of a substring in a given string?<br> </blockquote><div><br><br>1) Perhaps not, depending on the definition of el= egantly<br>2) Depends on the solution. :)<br>3) Look at SRFI-13, which is i= ncluded in SISC.=A0 It has a string-index function that'll do what you = want.<br> <br>Cheers,<br>=A0Scott<br></div></div> --0016e6464656f356e0046f6259ac-- --===============1014824350424902772== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline ------------------------------------------------------------------------------ --===============1014824350424902772== 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 --===============1014824350424902772==--