Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Highest and lowest possible result of this javascript expression?

What is the highest number this javascript expression can evaluate to? What is the lowest number? Why?

+(''+Math.random()).substring(2)

Extra credit: How many different values can the expression evaluate to? Can it be every value from the minimum to the maximum, or are some intermediate values not obtainable due to rounding issues?


Response to Daniel's answer (deleted, was 10000000000000000 max, 0 min):

I was playing around in Chrome's console and got this:

    Math.random();

>> 0.00012365682050585747

    '12365682050585747'.length

>> 17

    12365682050585747 > 10000000000000000

>> true

... so 10000000000000000 can't be the max!

like image 531
Dagg Nabbit Avatar asked Sep 22 '10 01:09

Dagg Nabbit


People also ask

How do you find the highest and lowest numbers in JavaScript?

To get the highest or lowest number from an array in JavaScript, you can use the Math. max() or the Math. min() methods then spread the elements from the array to these methods using the spread operator ( ... ).

How to find maximum number in JavaScript?

The Math.max() function returns the largest of the numbers given as input parameters, or - Infinity if there are no parameters.

What are expressions in JavaScript?

JavaScript's expression is a valid set of literals, variables, operators, and expressions that evaluate to a single value that is an expression. This single value can be a number, a string, or a logical value as depending on expression.

How do you add in JavaScript?

Basic JavaScript AdditionAdd numbers in JavaScript by placing a plus sign between them. You can also use the following syntax to perform addition: var x+=y; The "+=" operator tells JavaScript to add the variable on the right side of the operator to the variable on the left.


1 Answers

It depends on how the random number is generated, and how the number will be converted to string. The ECMAScript spec doesn't specify both of these.

In practice, the number will have at most 17 significant figures, so the maximum should be at most 1017.

The spec does specify that a number will be displayed in decimal form (instead of scientific form) when the exponent is between -6 and 20 (10-6 ≤ x < 1021), so we just need to restrict our attention on numbers in [10-6, 1) when trying to seek the maximum exhaustively.

However, in this range a number must be representable as s × 2e, where 1 ≤ s ≤ 2 − 2-52 with a precision of Δs = 2-52 and -20 ≤ e ≤ -1. The spec recommends that ToNumber(ToString(x)) == x, so the number should be precise down to 2-52+e for a given e. Thus the "17-digit" number with (2 − n × 2-52) × 2e with the smallest n will be the biggest number representable with a given e, after chopping the initial 0..

             v                 
(-20) 0.0000019073486328124998
(-19) 0.0000038146972656249996
(-18) 0.0000076293945312499975 (n=3)
(-17)  0.000015258789062499998
(-16)  0.000030517578124999997
(-15)  0.000061035156249999986 (n=2)
(-14)   0.00012207031249999999
(-13)   0.00024414062499999997
(-12)   0.00048828124999999995
(-11)    0.0009765624999999999 (always 16-digit?)
(-10)    0.0019531249999999998
(-9)     0.0039062499999999996
(-8)     0.0078124999999999965 (n=4)
(-7)      0.015624999999999998
(-6)      0.031249999999999997
(-5)      0.062499999999999986 (n=2)
(-4)       0.12499999999999999
(-3)       0.24999999999999997
(-2)       0.49999999999999994
(-1)        0.9999999999999999 (always 16-digit?)

From here we know that the absolute maximum is 78,124,999,999,999,965.


Math.random() can return any nonnegative numbers in the interval [0, 1), so the safe minimum is -324 from 5e-324 (the smallest subnormal number in double precision is 4.94 × 10-324).

like image 185
kennytm Avatar answered Oct 28 '22 16:10

kennytm