delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1994/01/10/05:21:11

Date: Mon, 10 Jan 94 11:18 GMT
From: mjo AT mrao DOT cam DOT ac DOT uk (Martin Oldfield)
To: djgpp AT sun DOT soe DOT clarkson DOT edu
Subject: tanh
Status: RO

>I've found a problem with the tanh function in djgpp 1.11. As x gets
>bigger, tanh(x) should approach 1.0. e.g. running a trivial program on
>a Sun yields:
>
>tanh(2.71828) =     0.991329
>tanh(7.38906) =     0.999999
>tanh(20.0855) =            1
...
>tanh(22026.5) =            1

>Running the same problem on djgpp generates a fp exception as it
>calculates the last line. 

The problem you are having is with fp overflow.  Recall that tanh is
defined in terms of e^x and e^(-x).  Now, while e^(-x) approaches 0,
e^x diverges rapidly.  e^22026 is about 10^9565, so your fix is to
write a wrapper function that returns 1.0 if x is greater than about 20.

   Andy


- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019