Skip to content

Commit a0acfa1

Browse files
committed
Added examples in the doc.
1 parent 11d0b21 commit a0acfa1

33 files changed

+696
-95
lines changed

Makefile.in

+2-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ host_triplet = @host@
110110
subdir = .
111111
ACLOCAL_M4 = $(top_srcdir)/aclocal.m4
112112
am__aclocal_m4_deps = $(top_srcdir)/m4/ax_check_enable_debug.m4 \
113-
$(top_srcdir)/m4/ax_gcc_builtin.m4 $(top_srcdir)/m4/libtool.m4 \
113+
$(top_srcdir)/m4/ax_gcc_builtin.m4 \
114+
$(top_srcdir)/m4/ax_is_release.m4 $(top_srcdir)/m4/libtool.m4 \
114115
$(top_srcdir)/m4/ltoptions.m4 $(top_srcdir)/m4/ltsugar.m4 \
115116
$(top_srcdir)/m4/ltversion.m4 $(top_srcdir)/m4/lt~obsolete.m4 \
116117
$(top_srcdir)/m4/va_opt_check.m4 $(top_srcdir)/configure.ac

aclocal.m4

+1
Original file line numberDiff line numberDiff line change
@@ -1428,6 +1428,7 @@ AC_SUBST([am__untar])
14281428

14291429
m4_include([m4/ax_check_enable_debug.m4])
14301430
m4_include([m4/ax_gcc_builtin.m4])
1431+
m4_include([m4/ax_is_release.m4])
14311432
m4_include([m4/libtool.m4])
14321433
m4_include([m4/ltoptions.m4])
14331434
m4_include([m4/ltsugar.m4])

config.h.in

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
/* config.h.in. Generated from configure.ac by autoheader. */
22

3+
/* Define this to 1 if you want info code */
4+
#undef FPNGL_ENABLE_INFO
5+
36
/* Define to 1 if you have the `clock' function. */
47
#undef HAVE_CLOCK
58

configure

+17
Original file line numberDiff line numberDiff line change
@@ -3043,6 +3043,18 @@ fi
30433043

30443044

30453045

3046+
3047+
3048+
# $is_release = (.git directory does not exist)
3049+
if test -d ${srcdir}/.git || (test -f ${srcdir}/.git && grep \.git/worktrees ${srcdir}/.git); then :
3050+
ax_is_release=no
3051+
else
3052+
ax_is_release=yes
3053+
fi
3054+
3055+
3056+
3057+
30463058
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking whether to enable debugging" >&5
30473059
$as_echo_n "checking whether to enable debugging... " >&6; }
30483060

@@ -3134,6 +3146,11 @@ $as_echo "#define NDEBUG 1" >>confdefs.h
31343146
fi
31353147
ax_enable_debug=$enable_debug
31363148

3149+
if test x$ax_enable_debug = xinfo; then
3150+
3151+
$as_echo "#define FPNGL_ENABLE_INFO 1" >>confdefs.h
3152+
3153+
fi
31373154

31383155

31393156

configure.ac

