Hi, I'm seeing an asymmetry in how approxfun treats -Inf and Inf, which I don't understand from reading the help file. e.g. a simple step function tmp <- approxfun(x=c(-Inf, -0.2, 0.2, Inf), y=c(-1, -1, 1, 1)) tmp(c(-1, 0, 1)) # [1] NaN 0 1 I expected: # [1] -1 0 1 Clearly I can work round this by using type=2, and not specifying the end points (or by replacing -Inf by a large negative number), but I was wondering what I was missing about the different behaviour between positive and negative infinity. To simplify further: approxfun(x=c(-1000, Inf), y=c(1, 1))(0) # [1] 1 approxfun(x=c(-Inf, 1000), y=c(1, 1))(0) # [1] NaN Sessioninfo: R version 4.3.0 (2023-04-21 ucrt) -- "Already Tomorrow" Copyright (C) 2023 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 (64-bit) Thanks, Tim ________________________________ This email and any attachments are intended for the named recipient only. Its unauthorised use, distribution, disclosure, storage or copying is not permitted. If you have received it in error, please destroy all copies and notify the sender. In messages of a non-business nature, the views and opinions expressed are the author's own and do not necessarily reflect those of Cefas. Communications on Cefas? computer systems may be monitored and/or recorded to secure the effective operation of the system and for other lawful purposes. All messages sent and received by the Centre for Environment, Fisheries and Aquaculture Science may be monitored in line with relevant UK legislation . <https://www.gov.uk/government/organisations/centre-for-environment-fisheries-and-aquaculture-science/about/personal-information-charter>
I think you're just seeing undefined behaviour.  In most cases it 
doesn't make sense to interpolate linearly between the points (-Inf, y0) 
and (-1, y1).  In your example with y0 == y1 it's fine, but if you had
   approxfun(x=c(-Inf, 0), y = c(1, 2))
what would you expect the result to be when evaluated at x = -1?  It 
will depend on how you evaluate the linear interpolation formula:
  f(x) = y0 + (x - x0)*(y1 - y0)/(x1 - x0) = 1 + Inf*1/Inf
       = y1 - (x1 - x)*(y1 - y0)/(x1 - x0) = 2 - 1*1/Inf
       = [y0*(x1 - x) + y1*(x - x0)]/(x1 - x0) = [1*1 + 2*Inf]/Inf
The second formula gives 2 (which is a reasonable answer), but the first 
and third are undefined because of the Inf/Inf.
Duncan Murdoch
On 2025-07-04 6:50 a.m., Timothy Earl (Cefas) via R-help
wrote:> Hi,
> 
> I'm seeing an asymmetry in how approxfun treats -Inf and Inf, which I
don't understand from reading the help file.
> 
> e.g. a simple step function
> tmp <- approxfun(x=c(-Inf, -0.2, 0.2,  Inf), y=c(-1, -1, 1, 1))
> tmp(c(-1, 0, 1))
> # [1] NaN   0   1
> I expected: # [1] -1   0   1
> 
> Clearly I can work round this by using type=2, and not specifying the end
points (or by replacing -Inf by a large negative number), but I was wondering
what I was missing about the different behaviour between positive and negative
infinity.
> 
> To simplify further:
> approxfun(x=c(-1000,  Inf), y=c(1, 1))(0)
> # [1] 1
> 
> approxfun(x=c(-Inf,  1000), y=c(1, 1))(0)
> # [1] NaN
> 
> Sessioninfo:
> R version 4.3.0 (2023-04-21 ucrt) -- "Already Tomorrow"
> Copyright (C) 2023 The R Foundation for Statistical Computing
> Platform: x86_64-w64-mingw32/x64 (64-bit)
> 
> Thanks,
> 
> Tim
> ________________________________
>   This email and any attachments are intended for the named recipient only.
Its unauthorised use, distribution, disclosure, storage or copying is not
permitted. If you have received it in error, please destroy all copies and
notify the sender. In messages of a non-business nature, the views and opinions
expressed are the author's own and do not necessarily reflect those of
Cefas. Communications on Cefas? computer systems may be monitored and/or
recorded to secure the effective operation of the system and for other lawful
purposes. All messages sent and received by the Centre for Environment,
Fisheries and Aquaculture Science may be monitored in line with relevant UK
legislation .
<https://www.gov.uk/government/organisations/centre-for-environment-fisheries-and-aquaculture-science/about/personal-information-charter>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
https://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
Approxfun uses the two-point formula [1] for a line: y = y_1 + (y_2 - y_1) * (x - x_1) / (x_2 - x_1) so in the first simplified case you get y = 1 + (1 - 1) * (0 - (-1000)) / (Inf - (-1000)) = 1 + 0 * 1000 / Inf = 1 + 0 / Inf = 1 + 0 = 1 while in the second case you get y = 1 + (1 - 1) * (0 - (-Inf)) / (1000 - (-Inf)) = 1 + 0 * Inf / Inf = 1 + NA / Inf = 1 + NA = NA Zero times infinity is undefined. [1] <https://en.m.wikipedia.org/wiki/Linear_equation> On July 4, 2025 3:50:26 AM PDT, "Timothy Earl (Cefas) via R-help" <r-help at r-project.org> wrote:>Hi, > >I'm seeing an asymmetry in how approxfun treats -Inf and Inf, which I don't understand from reading the help file. > >e.g. a simple step function >tmp <- approxfun(x=c(-Inf, -0.2, 0.2, Inf), y=c(-1, -1, 1, 1)) >tmp(c(-1, 0, 1)) ># [1] NaN 0 1 >I expected: # [1] -1 0 1 > >Clearly I can work round this by using type=2, and not specifying the end points (or by replacing -Inf by a large negative number), but I was wondering what I was missing about the different behaviour between positive and negative infinity. > >To simplify further: >approxfun(x=c(-1000, Inf), y=c(1, 1))(0) ># [1] 1 > >approxfun(x=c(-Inf, 1000), y=c(1, 1))(0) ># [1] NaN > >Sessioninfo: >R version 4.3.0 (2023-04-21 ucrt) -- "Already Tomorrow" >Copyright (C) 2023 The R Foundation for Statistical Computing >Platform: x86_64-w64-mingw32/x64 (64-bit) > >Thanks, > >Tim >________________________________ > This email and any attachments are intended for the named recipient only. Its unauthorised use, distribution, disclosure, storage or copying is not permitted. If you have received it in error, please destroy all copies and notify the sender. In messages of a non-business nature, the views and opinions expressed are the author's own and do not necessarily reflect those of Cefas. Communications on Cefas? computer systems may be monitored and/or recorded to secure the effective operation of the system and for other lawful purposes. All messages sent and received by the Centre for Environment, Fisheries and Aquaculture Science may be monitored in line with relevant UK legislation . <https://www.gov.uk/government/organisations/centre-for-environment-fisheries-and-aquaculture-science/about/personal-information-charter> >______________________________________________ >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide https://www.R-project.org/posting-guide.html >and provide commented, minimal, self-contained, reproducible code.-- Sent from my phone. Please excuse my brevity.