
Boost : 
From: Paul A. Bristow (boost_at_[hidden])
Date: 20031111 05:24:15
You will find some potentially useful background on _runtime_ floating point
comparisons (a minefield?) in the test library documentation:
boost_1_30_2\libs\test\doc\floating_point_comparison.htm
implemmented in
boost_1_30_2\boost\test\floating_point_comparison.hpp
Some references from previous work:
[1] Knuth D.E. The art of computer programming (vol II).
[2] David Goldberg What Every Computer Scientist Should Know About
FloatingPoint Arithmetic
[3] Kulisch U. Rounding near zero.
[4] Philippe Langlois From Rounding Error Estimation to Automatic Correction
with Automatic Differentiation
[5] Lots of information on William Kahan home page.
[4] Alberto Squassabia Comparing Floats: How To Determine if Floating Quantities
Are Close Enough Once a Tolerance Has Been Reached C++ Report March 2000.
[5] Pete Becker The Journeyman's Shop: Trap Handlers, Sticky Bits, and
FloatingPoint Comparisons C/C++ Users Journal December 2000.
HTH
Paul
PS As you have discovered, C++ doesn't give much help with compile time FP :(
Paul A Bristow, Prizet Farmhouse, Kendal, Cumbria, LA8 8AB UK
+44 1539 561830 Mobile +44 7714 33 02 04
mailto:pbristow_at_[hidden]
 Original Message
 From: boostbounces_at_[hidden]
 [mailto:boostbounces_at_[hidden]]On Behalf Of Andy Little
 Sent: Monday, November 10, 2003 10:40 AM
 To: boost_at_[hidden]
 Subject: [boost] comparing computed f.p.values ( including powers) at
 compiletime


 Hi,
 In my quest for the right operator semantics in my physical_quantity
 class I came up against the need to compare
 computed f.p. values in a metafunction.

 The calc looks something like this (simplified here)

 float const x_v1;
 float const x_p1,
 float const z_v1;

 float const x_v2;
 float const x_p2,
 float const z_v2;

 float const scale1 = pow(x_v1,x_p1) * z_v1;
 float const scale2 = pow(x_v2,x_p2) * z_v2;

 then the runtime equiv of the metafunction
 bool isbigger (scale1,scale2);

 Obviously this cant be computed at compile time, without a lot of
 complexities

 however if the f.p. value x is represented by an integer constant where
 log_x = log(x) * multiplier // multiplier == (say) 1e6
 the calc turns into:

 static int const log_scale1 = (log_xv1 * x_p1) + log_zv1;
 static int const log_scale2 = (log_xv2 * x_p2) + log_zv1;

 which of course can be computed and compared at compile time:

 static bool const is_log_bigger = log_scale1 > log_scale2;

 Of course there are a lot of downsides, (most of which are soluble with more
 work of course)
 the f.p constants must be +ve;
 The multiplier needs to be as big as possible not to lose resolution,
 but small enough not to cause overflow, and of course the sizeof int makes
 it platform dependent.

 Therefore it needs to be used with great caution to avoid hard to spot bugs
 etc.

 However if there is no other way it provides a rough and ready method of
 performing the required cmp.
 (The limitations could probably be reduced with some "metafunction
 assembler". ie ADC etc, to create bigger ints,sign etc)

 Apologies if this is as old as the hills.
 BTW If the technique is in use I would certainly be interested in any docs
 as to its use in practise.

 regards
 Andy little












 _______________________________________________
 Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk