3

I was trying to use log and then take the exp, but that makes only sense if I deal with products or divisions. One would usually define

Euclidean_Norm <- function(x) sqrt(sum(x^2))

But I can't handle overflow/underflow. I was thinking to implement

Euklidean_Norm2 <- function(x) log(exp(sqrt(sum(x^2))))

If I take for example

c(34212432, 21343210940, 5412359103) 

I get Inf with Euklidean_Norm2 and the Euklidean_Norm does not work. But it should be representable in R, since

sqrt(34212432^2 + 21343210940^2 + 5412359103^2)
[1] 22018797760

I am looking for a way to avoid this kind of overflows. I would be grateful for any hint.

user20650
  • 24,654
  • 5
  • 56
  • 91
JimmyJim
  • 101
  • 1
  • 6
  • 1
    Please provide a reproducible example of the issue occurring – Dason Oct 28 '18 at 14:29
  • I have a simple implementation `EuklideanDistance <- function(x) sqrt(sum(x^2)) ` and i want to figure out a way to avoid overflow/underflow. But i don't know how to approach this problem – JimmyJim Oct 28 '18 at 14:45
  • 1
    The overflow comes from the part `exp(...)` To avoid it with your function you can do: `m <- mean(x); m*Euklidean_Norm2(x/m)` (or you put this operation in the function definition) ... a variant: `m <- max(abs(x)); m*Euklidean_Norm2(x/m)` – jogo Oct 28 '18 at 16:14

0 Answers0