Thread: Re: [q-lang-users] [Chicken-users] Re: IEEE float arithmetic
Brought to you by:
agraef
From: John C. <co...@cc...> - 2006-06-20 23:31:25
|
Abdulaziz Ghuloum scripsit: > The test [for infinity] is obviously bogus since f==f+1.0 for large values of f. > (try 1e16) Oops, quite right. The expression "f != 0.0 && f == f + f" will work, I think. 0.0 and NaN are rejected by the left side of "&&", and all other finite values by the right side. -- Andrew Watt on Microsoft: John Cowan Never in the field of human computing co...@cc... has so much been paid by so many http://www.ccil.org/~cowan to so few! (pace Winston Churchill) |
From: John C. <co...@cc...> - 2006-06-21 04:30:05
|
Abdulaziz Ghuloum scripsit: > Out of curiosity, why not simply do "f==1.0/0.0 || f==-1.0/0.0" to test > if f is +inf.0/-inf.0? Umm. Too obvious, I guess. I started working these out in Chicken, where dividing by zero throws an exception, so I wanted some other method. But in C, of course, there's no such issue. -- One art / There is John Cowan <co...@cc...> No less / No more http://www.ccil.org/~cowan All things / To do With sparks / Galore -- Douglas Hofstadter |
From: Thomas C. <ch...@we...> - 2006-06-22 12:02:21
|
On Wed, 21 Jun 2006, John Cowan wrote: > Abdulaziz Ghuloum scripsit: > >> Out of curiosity, why not simply do "f==1.0/0.0 || f==-1.0/0.0" to test >> if f is +inf.0/-inf.0? > > Umm. Too obvious, I guess. I started working these out in > Chicken, where dividing by zero throws an exception, so I > wanted some other method. But in C, of course, there's no > such issue. [...] Hello, are you sure this works on every architecture? I dimly recall having seen systems where NaN != NaN holds, so you have to use the isnan predicate function from the C math library... cu, Thomas |
From: Albert G. <Dr....@t-...> - 2006-06-21 04:46:27
|
John Cowan wrote: > Abdulaziz Ghuloum scripsit: > >>Out of curiosity, why not simply do "f==1.0/0.0 || f==-1.0/0.0" to test >>if f is +inf.0/-inf.0? > > Umm. Too obvious, I guess. I started working these out in > Chicken, where dividing by zero throws an exception, so I > wanted some other method. But in C, of course, there's no > such issue. But you want the code at least not to do any harm on systems with _no_ IEEE floats. I'm not sure, but couldn't dividing by 0.0 on such systems kill your program with SIGFPE? Another point is that division is a much more costly operation than adding and subtracting. BTW, should I cc the Chicken list in this thread? -- Dr. Albert Gr"af Dept. of Music-Informatics, University of Mainz, Germany Email: Dr....@t-..., ag...@mu... WWW: http://www.musikinformatik.uni-mainz.de/ag |
From: John C. <co...@cc...> - 2006-06-21 04:49:38
|
Albert Graef scripsit: > But you want the code at least not to do any harm on systems with _no_ > IEEE floats. I'm not sure, but couldn't dividing by 0.0 on such systems > kill your program with SIGFPE? I don't know. I've never done serious floating-point work that had to run on a non-IEEE system (everything I did on a PDP-8, PDP-11, or pre-IEEE VAX was pretty much either integer-only or floats-as-integers, as in most Basics). > Another point is that division is a much more costly operation than > adding and subtracting. True. > BTW, should I cc the Chicken list in this thread? Might as well, though your postings might or might not go through. -- That you can cover for the plentiful John Cowan and often gaping errors, misconstruals, http://www.ccil.org/~cowan and disinformation in your posts co...@cc... through sheer volume -- that is another misconception. --Mike to Peter |
From: Albert G. <Dr....@t-...> - 2006-06-21 00:04:22
|
John Cowan wrote: > The expression "f != 0.0 && f == f + f" will work, I think. > 0.0 and NaN are rejected by the left side of "&&", and all > other finite values by the right side. Ok, I'll fix that, thanks. -- Dr. Albert Gr"af Dept. of Music-Informatics, University of Mainz, Germany Email: Dr....@t-..., ag...@mu... WWW: http://www.musikinformatik.uni-mainz.de/ag |
From: Albert G. <Dr....@t-...> - 2006-06-21 05:46:13
|
John Cowan wrote: > The expression "f != 0.0 && f == f + f" will work, I think. > 0.0 and NaN are rejected by the left side of "&&", and all > other finite values by the right side. Thinking about it some more I think that "!isnan(f) && isnan(f-f)" (given John's definition of isnan()) might be an even better way to do this. f-f can never be nan on an IEEE float system unless f is either nan or inf. And with non-IEEE floats this test should always fail because (presumably) f==f will always be true and hence isnan will always be false. Right? This test only relies on the algebraic properties of inf and nan and should be about the most efficient way to do it, too, as it just needs a single subtraction and two comparisons. -- Dr. Albert Gr"af Dept. of Music-Informatics, University of Mainz, Germany Email: Dr....@t-..., ag...@mu... WWW: http://www.musikinformatik.uni-mainz.de/ag |