Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

Highlighted
##

zp3

Beginner

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

08-19-2017
06:39 AM

57 Views

implementing LOG1P

Hi,

As everyone knows, log(1+dx) suffers heavily on cancellation errors when dx gets sufficient small. The solution is, as in most other programming languages already implemented, the use of the log1p() function. One common way to implement a workaround DIY is the rewriting of log(1+dx) as:

log1p=log(1+dx)*dx/((1-dx)-1)

The problem with this now is, that most compilers probably optimize it simply back to log(1+dx). Therefore I'd like to ask if there is a safe way to tell the compiler to just not touch this line of code?

Thanks for any advice!

2 Replies

Highlighted
##

mecej4

Black Belt

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

08-19-2017
07:42 AM

57 Views

The formula that you listed has a sign error. Rather than use a trick, why not use the C-library function log1p(), or write code such as

if(abs(dx) < 1d-8)then log1p = dx*(1d0 - 0.5d0*dx) else log1p = log(1d0 + dx) endif

See also https://www.johndcook.com/blog/2010/06/07/math-library-functions-that-seem-unnecessary/ .

Highlighted
##

TimP

Black Belt

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

08-19-2017
08:05 AM

57 Views

Most compilers have options to respect parentheses, such as ifort -standard-semantics .

For more complete information about compiler optimizations, see our Optimization Notice.