Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Floating Point; Division vs Multiplication

You have 2 functions;

f(x)= x(((x+1)^(1/2))-(x^(1/2)))    
g(x)= x/(((x+1)^(1/2))+(x^(1/2)))

Which one is more accurate?

Side note: If you could explain why, that would really help me out, I have a feeling it's f(x) as there isn't a denominator but I'm not 100% certain.

like image 893
Claire Blackman Avatar asked Jan 29 '15 18:01

Claire Blackman


People also ask

Is floating point multiplication faster than division?

Multiplication is faster than division.

Is multiplication faster than division on computers?

Yes, indeed, there is a difference. A loop with a million multiplies by 0.5 took 0.11 seconds and a loop with a million divides by 2 took 1.6 seconds. So it's true for the RPG (and probably for the IBM i) that multiplying is quicker than dividing.

Is division A floating point operation?

Arithmetic operations on floating point numbers consist of addition, subtraction, multiplication and division.

What is the difference between floating point division and integer division?

The division operator / means integer division if there is an integer on both sides of it. If one or two sides has a floating point number, then it means floating point division.


1 Answers

Floating-point arithmetic is quite different from real mathematics. In particular, floating-point arithmetic is not associative or distributive. Therefore mathematically equivalent expressions are not necessarily equivalent when evaluated with finite-precision floating-point arithmetic. This is the case even when individual operations, such as addition, subtraction, multiplication, division, and square root produce correctly rounded results, as required by the IEEE-754 floating-point standard.

In this case g() will be much more accurate than f() on average, and also for the specific case of x = 500. The reason is that f() suffers from subtractive cancellation. This occurs during effective subtraction of two floating-point numbers that are almost identical in magnitude. The leading digits cancel during the subtraction, leaving only a few remaining trailing digits that enter into subsequent computation. Also, any rounding error accumulated in the trailing digits of the original operands being subtracted may be magnified by subsequent computation. An expanded explanation with example can be found in this Wikipedia article.

In this case, sqrt(x+1) and sqrt(x) are almost the same magnitude, in particular as the magnitude of x increases. Using the example of x = 500 and employing IEEE-754 single-precision arithmetic, we find:

x = 500   f(x) = 0x1.659ae0p+3 (11.175156)   reference = 0x1.659798p+3 (11.174755)
x = 500   g(x) = 0x1.659798p+3 (11.174755)   reference = 0x1.659798p+3 (11.174755)

The error in f(500) is 420 ulps, while g(500) delivers the correctly rounded single-precision result.

like image 75
3 revs Avatar answered Oct 20 '22 18:10

3 revs