+4
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ AC_INIT([fpnglib],m4_esyscmd([tr -d '\n' < VERSION]),[frederic.goualard@univ-nan
2424
dnl For Automake
2525
AM_INIT_AUTOMAKE([gnu 1.7 dist-bzip2])
2626

27+
AX_IS_RELEASE([git-directory])
2728
AX_CHECK_ENABLE_DEBUG([yes])
29+
if test x$ax_enable_debug = xinfo; then
30+
AC_DEFINE([FPNGL_ENABLE_INFO],[1],[Define this to 1 if you want info code])
31+
fi
2832

2933
PKG_CHECK_MODULES([CHECK], [check >= 0.11.0])
3034

doc/Makefile.in

+2-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ host_triplet = @host@
110110
subdir = doc
111111
ACLOCAL_M4 = $(top_srcdir)/aclocal.m4
112112
am__aclocal_m4_deps = $(top_srcdir)/m4/ax_check_enable_debug.m4 \
113-
$(top_srcdir)/m4/ax_gcc_builtin.m4 $(top_srcdir)/m4/libtool.m4 \
113+
$(top_srcdir)/m4/ax_gcc_builtin.m4 \
114+
$(top_srcdir)/m4/ax_is_release.m4 $(top_srcdir)/m4/libtool.m4 \
114115
$(top_srcdir)/m4/ltoptions.m4 $(top_srcdir)/m4/ltsugar.m4 \
115116
$(top_srcdir)/m4/ltversion.m4 $(top_srcdir)/m4/lt~obsolete.m4 \
116117
$(top_srcdir)/m4/va_opt_check.m4 $(top_srcdir)/configure.ac

doc/fpnglib-figures/classes.jpg

13.3 KB
Loading

doc/fpnglib.info

+154-30
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,10 @@ const uint32_t seed = 13;
137137

138138
int main(void)
139139
{
140-
fpngl_irng32_t *irng32 = fpngl_mt19937v32(seed);
141-
printf("%u\n",fpngl_irng32_next32(irng32));
142-
printf("%lu\n",fpngl_irng32_next64(irng32));
143-
fpngl_irng32_delete(irng32);
140+
fpngl_irng32_t *irng32 = fpngl_mt19937v32(seed);
141+
printf("%u\n",fpngl_irng32_next32(irng32));
142+
printf("%lu\n",fpngl_irng32_next64(irng32));
143+
fpngl_irng32_delete(irng32);
144144
}
145145

146146
Assuming you have installed FPNGlib locally in some directory
@@ -221,9 +221,9 @@ const uint32_t seed = 13;
221221

222222
int main(void)
223223
{
224-
fpngl_irng32_t *irng32 = fpngl_mt19937v32(seed);
225-
printf("%u\n",(fpngl_irng32_next32(irng32) % 6) + 1);
226-
fpngl_irng32_delete(irng32);
224+
fpngl_irng32_t *irng32 = fpngl_mt19937v32(seed);
225+
printf("%u\n",(fpngl_irng32_next32(irng32) % 6) + 1);
226+
fpngl_irng32_delete(irng32);
227227
}
228228

229229
However, since 2^{32} = 6\times 715827882 + 4, there are 715827883
@@ -248,9 +248,9 @@ const uint32_t seed = 13;
248248

249249
int main(void)
250250
{
251-
fpngl_irng_t *irng = fpngl_irng_new32(fpngl_mt19937v32(seed));
252-
printf("%u\n",fpngl_ubound32(irng,6) + 1);
253-
fpngl_irng_delete(irng);
251+
fpngl_irng_t *irng = fpngl_irng_new32(fpngl_mt19937v32(seed));
252+
printf("%u\n",fpngl_ubound32(irng,6) + 1);
253+
fpngl_irng_delete(irng);
254254
}
255255
Remember that the function 'fpngl_mt19937v32' creates a 32 bits RNG
256256
with type 'fpngl_irng32_t'. The 'fpngl_ubound32' function computes a
@@ -304,16 +304,108 @@ const uint32_t seed = 13;
304304

305305
int main(void)
306306
{
307-
fpngl_frng64_t *frng64 = fpngl_java(seed);
308-
printf("%f\n",fpngl_frng64_nextf64(frng64));
309-
fpngl_frng64_delete(frng64);
307+
fpngl_frng64_t *frng64 = fpngl_java(seed);
308+
printf("%a\n",fpngl_frng64_nextf64(frng64));
309+
fpngl_frng64_delete(frng64);
310310
}
311311

312+
The output expressed using the C99 hexadecimal format(2) is
313+
"'0x1.314880e9bdp-10'."
314+
315+
Floating-point number generators ("FRNG"s, for short) are either of
316+
the type 'fpngl_frng32_t' when they produce single precision floats, or
317+
'fpngl_frng64_t' for double precision. In the same way as for integer
318+
generators, floats generators can only be manipulated through pointers
319+
to objects of these types. Any generator of either type must implement
320+
the same functionalities. In particular, as shown in the example below,
321+
double precision generators must implement the following calls:
322+
* 'fpgnl_frng64_name()': returns a constant string corresponding to
323+
the name of the generator;
324+
* 'fpngl_fnrg64_nextf64()': returns the next double precision random
325+
number;
326+
* 'fpngl_frng64_delete()': reclaims the memory used to represent the
327+
generator.
328+
As a consequence, it is possible to write functions requiring random
329+
floats independently of the FRNG actually used, and to pass it as a
330+
parameter (see, e.g., 'mcpi' in *note Example 2.1: exa:mcpi.).
331+
332+
Note, however, that FRNGs are not bound to return floats in any
333+
prescribed domain: some may return values in [0,1), some in (0,1), and
334+
some others in any other domain. It is then the responsibility of the
335+
programmer to choose FRNGs according to the domains they are interested
336+
into.
337+
338+
A classical way to implement FRNGs is to rely on an integer RNG and
339+
divide its output by some integer constant, usually the largest possible
340+
integer the RNG can produce or the next integer above. This is what we
341+
do to create 'frng0' in *note Example 2.1: exa:mcpi. Firstly, we create
342+
an IRNG based on the 32 bits version of the Mersenne Twister MT19937,
343+
then we create and FRNG by using the 'fpngl_bydivision_new' constructor.
344+
That constructor takes three parameters:
345+
1. A name for the newly created FRNG ("'FMT19937v32'" in *note Example
346+
2.1: exa:mcpi.);
347+
2. An IRNG of the type 'pfngl_irng_t';
348+
3. A divisor by which each random integer should be divided (here, we
349+
choose to divide by the largest random number that our IRNG can
350+
produce, therefore creating a FRNG returning values in [0,1]).
351+
352+
Note that, as is always the case in FPNGlib, the FRNG 'frng0' created
353+
from the IRNG 'irng32' "owns" it and is, therefore, responsible for its
354+
destruction. This is why we do not call 'fpngl_irng_delete' at the end
355+
of the program: the call 'fpngl_frng64_delete(frng0)' is sufficient to
356+
reclaim both the memory allocated to represent 'frng0' and 'irng32'.
357+
358+
The FRNG created with 'fpngl_drand48bsd' also relies on the division
359+
of the result of an LCG by a constant integer. The result is a
360+
floating-point number in [0,1) with 48 bits of entropy only.
361+
362+
// doc/snippets/mcpi.c
363+
#include <stdio.h>
364+
#include <math.h>
365+
#include <fpnglib/frng64_division.h>
366+
#include <fpnglib/mt19937ar.h>
367+
#include <fpnglib/irng_t.h>
368+
369+
#define NITERS 10000000
370+
const uint64_t seed = 13;
371+
// Approximation of pi to double precision
372+
const double m_pi = 0x1.921fb54442d18p+1;
373+
374+
// Estimating the value of pi by throwing darts at a unit circle.
375+
double mcpi(fpngl_frng64_t *frng, uint32_t niters)
376+
{
377+
uint32_t in_circle = 0;
378+
for (uint32_t i = 0; i < niters; ++i) {
379+
double x = fpngl_frng64_nextf64(frng);
380+
double y = fpngl_frng64_nextf64(frng);
381+
in_circle += ((x*x + y*y) <= 1);
382+
}
383+
return 4*((double)in_circle / niters);
384+
}
385+
386+
int main(void)
387+
{
388+
fpngl_irng_t *irng32 = fpngl_irng_new32(fpngl_mt19937v32(seed));
389+
fpngl_frng64_t *frng0 = fpngl_bydivision_new("FMT19937v32",
390+
irng32,
391+
fpngl_irng_max(irng32));
392+
fpngl_frng64_t *frng1 = fpngl_drand48bsd(seed);
393+
printf("%s: %.16g\n",fpngl_frng64_name(frng0), mcpi(frng0,NITERS));
394+
printf("%s: %.16g\n",fpngl_frng64_name(frng1), mcpi(frng1,NITERS));
395+
fpngl_frng64_delete(frng0);
396+
fpngl_frng64_delete(frng1);
397+
}
398+
399+
Example 2.1: Computing an approximation to \pi with a Monte Carlo
400+
method.
401+
312402
---------- Footnotes ----------
313403

314404
(1) IEEE Standard for Floating-Point Arithmetic. IEEE STD 754-2008.
315405
IEEE Computer Society, 2008.
316406

407+
(2) See Section 6.4.4.2 of ISO/IEC 9899:1999.
408+
317409

318410
File: fpnglib.info, Node: Generating a random float with some properties, Prev: Generating Random Floating-Point Numbers, Up: An Introduction to FPNGlib
319411

@@ -368,10 +460,10 @@ precision.
368460

369461
int main(void)
370462
{
371-
printf("Single precision exponent in [%d, %d]\n",
372-
fpngl_emin32,fpngl_emax32);
373-
printf("Double precision exponent in [%d, %d]\n",
374-
fpngl_emin64,fpngl_emax64);
463+
printf("Single precision exponent in [%d, %d]\n",
464+
fpngl_emin32,fpngl_emax32);
465+
printf("Double precision exponent in [%d, %d]\n",
466+
fpngl_emin64,fpngl_emax64);
375467
}
376468

377469
*Output:*
@@ -392,10 +484,10 @@ precision.
392484

393485
int main(void)
394486
{
395-
printf("Number of digits with single precision: %u\n",
396-
(uint32_t)trunc(fpngl_t32*log10(2)));
397-
printf("Number of digits with double precision: %u\n",
398-
(uint32_t)trunc(fpngl_t64*log10(2)));
487+
printf("Number of digits with single precision: %u\n",
488+
(uint32_t)trunc(fpngl_t32*log10(2)));
489+
printf("Number of digits with double precision: %u\n",
490+
(uint32_t)trunc(fpngl_t64*log10(2)));
399491
}
400492

401493
*Output:*
@@ -413,6 +505,30 @@ easy to extend.
413505

414506
figure 7.1: The "classes" of FPNGlib
415507

508+
7.1 Debugging facilities
509+
========================
510+
511+
FPNGlib offers facilities to debug itself. There are in particular some
512+
macros that can be used to display messages to the standard error ouput
513+
'stderr', provided the library was configured with the option
514+
'--enable-debug=yes' (the default).
515+
516+
-- Macro: FPNGL_DEBUG ...
517+
Uses 'fprintf' to output its arguments to 'stderr' if the macro
518+
'NDEBUG' is defined to '1'. Otherwise, it does nothing.
519+
520+
The macro flushes the output buffer with 'fflush' after each call.
521+
522+
Example:
523+
// doc/snippets/debug.c
524+
#include <fpnglib/debug.h>
525+
526+
int main(void)
527+
{
528+
double a = 1.0;
529+
FPNGL_DEBUG("This will be displayed only in debug mode: %g\n",a);
530+
}
531+
416532
8 Concept Index
417533
***************
418534

@@ -435,6 +551,12 @@ figure 7.1: The "classes" of FPNGlib
435551
9 Function Index
436552
****************
437553

554+
[index]
555+
* Menu:
556+
557+
* FPNGL_DEBUG: Generating a random float with some properties.
558+
(line 107)
559+
438560
10 Type Index
439561
*************
440562

@@ -443,14 +565,16 @@ figure 7.1: The "classes" of FPNGlib
443565
Tag Table:
444566
Node: An Introduction to FPNGlib4774
445567
Node: Generating Random Integers5439
446-
Ref: Generating Random Integers-Footnote-18416
447-
Node: Generating Random Integers in a Domain8684
448-
Ref: Generating Random Integers in a Domain-Footnote-111682
449-
Ref: Generating Random Integers in a Domain-Footnote-211755
450-
Ref: Generating Random Integers in a Domain-Footnote-311887
451-
Node: Generating Random Floating-Point Numbers11981
452-
Ref: Generating Random Floating-Point Numbers-Footnote-112932
453-
Node: Generating a random float with some properties13034
454-
Ref: fig:classes16137
568+
Ref: Generating Random Integers-Footnote-18420
569+
Node: Generating Random Integers in a Domain8688
570+
Ref: Generating Random Integers in a Domain-Footnote-111692
571+
Ref: Generating Random Integers in a Domain-Footnote-211765
572+
Ref: Generating Random Integers in a Domain-Footnote-311897
573+
Node: Generating Random Floating-Point Numbers11991
574+
Ref: exa:mcpi15634
575+
Ref: Generating Random Floating-Point Numbers-Footnote-116922
576+
Ref: Generating Random Floating-Point Numbers-Footnote-217024
577+
Node: Generating a random float with some properties17074
578+
Ref: fig:classes20197
455579

456580
End Tag Table

0 commit comments

Comments
 (0)