[R] approxfun asymmetry

Jeff Newmiller jdnewm|| @end|ng |rom dcn@d@v|@@c@@u@
Fri Jul 4 23:22:12 CEST 2025


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 using 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 using 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.



More information about the R-help mailing list