Re: [q-lang-users] Q 7.2 RC2
Brought to you by:
agraef
From: John C. <co...@cc...> - 2006-06-26 15:45:42
|
Albert Graef scripsit: > Yes, I have to agree that 3/0 should now return inf instead of failing, > although that introduces a big incompatibility with earlier versions. :( I think it's one that everyone can live with. > I'm not so sure about 3%0, though. As I argue under item 6 below, for me > the right way to implement (%) on inexact numbers is the way it is done > now, i.e., to fall back to inexact division. But inexact division > returns (or rather will return when I fixed it) inf in this case. So > there is a case for having 3%0 evaluate to inf, too. X%0 should still > fail if X/0 does (the current definition isn't working correctly in that > case, but I've fixed that now). However, AFAICT this case wouldn't > actually arise any more. I now agree with this: % is not to be understood as exact division (of the div-and-rem sort), but exactness-contagion division, just as + - * are exactness-contagion operators: they return inexact results iff at least one argument is inexact. / on the other hand always produces inexact results. So both 3%0 and 3/0 should return inf. > Therefore, if (%) is to work on all numbers (which is a good thing, > IMHO) then the right way to do this is to fall back to inexact division > on inexact operands. This one was a consequence of my misunderstanding of %, and I agree it should be as you say. > Besides, there's also the issue that there's no single "canonical" way > to do the rational approximation. So I think that it's much better to > leave that to a separate module (like Rob's ratutils.q) which can do a > much more comprehensive job and provide different kinds of specialized > approximation algorithms. For the record, R5RS Scheme provides a "rationalize" procedure of two arguments which returns the simplest rational number that differs from X by no more than Y, and adds: A rational number r1 is simpler than another rational number r2 if r1 = p1/q1 and r2 = p2/q2 (in lowest terms) and |p1| <= |p2| and |q1| <= |q2|. Thus 3/5 is simpler than 4/7. Although not all rationals are comparable in this ordering (consider 2/7 and 3/5) any interval contains a rational number that is simpler than every other rational number in that interval (the simpler 2/5 lies between 2/7 and 3/5). Note that 0 = 0/1 is the simplest rational of all. Rationalize, like almost all R5RS procedures, is exactness-contagious. > > 7) This brings up the issue of coercions from exact to inexact (easy, > > just need a special case for complex numbers) and for inexact to exact. > > These should be in the library somewhere. > > In fact they are. There's float to convert any Real number to a Float, > there are round, trunc, floor and ceil to go the opposite direction, and > there are Rob's Float -> Rational approximation algorithms. If needed, > these can be applied to the real and imaginary components of Complex. > What else do you need? > > Granted, you can't just apply, say, round to Complex, but usually that > doesn't make mathematical sense anyway. The library cannot know whether > you'd prefer to round according to, e.g., the 1-, 2- or maybe the \infty > norm, that should be up to the application. R5RS uses the phrase "{inexact,exact} [representable] number that is numerically closest to the argument", presumably in the sense of having the smallest difference from the argument. Still, I agree that flexibility probably beats simplicity here, especially in the case of general complex numbers. -- John Cowan co...@cc... http://www.ccil.org/~cowan Is it not written, "That which is written, is written"? |