Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: useful macros |
| Date | 2012-05-30 13:10 -0400 |
| Organization | A noiseless patient Spider |
| Message-ID | <4FC65479.7060004@verizon.net> (permalink) |
| References | <15aca137-fa13-441d-af65-60df91c88b04@googlegroups.com> <467b41e9-3c51-4052-959f-061c17e4babc@pr3g2000pbb.googlegroups.com> <9461b5b5-79b4-422c-a50c-1b2915629009@googlegroups.com> <0.bd19cf1c61d059f4f743.20120530164534BST.87ipfdbrkx.fsf@bsb.me.uk> |
On 05/30/2012 11:45 AM, Ben Bacarisse wrote: > Malcolm McLean <malcolm.mclean5@btinternet.com> writes: > >> בתאריך יום רביעי, 30 במאי 2012 04:26:11 UTC+1, מאת winndy: >>> On 5月27日, 上午4时08分, Malcolm McLean <malcolm.mcle...@btinternet.com> >>> wrote: ... >>>> uniform() - generate a random number on 0 - 1.0 minus epsilon ... >> I write these macros and put them at the top of pure ANSI source files. >> >> I didn't provide definitions because most regulars could easily write >> these macros in under a minute. > > The first is a little tricky. If you insist on absolute uniformity and > exactly the closed interval [0, 1-DBL_EPSILON], I'd have to work for > more than a minute. ... The wording is ambiguous; it could be interpreted as implying that the macro takes an argument named 'epsilon'. If so, the result should probably have the same type as 'epsilon', which would be the only reason I can come up with why a macro would be more appropriate than a function. The macro would have to use _Generic(). I suspect you're thinking about a function which can produce every representable number between 0 and nextafter(1.0, 0.0), with a uniform probability distribution. I agree, that would take a lot more than one minute to write up. However, I'd guess he was thinking of something along the lines of #define uniform() (rand()/(1.0 + RAND_MAX)) which can produce only RAND_MAX+1 different values, but those values can be uniformly distributed if RAND_MAX+1 is an integer power of FLT_RADIX and RAND_MAX*DBL_EPSILON < 1.0, which is often the case. Note: while 1 + DBL_EPSILON is, by definition, the same as nextafter(1.0, 2.0), 1 - DBL_EPSILON need not be the same as nextafter(1.0, 0.0), and usually won't be. The model for the representation of floating point values described in 5.2.4.2.2 is not mandatory; it's used solely for exposition of the requirements of that section. However, if it's a sufficiently accurate description of the actual representation of a double, then nextafter(1.0, 0.0) should be 1.0-DBL_EPSILON/FLT_RADIX.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
useful macros Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-05-26 13:08 -0700
Re: useful macros winndy <jiaoyk@gmail.com> - 2012-05-29 20:26 -0700
Re: useful macros Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-05-30 00:47 -0700
Re: useful macros Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-05-30 16:45 +0100
Re: useful macros James Kuyper <jameskuyper@verizon.net> - 2012-05-30 13:10 -0400
Re: useful macros Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-05-30 21:54 +0100
Re: useful macros "BartC" <bc@freeuk.com> - 2012-05-30 23:24 +0100
Re: useful macros James Kuyper <jameskuyper@verizon.net> - 2012-05-30 18:55 -0400
Re: useful macros "BartC" <bc@freeuk.com> - 2012-05-31 00:45 +0100
Re: useful macros Keith Thompson <kst-u@mib.org> - 2012-05-30 17:53 -0700
Re: useful macros "BartC" <bc@freeuk.com> - 2012-05-31 10:29 +0100
csiph-web