Author Topic: JAVACOMP: digits of pi  (Read 6916 times)

0 Members and 1 Guest are viewing this topic.

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
JAVACOMP: digits of pi
« on: August 22, 2007 »
My very first ever javascript program.  It's just a port, but it's let me know how it all works and what the limitations are ie. it's crap for any kind of computation!.

Jim
Challenge Trophies Won:

Offline benny!

  • Senior Member
  • DBF Aficionado
  • ********
  • Posts: 4384
  • Karma: 228
  • in this place forever!
    • View Profile
    • bennyschuetz.com - mycroBlog
Re: JAVACOMP: digits of pi
« Reply #1 on: August 22, 2007 »
Loads of digits Jim. Nice one - works fine here on Mozilla  :goodpost:
[ mycroBLOG - POUET :: whatever keeps us longing - for another breath of air - is getting rare ]

Challenge Trophies Won:

Offline combatking0

  • JavaScript lives!
  • Senior Member
  • DBF Aficionado
  • ********
  • Posts: 4569
  • Karma: 235
  • Retroman!
    • View Profile
    • Combat King's Barcode Battler Home
Re: JAVACOMP: digits of pi
« Reply #2 on: August 22, 2007 »
Very nice - accurate too.
You are our 9001st visitor.
Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17407
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: JAVACOMP: digits of pi
« Reply #3 on: August 22, 2007 »
My sinescrolls will look smoother than ever thanks to you! :)

Code: [Select]
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Paul

  • Pentium
  • *****
  • Posts: 1490
  • Karma: 47
    • View Profile
Re: JAVACOMP: digits of pi
« Reply #4 on: August 22, 2007 »
In Firefox I get, "A script on this page may be busy, or it may have stopped responding. You can stop the script now, or you can continue to see if the script will complete."

when I chose to let it continue the same message just pops up again after a while, pressing cancel shows what's presumably lots of digits of pi?


In IE the browser just crashes.
I will bite you - http://s5.bitefight.se/c.php?uid=31059
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: JAVACOMP: digits of pi
« Reply #5 on: August 23, 2007 »
I wrote it in IE7 and it has the same problem about it being busy.  I only let it do 900digits max, but that will take several minutes - it takes 5s in C.

I don't understand how the games framework p01 posted lets you run interactively without that problem.

Jim
Challenge Trophies Won:

Offline mike_g

  • Amiga 1200
  • ****
  • Posts: 435
  • Karma: 34
    • View Profile
Re: JAVACOMP: digits of pi
« Reply #6 on: August 23, 2007 »
Took some time for me too (firefox). I stopped the script after about 10 seconds, but still got about 100 digits of pie which is good enough for me :)

Offline p01

  • Atari ST
  • ***
  • Posts: 158
  • Karma: 51
    • View Profile
    • www.p01.org
Re: JAVACOMP: digits of pi
« Reply #7 on: August 23, 2007 »
Jim: Unlike in C/C++, JS's main function are not supposed to be run in an (more or less) infinite loop. The browsers has other things to do, like handling I/O, rendering, ...

Therefore JS's "main", must be run a regular intervals using either setTimeout( functionHandle, delayInMs ) which will jump to the functionHandle once, or setInterval( functionHandle, delayInMs ) which will (do its best to) jump functionHandle every delayInMs ms.

Notice that to avoid consuming all the CPU resources, browser vendors allocate some time slots to the JS engine. Therefore if it's very small the delayInMs will not matter. The browser will execute the code at the next time slot. In practice 20ms is safe.

But within this 20ms, you nothing forbids you to compute several things.

Offline combatking0

  • JavaScript lives!
  • Senior Member
  • DBF Aficionado
  • ********
  • Posts: 4569
  • Karma: 235
  • Retroman!
    • View Profile
    • Combat King's Barcode Battler Home
Re: JAVACOMP: digits of pi
« Reply #8 on: August 23, 2007 »
I get a similar message in MSIE 6, but it's not serious - the code does what it is intended to do.
You are our 9001st visitor.
Challenge Trophies Won:

Offline p01

  • Atari ST
  • ***
  • Posts: 158
  • Karma: 51
    • View Profile
    • www.p01.org
