# Question: Exponential Notation and binary numbers (>e+61)

Discussion in 'Javascript' started by J.Sperlhofer, Apr 22, 2005.

1. ### J.SperlhoferGuest

Good morning, Javascript-Professionals.

I'm looking for an possibility to show a (calculated) 64bit-Number
without exponential notation. I don't want to see exponational notation
within my binary numbers.

To demonstrate my problem, try this code:

----
var binNumber = Math.pow(2,61);
document.getElementById("inputbox").value = binNumber.toString(2);
----

"10000000000000000000000000000000000000000000000000000000000000".
My codesnippet works great for powers of 2 below 61, but it starts with
the exponantial notation at powers of 61.

I looked around the web but i couldn't find a solution, just the
information that "javascript returns exponential notation in all
browsers outside the boundaries of 1e-5 and 1e+15)" (taken from the
JavaScript Core Language Reference, Part IV) - which is actually wrong
in the scope of binary digits.

Is there a simple (but fast) solution for my problem, or a switch to
turn off the exponential Notation?

Any help would be greatly appreciated.

Sincerely, J.Sperlhofer

J.Sperlhofer, Apr 22, 2005

2. ### Lasse Reichstein NielsenGuest

"J.Sperlhofer" <> writes:

> Good morning, Javascript-Professionals.
>
> I'm looking for an possibility to show a (calculated) 64bit-Number
> without exponential notation.

What kind of 64 bit number? Is it a 64 bit floating point number or
a 64 bit integer. Probably the former, since that is the type that
Javascript uses for numbers (and I assume you are aware that it
has at most a 53 bit precission).

> I don't want to see exponational notation within my binary numbers.

> To demonstrate my problem, try this code:
>
> ----
> var binNumber = Math.pow(2,61);
> document.getElementById("inputbox").value = binNumber.toString(2);
> ----
>
> It shows "1(e+61)" instead of
> "10000000000000000000000000000000000000000000000000000000000000".

In my browser (Opera 8), it doesn't use exponential notation.

> Is there a simple (but fast) solution for my problem, or a switch to
> turn off the exponential Notation?

Don't know any switch, but if your format is correct, a simple solution
would be:
----
function deexponentialize(number) {
var string = number.toString(2);
string = string.replace(/\(e\+(\d+)\)/, function(m,n,i) {
var zeros = [];
while(n--) {
zeros.push('0');
}
return zeros.join("");
});
return string;
}
----
(requires modern browser)

/L
--
Lasse Reichstein Nielsen -
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'

Lasse Reichstein Nielsen, Apr 23, 2005

3. ### J.SperlhoferGuest

Hello again!
Thanks a lot, Lasse for looking into my problem.

> "J.Sperlhofer" <> writes:
>>I'm looking for an possibility to show a (calculated) 64bit-Number
>>without exponential notation.

>
> What kind of 64 bit number? Is it a 64 bit floating point number or
> a 64 bit integer. Probably the former, since that is the type that
> Javascript uses for numbers (and I assume you are aware that it
> has at most a 53 bit precission).

Nothing of both, but more a 64bit integer: It should be a 64digit/bit
binary number - i wasn't talking of the intern datatype.

But read on at the bottom of the posting.

> In my browser (Opera 8), it doesn't use exponential notation.
>

Thats an excellent information i can work with.
(... and so i installed the latest build of opera 8.00 to test it myself.)

>>Is there a simple (but fast) solution for my problem, or a switch to
>>turn off the exponential Notation?

>
>
> Don't know any switch, but if your format is correct, a simple solution
> would be:
> ----
> function deexponentialize(number) {
> var string = number.toString(2);
> string = string.replace(/\(e\+(\d+)\)/, function(m,n,i) {
> var zeros = [];
> while(n--) {
> zeros.push('0');
> }
> return zeros.join("");
> });
> return string;
> }
> ----
> (requires modern browser)

So ... the conclusion:

