Different results for tan(pi/2) and tanpi(1/2)

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Different results for tan(pi/2) and tanpi(1/2)

Hans W Borchers-2
As the subject line says, we get different results for tan(pi/2) and
tanpi(1/2), though this should not be the case:

    > tan(pi/2)
    [1] 1.633124e+16

    > tanpi(1/2)
    [1] NaN
    Warning message:
    In tanpi(1/2) : NaNs produced

By redefining tanpi with sinpi and cospi, we can get closer:

    > tanpi <- function(x) sinpi(x) / cospi(x)

    > tanpi(c(0, 1/2, 1, 3/2, 2))
    [1]    0  Inf    0 -Inf    0

Hans Werner

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Different results for tan(pi/2) and tanpi(1/2)

John McKown
On Fri, Sep 9, 2016 at 12:24 PM, Hans W Borchers <[hidden email]>
wrote:

> As the subject line says, we get different results for tan(pi/2) and
> tanpi(1/2), though this should not be the case:
>
>     > tan(pi/2)
>     [1] 1.633124e+16
>
>     > tanpi(1/2)
>     [1] NaN
>     Warning message:
>     In tanpi(1/2) : NaNs produced
>
> By redefining tanpi with sinpi and cospi, we can get closer:
>
>     > tanpi <- function(x) sinpi(x) / cospi(x)
>
>     > tanpi(c(0, 1/2, 1, 3/2, 2))
>     [1]    0  Inf    0 -Inf    0
>
> Hans Werner
>
>
>
​When I do a ?tanpi, I see the following:

     ‘tanpi(0.5)’ is ‘NaN’.  Similarly for other inputs with fractional
     part ‘0.5’.



​I don't know why this is, but apparently the function is working as
documented. Whether that is correct or not is not for me to say.​



--
Unix: Some say the learning curve is steep, but you only have to climb it
once. -- Karl Lehenbauer
Unicode: http://xkcd.com/1726/

Maranatha! <><
John McKown

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Different results for tan(pi/2) and tanpi(1/2)

R devel mailing list
In reply to this post by Hans W Borchers-2
It should be the case that tan(pi*x) != tanpi(x) in many cases - that is
why it was added.  The limits from below and below of the real function
tan(pi*x) as x approaches 1/2 are different, +Inf and -Inf, so the limit is
not well defined.   Hence the computer function tanpi(1/2) ought to return
Not-a-Number.

Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Fri, Sep 9, 2016 at 10:24 AM, Hans W Borchers <[hidden email]>
wrote:

> As the subject line says, we get different results for tan(pi/2) and
> tanpi(1/2), though this should not be the case:
>
>     > tan(pi/2)
>     [1] 1.633124e+16
>
>     > tanpi(1/2)
>     [1] NaN
>     Warning message:
>     In tanpi(1/2) : NaNs produced
>
> By redefining tanpi with sinpi and cospi, we can get closer:
>
>     > tanpi <- function(x) sinpi(x) / cospi(x)
>
>     > tanpi(c(0, 1/2, 1, 3/2, 2))
>     [1]    0  Inf    0 -Inf    0
>
> Hans Werner
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Different results for tan(pi/2) and tanpi(1/2)

Hans W Borchers-2
The same argument would hold for tan(pi/2).
I don't say the result 'NaN' is wrong,
but I thought,
tan(pi*x) and tanpi(x) should give the same result.

Hans Werner


On Fri, Sep 9, 2016 at 8:44 PM, William Dunlap <[hidden email]> wrote:

> It should be the case that tan(pi*x) != tanpi(x) in many cases - that is why
> it was added.  The limits from below and below of the real function
> tan(pi*x) as x approaches 1/2 are different, +Inf and -Inf, so the limit is
> not well defined.   Hence the computer function tanpi(1/2) ought to return
> Not-a-Number.
>
> Bill Dunlap
> TIBCO Software
> wdunlap tibco.com
>
> On Fri, Sep 9, 2016 at 10:24 AM, Hans W Borchers <[hidden email]>
> wrote:
>>
>> As the subject line says, we get different results for tan(pi/2) and
>> tanpi(1/2), though this should not be the case:
>>
>>     > tan(pi/2)
>>     [1] 1.633124e+16
>>
>>     > tanpi(1/2)
>>     [1] NaN
>>     Warning message:
>>     In tanpi(1/2) : NaNs produced
>>
>> By redefining tanpi with sinpi and cospi, we can get closer:
>>
>>     > tanpi <- function(x) sinpi(x) / cospi(x)
>>
>>     > tanpi(c(0, 1/2, 1, 3/2, 2))
>>     [1]    0  Inf    0 -Inf    0
>>
>> Hans Werner
>>
>> ______________________________________________
>> [hidden email] mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Different results for tan(pi/2) and tanpi(1/2)

