### Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

## The Log-Scale Trick

A common trick for avoiding overflow and underflow is to work in a logarithmic scale, rather than the original scale:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

# The R code below graphs log(x) as a function of x

curve(log, from = 0, to = 1000, xlab = 'x',

ylab = 'log(x)', main = 'The logarithm function')

print(3 ^ -800, digits = 22) # underflow

print(log(3 ^ -800), digits = 22) # log of underflow

print(-800 * log(3), digits = 22) # avoiding underflow using the log-scale

# Addition, when using the log-scale, replaces multiplication

print(3 ^ -600 * 3 ^ -100 * 3 ^ 150, digits = 22) # underflow

# Avoid underflow and return results in the log-scale

print(log(3) * (-600 - 100 + 150), digits = 22)

# Avoid underflow and return results in original scale

print(exp(log(3) * (-600 - 100 + 150)), digits = 22)

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Suggested playgrounds

Open Source Your Knowledge: become a Contributor and help others learn. Create New Content