This snippet works perfectly for powers of 2 in binary notation, but it
becomes useless when precicion is lost due to the exponentional notation
and the 53bit precission of IEEE (52/53 bit mantissa, 11 bit exponent).
But nevertheless: it helps.

Even Opera will lost the precission if you add a small number to the
large number, like this excample shows:

var binNumber = Math.pow(2, intPower) + 500;

The output is correct as long a intPower is below 54, afterwards it will
starts to "forget" the added 500. I know why this happens, and i dont
think there is a possibilty to work with datatypes of a higher
precission. Or is there a possibility like a 128bit-integer?

But nevertheless:
Thanks a lot for the great workaround, which will help me a lot i guess.

Sincerly,
J.Sperlhofer.

J.Sperlhofer, Apr 23, 2005
4. ### Lasse Reichstein NielsenGuest

"J.Sperlhofer" <> writes:

> This snippet works perfectly for powers of 2 in binary notation, but
> it becomes useless when precicion is lost due to the exponentional
> notation and the 53bit precission of IEEE (52/53 bit mantissa, 11 bit
> exponent). But nevertheless: it helps.

Yes, the 53 bit limit is inherent in the ECMAScript number type (a
IEEE-754 64-bit floating point number).

> Even Opera will lost the precission if you add a small number to the
> large number, like this excample shows:
>
> var binNumber = Math.pow(2, intPower) + 500;

The first loss of precission happens at the expected 53 bit limit:

Math.pow(2,53)+1 == Math.pow(2,53) // true

> The output is correct as long a intPower is below 54, afterwards it
> will starts to "forget" the added 500. I know why this happens, and i
> dont think there is a possibilty to work with datatypes of a higher
> precission. Or is there a possibility like a 128bit-integer?

Not in ECMAScript. Unless some implementation adds its own types with
larger precission, then no.

It seems that Javascript was not intended for large scientific
computations, having only one number type. You seem to need the
equivalent of a BigInteger from Java.

/L
--
Lasse Reichstein Nielsen -
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'

Lasse Reichstein Nielsen, Apr 23, 2005
5. ### Dr John StocktonGuest

Question: Exponential Notation and binary numbers (>e+61)

JRS: In article <JPbae.15487\$>, dated Fri, 22
Apr 2005 18:58:49, seen in news:comp.lang.javascript, J.Sperlhofer
<> posted :
>Good morning, Javascript-Professionals.

It is now tomorrow afternoon, and I am not a professional.

>I'm looking for an possibility to show a (calculated) 64bit-Number
>without exponential notation. I don't want to see exponational notation
>within my binary numbers.

N = Math.pow(2,61)
S = ""
while (N>0) { D = N%2 ; S = D + S ; N = (N-D)/2 }

-> 10000000000000000000000000000000000000000000000000000000000000

Be aware, however, that Javascript (currently?) uses only IEEE Doubles
for numbers, with 53-bit resolution. If you need 64-bit work, you will
need to represent numbers as arrays or objects of parts, and do your own
arithmetic.

See my Web site, via sig below.

--
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.

Dr John Stockton, Apr 23, 2005
6. ### J.SperlhoferGuest

Re: Question: Exponential Notation and binary numbers (>e+61)

Dr John Stockton wrote:
> It is now tomorrow afternoon, and I am not a professional.

Thanks a lot for reading my posting too, John Stockton.

> Be aware, however, that Javascript (currently?) uses only IEEE Doubles
> for numbers, with 53-bit resolution.

This seems to be my biggest problem right now, cause to get an accurate
calculation, i need at least a 59-bit resolution.

> If you need 64-bit work, you will
> need to represent numbers as arrays or objects of parts, and do your own
> arithmetic.

That is what I thought of already, but i never did a own object in
Javascript, so i searched the web for some more information. Do you know
languages, but i cant seem to find a usefull tuturial dealing with that
theme.

>
> See my Web site, via sig below.
>

I read parts of it while i was drinking my morning-coffee, and I
bookmarked it right afterwards ... really usefull information

Sincerly,
J.Sperlhofer

J.Sperlhofer, Apr 24, 2005
7. ### Dr John StocktonGuest

Re: Question: Exponential Notation and binary numbers (>e+61)

JRS: In article <vVHae.17470\$>, dated Sun, 24
Apr 2005 07:29:31, seen in news:comp.lang.javascript, J.Sperlhofer
<> posted :

>That is what I thought of already, but i never did a own object in
>Javascript, so i searched the web for some more information. Do you know
>languages, but i cant seem to find a usefull tuturial dealing with that
>theme.

Well, arrays are probably simpler for the purpose, anyway. If working
with integers will do, all you need do is translate my program
longcalc.pas : it's under 3000 lines, not all of which will be really
needed.

But a requirement for 59-bit resolution seems strange; possibly the task
can be rearranged so as not to need it? What sort of mathematical
operations might be needed?

--
Web <URL:http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms & links.
PAS EXE TXT ZIP via <URL:http://www.merlyn.demon.co.uk/programs/00index.htm>.
Do not Mail News to me. Before a reply, quote with ">" or "> " (SoRFC1036)

Dr John Stockton, Apr 24, 2005
8. ### J.SperlhoferGuest

Re: Question: Exponential Notation and binary numbers (>e+61)

I'll take a look into your programm, thanks a lot for your offer, John
Stockton.

The program should be able to adress every second between the Big Bang
and now (60 x 60 x 24 x 365 x ~14000000000)... since it will not be
scientific, there is no need for heavy calculations.

Sincerly,
J.Sperlhofer

Dr John Stockton wrote:
> JRS: In article <vVHae.17470\$>, dated Sun, 24
> Apr 2005 07:29:31, seen in news:comp.lang.javascript, J.Sperlhofer
> <> posted :
>
>
>>That is what I thought of already, but i never did a own object in
>>Javascript, so i searched the web for some more information. Do you know
>>languages, but i cant seem to find a usefull tuturial dealing with that
>>theme.

>
>
> Well, arrays are probably simpler for the purpose, anyway. If working
> with integers will do, all you need do is translate my program
> longcalc.pas : it's under 3000 lines, not all of which will be really
> needed.
>
> But a requirement for 59-bit resolution seems strange; possibly the task
> can be rearranged so as not to need it? What sort of mathematical
> operations might be needed?
>

J.Sperlhofer, Apr 28, 2005
9. ### wolfgang zeidlerGuest

Re: Question: Exponential Notation and binary numbers (>e+61)

J.Sperlhofer schrieb:
> I'll take a look into your programm, thanks a lot for your offer, John
> Stockton.
>
> The program should be able to adress every second between the Big Bang
> and now (60 x 60 x 24 x 365 x ~14000000000)... since it will not be
> scientific, there is no need for heavy calculations.
>

If you only need addition and multiplication
you'll find a ( slow ) javascript solution at
http://home.arcor.de/wzwz.de/wiki/ebs/

regards, w.z.

wolfgang zeidler, Apr 28, 2005
10. ### Dr John StocktonGuest

Re: Question: Exponential Notation and binary numbers (>e+61)

JRS: In article <Kg3ce.24608\$>, dated Thu, 28
Apr 2005 10:53:30, seen in news:comp.lang.javascript, J.Sperlhofer
<> posted :
>Dr John Stockton wrote:

>> But a requirement for 59-bit resolution seems strange; possibly the task
>> can be rearranged so as not to need it? What sort of mathematical
>> operations might be needed?

Responses should go after trimmed quotes; corrected; see FAQ.

>The program should be able to adress every second between the Big Bang
>and now (60 x 60 x 24 x 365 x ~14000000000)... since it will not be
>scientific, there is no need for heavy calculations.

That's about 4.34e17, indeed about 5 bits too big for a Double.

You could store values to the second as an Object holding Days and
Seconds (if you can take all days as being the same length.

Or you could, for simple calculations, store as an array of digits
representing seconds and use "school arithmetic".

--