R devel mailing list
tanpi(x) should be more accurate than tan(pi*x), especially near multiples
of pi/2.

Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Fri, Sep 9, 2016 at 11:55 AM, Hans W Borchers <[hidden email]>
wrote:

> The same argument would hold for tan(pi/2).
> I don't say the result 'NaN' is wrong,
> but I thought,
> tan(pi*x) and tanpi(x) should give the same result.
>
> Hans Werner
>
>
> On Fri, Sep 9, 2016 at 8:44 PM, William Dunlap <[hidden email]> wrote:
> > It should be the case that tan(pi*x) != tanpi(x) in many cases - that is
> why
> > it was added.  The limits from below and below of the real function
> > tan(pi*x) as x approaches 1/2 are different, +Inf and -Inf, so the limit
> is
> > not well defined.   Hence the computer function tanpi(1/2) ought to
> return
> > Not-a-Number.
> >
> > Bill Dunlap
> > TIBCO Software
> > wdunlap tibco.com
> >
> > On Fri, Sep 9, 2016 at 10:24 AM, Hans W Borchers <[hidden email]>
> > wrote:
> >>
> >> As the subject line says, we get different results for tan(pi/2) and
> >> tanpi(1/2), though this should not be the case:
> >>
> >>     > tan(pi/2)
> >>     [1] 1.633124e+16
> >>
> >>     > tanpi(1/2)
> >>     [1] NaN
> >>     Warning message:
> >>     In tanpi(1/2) : NaNs produced
> >>
> >> By redefining tanpi with sinpi and cospi, we can get closer:
> >>
> >>     > tanpi <- function(x) sinpi(x) / cospi(x)
> >>
> >>     > tanpi(c(0, 1/2, 1, 3/2, 2))
> >>     [1]    0  Inf    0 -Inf    0
> >>
> >> Hans Werner
> >>
> >> ______________________________________________
> >> [hidden email] mailing list
> >> https://stat.ethz.ch/mailman/listinfo/r-devel
> >
> >
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Different results for tan(pi/2) and tanpi(1/2)

glsnow
In reply to this post by Hans W Borchers-2
If pi were stored and computed to infinite precision then yes we would
expect tan(pi/2) to be NaN, but computers in general and R
specifically don't store to infinite precision (some packages allow
arbitrary (but still finite) precision) and irrational numbers cannot
be stored exactly.  So you take the value of the built in variable pi,
which is close to the theoretical value, but not exactly equal, divide
it by 2 which could reduce the precision, then pass that number (which
is not equal to the actual irrational value where tan has a
discontinuity) to tan and tan returns its best estimate.

Using finite precision approximations to irrational and other numbers
that cannot be stored exactly can have all types of problems at and
near certain values, that is why there are many specific functions for
calculating in those regions.





On Fri, Sep 9, 2016 at 12:55 PM, Hans W Borchers <[hidden email]> wrote:

> The same argument would hold for tan(pi/2).
> I don't say the result 'NaN' is wrong,
> but I thought,
> tan(pi*x) and tanpi(x) should give the same result.
>
> Hans Werner
>
>
> On Fri, Sep 9, 2016 at 8:44 PM, William Dunlap <[hidden email]> wrote:
>> It should be the case that tan(pi*x) != tanpi(x) in many cases - that is why
>> it was added.  The limits from below and below of the real function
>> tan(pi*x) as x approaches 1/2 are different, +Inf and -Inf, so the limit is
>> not well defined.   Hence the computer function tanpi(1/2) ought to return
>> Not-a-Number.
>>
>> Bill Dunlap
>> TIBCO Software
>> wdunlap tibco.com
>>
>> On Fri, Sep 9, 2016 at 10:24 AM, Hans W Borchers <[hidden email]>
>> wrote:
>>>
>>> As the subject line says, we get different results for tan(pi/2) and
>>> tanpi(1/2), though this should not be the case:
>>>
>>>     > tan(pi/2)
>>>     [1] 1.633124e+16
>>>
>>>     > tanpi(1/2)
>>>     [1] NaN
>>>     Warning message:
>>>     In tanpi(1/2) : NaNs produced
>>>
>>> By redefining tanpi with sinpi and cospi, we can get closer:
>>>
>>>     > tanpi <- function(x) sinpi(x) / cospi(x)
>>>
>>>     > tanpi(c(0, 1/2, 1, 3/2, 2))
>>>     [1]    0  Inf    0 -Inf    0
>>>
>>> Hans Werner
>>>
>>> ______________________________________________
>>> [hidden email] mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-devel
>>
>>
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel



