How to code the expression with least roundoff error

In summary: A possible approach would be to calculate $$z^m \ln \frac{z}{z-1} - \sum_{k=1}^m \frac{z^{m-k}}{k}$$Summing smaller numbers first will reduce truncation error, so this may be better.In summary, the expression to code is: (z complex, m some positive integer)Z_1=1.0_q/Z ZV1=LOG(Z/(Z-1)) ZK=1.0_q DO K=1,M; ZK=ZK*Z_1
  • #1
bsmile
48
2
The expression to code is: (z complex, m some positive integer)

z^m ( ln(z/(z-1)) - sum( 1/(k z^k), {k=1,m} ) )

The way I code is (in fortran) (in case z<2)

Z_1=1.0_q/Z

ZV1=LOG(Z/(Z-1))
ZK=1.0_q
DO K=1,M; ZK=ZK*Z_1
ZV1=ZV1-ZK/K
ENDDO
ZV1=ZV1/ZK

But it gives quite big error by this bruteforce way of coding. Can anybody help me out to suggest a better way to code it?

Thanks,
 
Technology news on Phys.org
  • #2
I would do the sum first, then subtract it from the logarithm. Another possibility is to calculate
$$
z^m \ln \frac{z}{z-1} - \sum_{k=1}^m \frac{z^{m-k}}{k}
$$
Depending on the value of z and m, one approach might be better than the other.
 
  • #3
Summing smaller numbers first will reduce truncation error, so this may be better. I assume the common factor z^m can be placed outside the sum, even if z is complex?

##z^m ( \ln \frac{z}{z-1} - \sum_{k=m}^1 \frac{z^{-k}}{k})##
 
  • #4
Thanks for your help. I wonder whether the issue comes from Log[z/(z-1)]. To test it, I evaluated two equivalent expressions with Mathematica making use of the following identity

ln(z/(z-1))=∫^{1}_{0} 1/(z-x) dx

Ideally, the difference between left and right hand sides should be zero, but Mathematica tells me otherwise, as shown from the attached figure.

Can you explain to me why as usually we would assume the elementary functions have been extremely well carried out as built-in functions and should attain the highest machine precision?

(how to upload a figure?)
 
  • #5
bsmile said:
(how to upload a figure?)
Below the text box, to the right, you should find a botton marked "UPLOAD".
 
  • #6
DrClaude said:
Below the text box, to the right, you should find a botton marked "UPLOAD".

Thanks, I had been clicking on the figure icon on top of the textbox, and it really didn't do what I wanted. Here is the figure showing the two otherwise equivalent expressions have error bigger than machine precision and has a strong dependence on the input parameters.

I understand the precision of the output from the built-in functions will have a dependence on the input parameters, but they should do at least what we ordinary people can achieve. Obviously if |z| is small, we should be able to make it more accurate by using taylor expansion than what gfortran
comp.jpg
returns us.
 
  • #7
One thing I might mention is that the lack of accuracy can be in NIntegrate as well as it might fail to reach prescribed precision goal I set. I have written to Mathematica to see whether there is way to detect the condition satisfied or not.
 
  • #8
bsmile said:
One thing I might mention is that the lack of accuracy can be in NIntegrate as well as it might fail to reach prescribed precision goal I set. I have written to Mathematica to see whether there is way to detect the condition satisfied or not.
Have you set WorkingPrecision as well as PrecisionGoal?
 
  • #9
bsmile said:
The expression to code is: (z complex, m some positive integer)

z^m ( ln(z/(z-1)) - sum( 1/(k z^k), {k=1,m} ) )

The way I code is (in fortran) (in case z<2)

Z_1=1.0_q/Z

ZV1=LOG(Z/(Z-1))
ZK=1.0_q
DO K=1,M; ZK=ZK*Z_1
ZV1=ZV1-ZK/K
ENDDO
ZV1=ZV1/ZK

But it gives quite big error by this bruteforce way of coding. Can anybody help me out to suggest a better way to code it?

Thanks,
I have forgotten all the FORTRAN I once knew (good riddance!) but I do remember some hard-to-track-down bugs due to not forcing double precision - particularly with complex variables.
 

FAQ: How to code the expression with least roundoff error

1. What is roundoff error in coding?

Roundoff error is the difference between the exact value of a number and the approximate value that is used in calculations. It occurs due to the limitations of representing real numbers in a computer's binary system, leading to small discrepancies in calculations.

2. How can I minimize roundoff error when coding expressions?

To minimize roundoff error, it is important to use data types with higher precision, such as double or long double, instead of float. Additionally, using mathematical operations that are less prone to rounding errors, such as addition and subtraction rather than multiplication and division, can help reduce roundoff error.

3. Can rounding mode affect the roundoff error in coding expressions?

Yes, the rounding mode can have an impact on the roundoff error in coding expressions. Rounding modes determine how a number is rounded when it cannot be represented exactly in the chosen data type. Choosing the correct rounding mode, such as rounding to the nearest even number, can help minimize roundoff error.

4. How can I test for roundoff error in my code?

There are various methods for testing roundoff error in code, such as comparing the results of the same calculation using different data types or rounding modes. It is also helpful to use test cases with known exact values to compare against the calculated results.

5. Are there any tools or libraries that can help with minimizing roundoff error in coding expressions?

Yes, there are tools and libraries available that can help minimize roundoff error in coding expressions. For example, the GNU Multiple Precision Arithmetic Library (GMP) provides high-precision arithmetic operations, and the IEEE Standard for Floating-Point Arithmetic (IEEE 754) specifies recommended methods for implementing floating-point arithmetic to minimize roundoff error.

Similar threads

Replies
4
Views
1K
Replies
7
Views
3K
Replies
20
Views
2K
Replies
1
Views
3K
Replies
10
Views
25K
Replies
5
Views
2K
Replies
8
Views
3K
Replies
4
Views
3K
Back
Top