1

I've following formula in Wolfram:

log base 10 of (10^-18x)

WolframLink (with x example)

First of all Log Function:

function getLog(y) {
    return Math.log(y)/Math.log(10);
}

Now I'm trying to get my value:

var x = Math.pow(10,33);
var faktor = getLog(Math.pow(10,-(18*x)));
console.log(faktor);

console gives -Infinity

What is wrong about my code? Thanks for help.

Standard
  • 1,450
  • 17
  • 35
  • So is there even a possibility to calculate this with Javascript? – Standard Jul 22 '14 at 19:28
  • Gah I deleted my comment! Original comment was [you are overflowing javascript's maximum int](http://stackoverflow.com/questions/307179/what-is-javascripts-max-int-whats-the-highest-integer-value-a-number-can-go-t) – TheNorthWes Jul 22 '14 at 19:30
  • Yes, I saw it. So is there a chance to get a workaround or do I need Ajax Requests? – Standard Jul 22 '14 at 19:31

3 Answers3

2

Your computations are causing floating point underflow. A floating point number is roughly represented as 0.<mantissa> × 2<exponent>, so that the distribution of log(|x|) for representable values x is uniformly dense. Both the mantissa and exponent have a certain number of bits allocated for them. The exponent of the representation can only become so small, and if you try to make it smaller than the minimum, the representation is forced to round to 0.

You can instead perform your computations in logspace using these identities. A few of these are listed here.

  • log(x × y) = log(x) + log(y)
  • log(x / y) = log(x) - log(y)
  • log(xy) = y × log(x)
Timothy Shields
  • 75,459
  • 18
  • 120
  • 173
  • So my formula would become `var faktor = (-18*x)*getLog(10);`, which means I don't need log even more? – Standard Jul 22 '14 at 20:43
  • @wernersbacher You are computing `var faktor = getLog(Math.pow(10,-(18*x)));`. That's log10(exp10(-18x)) = -18x. So you can simply compute `var faktor = -18 * x;` instead. – Timothy Shields Jul 22 '14 at 20:44
  • When x is 10^33, y should be 15 (Take a look on my wolfram link), but this is obviously another result. I think i made a huge mistake, its not 10^(-18*x), it's 10^(-18)*x what is completely different. i'm investigating. – Standard Jul 22 '14 at 20:48
  • well, now vanilla javascript can handle it. still thank you very much, your answer helped me while searching my mistake. – Standard Jul 22 '14 at 20:54
1

You are overflowing the maximum integer allowed in JavaScript by quite a bit.

Max int in JS

Wolfram proof because why not

You are going to need a big number extension of JavaScript, which SO has many posts about. See here for what appears to be unlimited integer math

More posts like this can be chased starting here

Community
  • 1
  • 1
TheNorthWes
  • 2,661
  • 19
  • 35
  • So I have x to set like this: `var x = new BigNumber(Math.pow(10,33));` and calculate like before? – Standard Jul 22 '14 at 19:41
  • You have to use the library and ***only*** that library. If you try to do x * 4 you will get infinity. JavaScript cannot handle that, but that BigNumber extension and set of functions can. – TheNorthWes Jul 22 '14 at 19:56
  • The problem is, that there is no logarithm function in bignumber.js and I can't find a library with it. :( – Standard Jul 22 '14 at 19:58
  • 1
    @AdmiralAdama The problem is underflow, not overflow. `-18*Math.pow(10,33)` computes just fine in Javascript, because it is using a floating point representation: `-1.8000000000000002e+34` – Timothy Shields Jul 22 '14 at 20:26
0

Code

Values in practice:

var x = Math.pow(10,33);           // x = 1.0000000000000001e+33
var exponent = -18*x;              // exponent = -1.8000000000000002e+34
var deci = Math.pow(10, exponent); // deci = 0
var logged = Math.log(deci);       // logged = NaN (-Infinity)
var result = logged/Math.log(10);  // result = NaN (-Infinity)

The problem is that floating point values can have decimal exponents in the range -308 to +308 (approximately, it's really all in binary), and your exponent is approximately 18,000,000,000,000,002,000,000,000,000,000,000.

Maths

Maths gives us log(xy) = y.log(x). And that logb(b) = 1. Thus,

   log10(10-18x) = -18x.log(10) = -18x.

So the maths lesson is that "logging X (base b)" and "raising b to the power of X" are inverse operations.

Phil H
  • 19,928
  • 7
  • 68
  • 105