--
Gregory (Greg) L. Snow Ph.D.
[hidden email]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Different results for tan(pi/2) and tanpi(1/2)

R devel mailing list
Other examples of functions like this are log1p(x), which is log(1+x)
accurate for small x, and expm1(x), which is exp(x)-1 accurate for small
x.  E.g.,
  > log1p( 1e-20 )
  [1] 1e-20
  > log( 1 + 1e-20 )
  [1] 0
log itself cannot be accurate here because the problem is that 1 == 1 +
1e-20 in double precision arithmetic (52 binary digits of precision).



Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Fri, Sep 9, 2016 at 12:58 PM, Greg Snow <[hidden email]> wrote:

> If pi were stored and computed to infinite precision then yes we would
> expect tan(pi/2) to be NaN, but computers in general and R
> specifically don't store to infinite precision (some packages allow
> arbitrary (but still finite) precision) and irrational numbers cannot
> be stored exactly.  So you take the value of the built in variable pi,
> which is close to the theoretical value, but not exactly equal, divide
> it by 2 which could reduce the precision, then pass that number (which
> is not equal to the actual irrational value where tan has a
> discontinuity) to tan and tan returns its best estimate.
>
> Using finite precision approximations to irrational and other numbers
> that cannot be stored exactly can have all types of problems at and
> near certain values, that is why there are many specific functions for
> calculating in those regions.
>
>
>
>
>
> On Fri, Sep 9, 2016 at 12:55 PM, Hans W Borchers <[hidden email]>
> wrote:
> > The same argument would hold for tan(pi/2).
> > I don't say the result 'NaN' is wrong,
> > but I thought,
> > tan(pi*x) and tanpi(x) should give the same result.
> >
> > Hans Werner
> >
> >
> > On Fri, Sep 9, 2016 at 8:44 PM, William Dunlap <[hidden email]>
> wrote:
> >> It should be the case that tan(pi*x) != tanpi(x) in many cases - that
> is why
> >> it was added.  The limits from below and below of the real function
> >> tan(pi*x) as x approaches 1/2 are different, +Inf and -Inf, so the
> limit is
> >> not well defined.   Hence the computer function tanpi(1/2) ought to
> return
> >> Not-a-Number.
> >>
> >> Bill Dunlap
> >> TIBCO Software
> >> wdunlap tibco.com
> >>
> >> On Fri, Sep 9, 2016 at 10:24 AM, Hans W Borchers <[hidden email]>
> >> wrote:
> >>>
> >>> As the subject line says, we get different results for tan(pi/2) and
> >>> tanpi(1/2), though this should not be the case:
> >>>
> >>>     > tan(pi/2)
> >>>     [1] 1.633124e+16
> >>>
> >>>     > tanpi(1/2)
> >>>     [1] NaN
> >>>     Warning message:
> >>>     In tanpi(1/2) : NaNs produced
> >>>
> >>> By redefining tanpi with sinpi and cospi, we can get closer:
> >>>
> >>>     > tanpi <- function(x) sinpi(x) / cospi(x)
> >>>
> >>>     > tanpi(c(0, 1/2, 1, 3/2, 2))
> >>>     [1]    0  Inf    0 -Inf    0
> >>>
> >>> Hans Werner
> >>>
> >>> ______________________________________________
> >>> [hidden email] mailing list
> >>> https://stat.ethz.ch/mailman/listinfo/r-devel
> >>
> >>
> >
> > ______________________________________________
> > [hidden email] mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-devel
>
>
>
> --
> Gregory (Greg) L. Snow Ph.D.
> [hidden email]
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel