473,837 Members | 1,487 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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.getEle mentById("input box").value = binNumber.toStr ing(2);
----

It shows "1(e+61)" instead of
"10000000000000 000000000000000 000000000000000 000000000000000 000".
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
Jul 23 '05 #1
9 6557
"J.Sperlhof er" <so***********@ gmx.net> 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.getEle mentById("input box").value = binNumber.toStr ing(2);
----

It shows "1(e+61)" instead of
"10000000000000 000000000000000 000000000000000 000000000000000 000".
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 deexponentializ e(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 - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #2
Hello again!
Thanks a lot, Lasse for looking into my problem. :-)
"J.Sperlhof er" <so***********@ gmx.net> 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 deexponentializ e(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. :-)

ad:
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.
Jul 23 '05 #3
"J.Sperlhof er" <so***********@ gmx.net> 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).
ad:
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 - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #4
JRS: In article <JP************ ******@news.che llo.at>, dated Fri, 22
Apr 2005 18:58:49, seen in news:comp.lang. javascript, J.Sperlhofer
<so***********@ gmx.net> 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 }

-> 100000000000000 000000000000000 000000000000000 000000000000000 00

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.

--
© John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #5


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
a good tutorial to start with? I know object-orientation in other
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
Jul 23 '05 #6
JRS: In article <vV************ *******@news.ch ello.at>, dated Sun, 24
Apr 2005 07:29:31, seen in news:comp.lang. javascript, J.Sperlhofer
<so***********@ gmx.net> 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
a good tutorial to start with? I know object-orientation in other
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?

--
© John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demo n.co.uk/> - FAQqish topics, acronyms & links.
PAS EXE TXT ZIP via <URL:http://www.merlyn.demo n.co.uk/programs/00index.htm>.
Do not Mail News to me. Before a reply, quote with ">" or "> " (SoRFC1036)
Jul 23 '05 #7
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 <vV************ *******@news.ch ello.at>, dated Sun, 24
Apr 2005 07:29:31, seen in news:comp.lang. javascript, J.Sperlhofer
<so***********@ gmx.net> 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
a good tutorial to start with? I know object-orientation in other
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?

Jul 23 '05 #8
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.
Jul 23 '05 #9
JRS: In article <Kg************ ******@news.che llo.at>, dated Thu, 28
Apr 2005 10:53:30, seen in news:comp.lang. javascript, J.Sperlhofer
<so***********@ gmx.net> 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".

--
© John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demo n.co.uk/> - FAQqish topics, acronyms & links;
Astro stuff via astron-1.htm, gravity0.htm ; quotings.htm, pascal.htm, etc.
No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.
Jul 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
10562
by: Timothy Fitz | last post by:
Why are all numberical literals in exponential notation floats? To me it is counter-intuitive for 1e3 to behave so fundamentally different from 1000. Timothy Fitz
1
6366
by: Mahesha | last post by:
Exponential Moving avg is calculated using the formula. EMA = (Today's Price)* K + (EMA yesterday) * (1-K) where K = 2 / (N+1) The user is going to Input the K. It is something like F(N) = Price * K + F(N-1) * (1-K) How can I reference, the previously calculated value in the Next row calculation. I need to implement this in SQL Server.
8
1449
by: Carramba | last post by:
hi! int digit = 12; printf("%x\n", digit); running this code I get 'c' as output? that is the problem? -- ______________________________________ I se the lightat the end, but every time I take a step it's get dim.
21
3234
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each column. Once the array elements are split, what is the best way to sort them? Thank you. //populate data object with data from xml file. //Data is a comma delimited list of values var jsData = new Array(); jsData = {lib: "#field...
9
6675
by: Joe Attardi | last post by:
Hi all, Math is not my strongest area so forgive me if I use some of the wrong terminology. It seems that scientific notation is immune to rounding errors. For example: (4.98 * 100) + 5.51 // returns 503.51000000000005, rounding error! 4.98e2 + 5.51 // returns 503.51, correct!
4
42767
by: b4ukiran | last post by:
Hi all, I have a requirement to print large double values without the exponential notaion. The double value can be a declared variable or any calculated value within the code. In any case, I should be able to print the result without containing the exponential notation. Please help me out.
3
7292
ashsa
by: ashsa | last post by:
Hi everyone, I am trying to display a numeric value fetched from an sql server table which is stored in the exponential notation. For eg, 0.08 is getting stored in the table as 8.0000000000000002E-2. This causes the xslt function format-number() to fail. I use it as <xsl:value-of select='format-number(8.0000000000000002E-2, "###,###.00")' /> The message says that This name may not contain the '#' character: -->#<--##,###.00...
30
5722
by: Barry L. Bond | last post by:
Greetings! I just got a new Peet Brothers Ultimeter 2100 Weather Station. This new one has a way to display the heat index, if you press the "dew point" key twice. Being aware of all the controversy surrounding the exact calculation of the heat index, I would like my own software (which I programmed in the 1990's, when I got their Ultimeter 2000 weather station, but that one didn't show heat index) to display it. Living in Florida,...
0
9837
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9682
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10621
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10267
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9392
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7804
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5845
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4474
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3123
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.