I added real->float in the math functions in the science collection that I'll call.<br><br>;;; (real->float x) -> inexact-real?<br>;;; x : real?<br>;;; Returns an inexact real (i.e., a float) given real x. Raises an error if x<br>
;;; is not a real. This can be used to ensure a real value is a float, even in<br>;;; unsafe code.<br>(define (real->float x)<br> (if (real? x)<br> (exact->inexact x)<br> (error "expected real, given" x)))<br>
<br>I'll use it to protect unsafe code. I'm sure it's more overhead than putting it in-line, but hopefully not too much. Putting a contract on it would probably not make much sense.<br><br><div class="gmail_quote">
On Sun, Oct 4, 2009 at 4:27 PM, Matthew Flatt <span dir="ltr"><<a href="mailto:mflatt@cs.utah.edu">mflatt@cs.utah.edu</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div class="im">At Sun, 4 Oct 2009 14:46:48 -0400, Doug Williams wrote:<br>
> When you use mutable data structures, you live with the choice. For the<br>
> statistics routines, I use exact->inexact inside the loop at the point where<br>
> I use the value, so I'm not worried about it. Off the top of my head, the<br>
> only problem I see is that exact->inexact also works on complex numbers, so<br>
> I may still not have a simple float.<br>
<br>
</div>Right. You'd need a `real?' or (after conversion) `inexact-real?' test.<br>
<br>
In the `mean-and-variance' example, I changed `(exact->inexact x)' in<br>
the loop to `(if (real? x) (exact->inexact x) 0.0)'. The overhead of<br>
the test was just 1 msec out of 90 msec, so that's probably a good<br>
option for preserving safety.<br>
<div class="im"><br>
> I assume +inf.0, -inf.0, and +nan.0<br>
> (and, therefore, -nan.0) also pass through exact->inexact. I further assume<br>
> those are stored as floats (in an IEEE format) and work as expected - at<br>
> least they seem to in the REPL. Is that a correct assumption?<br>
<br>
</div>Yes.<br>
<div class="im"><br>
> Are there<br>
> other cases where exact->inexact does not give me a float?<br>
<br>
</div>No, the only issue is complex numbers.<br>
<div><div></div><div class="h5"><br>
> On Sun, Oct 4, 2009 at 12:53 PM, Matthew Flatt <<a href="mailto:mflatt@cs.utah.edu">mflatt@cs.utah.edu</a>> wrote:<br>
><br>
> > At Sat, 3 Oct 2009 08:34:03 -0600, Matthew Flatt wrote:<br>
> > > (while the contract on the "checked" version ensures that the unsafe<br>
> > > operations will not cause a crash).<br>
> ><br>
> > Not true. Sam points out that a vector (or other sequence) can be<br>
> > mutable, so checking elements at the beginning does not make `variance'<br>
> > safe if it uses unsafe operations on the elements internally.<br>
> ><br>
> ><br>
> ><br>
</div></div></blockquote></div><br>