Re: JAVACOMP: digits of pi
« Reply #9 on: August 23, 2007 »
One more thing: document.write() is evil. It forces the DOM engine to parse what you inject. Prefer something like:

HTML
Code: [Select]
<p id="output">3.</p>JS
Code: [Select]
outputTextHandle=document.getElementById('output').firstChild;
// ...
outputTextHandle.nodeValue += foo;

Changing only the nodeValue of a textNode is the method of choice as the browser knows you only touch a text node. No need to parse and append a chunk of markup.

HTH

Side note: combatking0, I hope the winners banner will read JavaScript comp, not Java comp ;)

Offline combatking0

  • JavaScript lives!
  • Senior Member
  • DBF Aficionado
  • ********
  • Posts: 4569
  • Karma: 235
  • Retroman!
    • View Profile
    • Combat King's Barcode Battler Home
Re: JAVACOMP: digits of pi
« Reply #10 on: August 23, 2007 »
Fortunately, Va!n has produced a wide selection of banners - some of which have the word "JavaScript" on them.

Thanks again, Va!n.
You are our 9001st visitor.
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: JAVACOMP: digits of pi
« Reply #11 on: August 23, 2007 »
Thanks p01, K++, I think I get it...I know I'm being unfriendly when I want to take several minutes to render a page of html :D
I wrote a program that just did
Code: [Select]
var counter
main=function()
{document.write(counter++);
}
setInterval=(...whatever it was out of your example to call main..., 50)
Now, that I would expect would write out
0
1
2
3
but it doesn't.  So I got something wrong and I probably need to post more code.
Does that setInterval cause a page refresh?  If not, how does the document being rendered to get affected?
More to the point, I've read a load of docs that explain the syntax of js, and I've got that no problem - are there any good docs or sites that can tell me these other bits?
The great thing about this pi algorithm is it lends itself to this because it generates 9 digits at a time, and so it doesn't absoutely need to hog the cpu.

Jim
Challenge Trophies Won:

Offline p01

  • Atari ST
  • ***
  • Posts: 158
  • Karma: 51
    • View Profile
    • www.p01.org
Re: JAVACOMP: digits of pi
« Reply #12 on: August 23, 2007 »
Jim: Like in C ( I guess, been a while since I last coded in C/C++ ), you must initialize counter before being able to increment it.

Neither setTimeout nor setInterval cause a refresh. That would be incredibly stupid, don't you think ;)


As I said document.write() is bad bad bad. Prefer the DOM way as I explained above and in the Updating text on a page thread.

Here is a quick fix of your code snippet:
Code: [Select]
// boo ugly, no DOCTYPE !!!
<body>bla
<script>
var counter=0
main=function()
{
document.body.firstChild.nodeValue += counter++;
}
setInterval(main, 50)
</script>
</body>
Regarding documentations, I'd say : go straight to the source a.k.a the W3C and ECMA, and check Enhancing Web Pages with JavaScript and the related links at bottom. And of course read some source codes ( FWIW mine might be a bit criptic but they can certainly help in a few areas )

HTH

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: JAVACOMP: digits of pi
« Reply #13 on: August 23, 2007 »
In C, counter would be global and get initialised to 0 anyway, guaranteed by standard.

DOM is kind of odd - the whole point of javascript was to be really simple, then folks started hammering it and DOM had to be born.

When I was a kid, writing my first html page, doctype hadn't been invented!  Or style sheets for that matter.  I have a web page on geocities I wrote in 1995 using notepad.

I guess I'm looking for an overview and your links are very useful - thanks, and I see I have some reading to do :D  My real problem is I've done nothing with web stuff since it was invented and so the problems, the solutions and the motivation behind them has passed me by.

Jim
Challenge Trophies Won:

Offline combatking0

  • JavaScript lives!
  • Senior Member
  • DBF Aficionado
  • ********
  • Posts: 4569
  • Karma: 235
  • Retroman!
    • View Profile
    • Combat King's Barcode Battler Home
Re: JAVACOMP: digits of pi
« Reply #14 on: August 24, 2007 »
When I was a kid, writing my first html page, doctype hadn't been invented!  Or style sheets for that matter.  I have a web page on geocities I wrote in 1995 using notepad.

I'm in the same boat!
You are our 9001st visitor.
Challenge Trophies Won: