# [gambit-list] Which function in libm are portable enough to assume that they will be on all platforms?

Thu Oct 31 11:01:48 EDT 2013

```On Oct 31, 2013, at 10:40 AM, Marc Feeley wrote:

>
>
> On Oct 31, 2013, at 9:40 AM, Bradley Lucier <lucier at math.purdue.edu> wrote:
>
>>
>> On Oct 31, 2013, at 8:16 AM, Marc Feeley wrote:
>>
>>>
>>> If you define
>>>
>>> (define (flsinh x)
>>> (fl* 0.5 (fl- (flexp x) (flexp (fl- x)))))
>>>
>>> (define (flcosh x)
>>> (fl* 0.5 (fl+ (flexp x) (flexp (fl- x)))))
>>>
>>> (define (fltanh x)
>>> (let ((a (flexp (fl* 2.0 x))))
>>>  (fl/ (fl- a 1.0) (fl+ a 1.0))))
>>>
>>> can you use those functions to improve the precision of complex asin and acos?
>>
>> No
>>
>>> Can these Scheme definitions be improved?
>>
>> Yes

Here's an example for sinh:

If x < 0, return -sinh(-x)

There are numbers 0 < x_0 < x_1 < x_2 < x_3 < +inf.0 such that

If 0<x<x_0 then use a power series about 0  (avoids catastrophic cancelation)

If x_0 < x < x_1 return (e^x-e^{-x})/2

If x_1 < x < x_2 return e^x/2

If x_2< x < x_3 return e^{x-1}*(e/2)   (avoids spurious overflow)

If x_3 < x return +inf.0

And that's if you're satisfied with an error of up to about 2 ulps.

And you have to figure out what x_i, i=0,1,2,3 are and what the power series is.

This is somewhat what fdlibm, glibc, eglibc, ..., Mac OS's library, ... do.  Only they probably use better formulas than I just put up there.

I don't know that all platforms do this.  But I think "most" do.  And even the ones who don't, probably do as well as a quick and dirty definition like you gave above for sinh does.

>>
>>> And how good is the precision when compared to the C sinh and cosh?
>>
>> Crap.
>>
>> Marc, I'm too busy right now to prepare a lecture on this stuff.
>>
>
>
> On Oct 30, 2013, at 11:30 PM, Bradley Lucier <lucier at math.purdue.edu> wrote:
>
>> So my question is---is it reasonable to import these floating-point functions into Gambit to make better versions of the standard functions?
>
> I think it is "reasonable" if both
>
> 1) most C libm implementations have good precision sinh, etc (where "good" means better than the above portable definitions)

Yes, they do.  "Most" includes linux, mac os x.  I don't know what else it includes.  I'd like to know about iOS and Android.

>
> 2) in case the C libm sinh, etc have poor precision, the use of the above portable definitions in the definition of complex asin and acos gives no worse precision than the current implementation in Gambit.

I'm sorry, I don't understand this sentence.

```