c++ - Computer precision: when should I have to worry about it? -
in c++ programming, when need worry precision issue? take small example (it might not perfect 1 though),
std::vector<double> first (50000, 0.0); std::vector<double> second (first);
could possible second[619] = 0.00000000000000000000000000001234
(i mean small value). or sum = second[0]+second[1]+...+second[49999] => 1e-31
? or sum = second[0]-second[1]-...-second[49999] => -7.987654321e-12
?
my questions:
- could small disturbances in working
double
type numbers? - what may cause these kind of small disturbances? i.e. rounding errors become large? please list them? how take precautions?
- if there small disturbance in operations, mean after these operations, using
if (sum == 0)
dangerous? 1 should useif (sum < small)
instead,small
defined small value, such1e-30
? - lastly, small disturbances result negative value? because if possible, should better use
if (abs(sum) < small)
instead.
any experiences?
this reference document floating point precision: what every computer scientist should know floating-point arithmetic
one of more important parts catastrophic cancellation
catastrophic cancellation occurs when operands subject rounding errors. example in quadratic formula, expression b2 - 4ac occurs. quantities b2 , 4ac subject rounding errors since results of floating-point multiplications. suppose rounded nearest floating-point number, , accurate within .5 ulp. when subtracted, cancellation can cause many of accurate digits disappear, leaving behind digits contaminated rounding error. hence difference might have error of many ulps. example, consider b = 3.34, = 1.22, , c = 2.28. exact value of b2 - 4ac .0292. b2 rounds 11.2 , 4ac rounds 11.1, hence final answer .1 error 70 ulps, though 11.2 - 11.1 equal .16. subtraction did not introduce error, rather exposed error introduced in earlier multiplications.
benign cancellation occurs when subtracting known quantities. if x , y have no rounding error, theorem 2 if subtraction done guard digit, difference x-y has small relative error (less 2).
a formula exhibits catastrophic cancellation can rearranged eliminate problem. again consider quadratic formula
Comments
Post a Comment