How to refresh cached image ONCE

  • Thread starter C. (http://symcbean.blogspot.com/)
  • Start date
J

Jorge

The function you observed, Resol, is now Resol1; new Resol3 is similar
but uses only randoms >= 0.5.

John,

I have retouched your resol1() a little bit. Now it receives a
parameter p, and it doesn't return until the higher resolution found
remains unchanged for p iterations of the loop. It gives higher
readings now.

I have also written a bits() function that finds what is the smaller
power of 2 for which it's true that (2^x) === ((2^x)+1). I think that
that must be the size of the mantissa, or not ?

See it here : http://tinyurl.com/68d9br

These are the numbers I get :

IE 5.2.3 : 53, 53.
FF2, FF3 : 52, 53.
Safari : 30, 53.
Opera 9.51 : 31, 53.
iCab 3.0.5 : 63, 53.

But I don't understand why those numbers are different. And, if the
mantissa is 53 bits (iCab), how can, why does resol1() give 63 ?

Here's the code :

<script>
window.onload= function () {
var resol1= (function (p) {
var x, t, max= 0, i= p;
while (i) {
t= 0, x= Math.random();
while ((x*= 2)%1 > 0) { // shift left until empty
t++;
}
if (t > max) {
max= t, i= p;
} else {
i--;
}
}
return max;
})(1e4);

var bits= (function () {
var x, i= 0;
do {
x= Math.pow(2,++i);
} while (x !== (x+1))
return i;
})();

var test= Math.pow(2,bits);
var text= "resol1() : Maximum resolution is at least "+resol1+"
bits.<br>";
text+= "bits() : "+bits+" -> In this browser "+(test+1)+" ===
1+"+test;

(document.body.appendChild(
document.createElement(
'h2'))).innerHTML= text;

};
</script>

Thanks,
--Jorge.
 
C

C. (http://symcbean.blogspot.com/)

See here :http://tinyurl.com/6lwmmb

The first & second buttons do what Thomas is proposing.
The 3rd button does a truly "unconditional reload" of the *same* image
file.

Though, you don't seem to want an unconditional reload, because you
don't want the image to be reloaded unless the image content has
changed.
The cache control headers have to be setup properly in order to
achieve this.

But still, in order to trigger the reload, it's unclear to me how is
it going to be discovered client-side, that the image content has
changed at the server-side ?

--Jorge.


Thanks Jorge - unfortunately the first 2 still use the cached copy (in
Firefox) while the last undermines the caching policy I'm trying to
implement.

I struggled for a while with this - as per my original post - I could
get it to reload by accesing the image URL directly from the browser
then hitting the reload button - I just need to implement that in
Javascript - eventually I came up with one solution:

I can get to go back to the server even though the image is cached if
I put in an iframe with src= the image URL then
document.getElementById("MyIframe").contentWindow.location.reload();

(only tested with Firefox & seems like rather hard work though)

(BTW - regarding the big discussion about random numbers - if it's
that important use a timestamp instead)

C.
 
J

Jorge

I can get to go back to the server even though the image is cached if
I put it in an iframe with src= the image URL then
document.getElementById("MyIframe").contentWindow.location.reload();

If just a plain location.reload() won't do, if setting img.src+= "?
something" won't do either, then that sounds like a Good Idea...

--Jorge.
 
D

Dr J R Stockton

In comp.lang.javascript message <0d5ff528-59a3-4bb7-8f48-5df465dce82c@i7
6g2000hsf.googlegroups.com>, Mon, 21 Jul 2008 06:27:24, Jorge
I have also written a bits() function that finds what is the smaller
power of 2 for which it's true that (2^x) === ((2^x)+1). I think that
that must be the size of the mantissa, or not ?

We know that 2^53 is 9007199254740992. A Number, being an IEEE Double
and having a 53-bit mantissa, cannot represent 9007199254740993. But
it's not yet absolutely clear to me whether 2^53+1 MUST evaluate to 2^53
and not to 2^53+2.

It is certain that JavaScript uses IEEE Doubles, and that those consist
of one sign bit, eleven biased-exponent bits, a non-stored "1" bit
leading the mantissa, and fifty-two more mantissa bits. 2^53+1 is the
lowest integer requiring MSB...LSB to be at least 54 bits; the lowest
which cannot be stored exactly.


These are the numbers I get :

IE 5.2.3 : 53, 53.
FF2, FF3 : 52, 53.
Safari : 30, 53.
Opera 9.51 : 31, 53.
iCab 3.0.5 : 63, 53.

But I don't understand why those numbers are different. And, if the
mantissa is 53 bits (iCab), how can, why does resol1() give 63 ?

I'm not entirely sure, to +/- 1 in the result, whether the algorithms
are ideal. But there are clearly three cases : about 31, about 52, and
about 63.

I suppose that "about 31" are those in which 32-bit processing is
involved - probably a 32-bit generator (which will repeat after 2*32 (or
2^32-1) values). Clearly, given the integer operations such as X|Y and
the use of Number for storage, the JavaScript engine must have
interconverters.

The others probably, but not necessarily, use a 64-bit integer pseudo-
random binary sequence generator of the form
U[n] = (U[n-1]*vast + 1) mod 2^64.

Note that the FPU of a PC has the "comp" type, a 64-bit integer, and the
"Extended" type, with a genuine 64-bit mantissa; and a PC can do 64-bit
integer arithmetic either with comp or with a pair of 32-bit integers.
So the system programmer can calculate U[n], put it into the mantissa of
an Extended with suitable exponent, and ask for conversion to the Double
equivalent. In the conversion, U[n] will lose all its leading zeroes,
and then as many trailing bits as cannot fit into the 53 places
available. The value of the least preserved bit will depend on the
value of the Random generated.

However, that means that the resolution of Math.random depends on the
value generated - which seems undesirable.

Those who think that Math.random should give only multiples of 2^-53
(and will in time give all such values such that 0<=X<1) can instead
mask U[n] to 53 bits before conversion, or round the converted result to
a multiple of 2^-53.
 
T

Thomas 'PointedEars' Lahn

Jorge said:

I explained to you before why those alias URLs are unsuitable and
unnecessary in Usenet, and asked you to stop posting them.

I am asking you again to post original URL(s) here instead. As a Google
Groups user who heavily relies on the information in that archive, surely
you can see the advantages in complying with that request.
[...]
But still, in order to trigger the reload, it's unclear to me how is
it going to be discovered client-side, that the image content has
changed at the server-side ?

Then you should read <http://mnot.net/cache-docs> (again). It explains in
great detail which headers are used by clients to determine that.

Therefore, I see two distinct possibilities and two variations:

1. A client that makes only GET requests.

A) If there is a cached resource and headers indicate that the
server-side resource has not changed, it disconnects without
receiving the message body and retrieves the resource from
the cache instead.

B) The entire response is evaluated regardless of headers first.
Then headers are evaluated to determine which resource can be
considered newer. Then proceed as in (A).

2. A client that makes a HEAD request first. If headers indicate that
the cached resource (if there is one) and the server-side resource
differ, it proceeds with a GET request; else it retrieves the
resource from the cache. (Further tests may happen afterwards.)


HTH

PointedEars
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]>, Tue,
22 Jul 2008 19:08:42 said:
I explained to you before why those alias URLs are unsuitable and
unnecessary in Usenet, and asked you to stop posting them.

I am asking you again to post original URL(s) here instead. As a Google
Groups user who heavily relies on the information in that archive, surely
you can see the advantages in complying with that request.


Why should one be concerned with your alleged convenience in the matter
of compressed URLs when you persistently ignore the convenience of
others by not only providing but insisting upon the briefest of
attributions?
 
J

Jorge

Then you should read <http://mnot.net/cache-docs> (again).  It explainsin
great detail which headers are used by clients to determine that.

I worded it badly. Does the server's image change because the user/
client posts some new data ?
If so, the client knows beforehand when to reload the image.
If not, either poll or reload blindly (relying on correct cache
headers) or the image would need to be pushed somehow, if at all
possible.

--Jorge.
 
J

Jorge

I explained to you before why those alias URLs are unsuitable and
unnecessary in Usenet, and asked you to stop posting them.

Yes Thomas, you did. But I still don't agree with you. Sorry about
that.

Regards,
--Jorge.
 
J

Jorge

I'm not entirely sure, to +/- 1 in the result, whether the algorithms
are ideal.  But there are clearly three cases : about 31, about 52, and
about 63.

What do you think about this :

do {
x*= 2, x-= Math.floor(x), t++;
} while ( x> 0) // shift left until empty

(?)

--Jorge.
 
D

Dr J R Stockton

In comp.lang.javascript message <72ddfea4-511a-4a91-9e37-0a8be4328cd3@k3
6g2000pri.googlegroups.com>, Thu, 24 Jul 2008 16:56:10, Jorge
What do you think about this :

do {
x*= 2, x-= Math.floor(x), t++;
} while ( x> 0) // shift left until empty

It gives an answer one larger than the corresponding part of my code.
That may be better.
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]
rlyn.invalid>, Fri, 25 Jul 2008 23:30:59, Dr J R Stockton
In comp.lang.javascript message <72ddfea4-511a-4a91-9e37-0a8be4328cd3@k3
6g2000pri.googlegroups.com>, Thu, 24 Jul 2008 16:56:10, Jorge


It gives an answer one larger than the corresponding part of my code.
That may be better.

Or for (x = Math.random(), t = 0 ; x%1 > 0 ; t++) x *= 2


I have changed <URL:http://www.merlyn.demon.co.uk/js-randm.htm#MR>
subsection "For Your Browser".

I see, in WinXP, Safari & Opera showing as expected for a 32-bit PRBS
converted to Double. Firefox has a PRBS of at least 53 bits, with my
results comparable with Math.random() returning only multiples of 2^-53.
But MSIE apparently can return an odd multiple of 2^-54, which seems
unsound.

ISO/IEC 16262 15.8.2.14 is somewhat liberal in its definition of what
Math.random() should give; but, as in its range the absolute resolution
of Number is lowest (for values of 0.5 and higher) at 2^-53, the
greatest uniformity is obtained by allowing return values of N * 2^-53
for 0 <= N < 2^53. Anything else is less than ideal.

Comments?
 
J

Jorge

In comp.lang.javascript message <[email protected]
rlyn.invalid>, Fri, 25 Jul 2008 23:30:59, Dr J R Stockton
<[email protected]> posted:







Or    for (x = Math.random(), t = 0 ; x%1 > 0 ; t++) x *= 2

I have changed <URL:http://www.merlyn.demon.co.uk/js-randm.htm#MR>
subsection "For Your Browser".

I see, in WinXP, Safari & Opera showing as expected for a 32-bit PRBS
converted to Double.  Firefox has a PRBS of at least 53 bits, with my
results comparable with Math.random() returning only multiples of 2^-53.
But MSIE apparently can return an odd multiple of 2^-54, which seems
unsound.

ISO/IEC 16262 15.8.2.14 is somewhat liberal in its definition of what
Math.random() should give; but, as in its range the absolute resolution
of Number is lowest (for values of 0.5 and higher) at 2^-53, the
greatest uniformity is obtained by allowing return values of N * 2^-53
for 0 <= N < 2^53.  Anything else is less than ideal.

Comments?

On a Mac: resolA, resolB, resolC :

Safari 3.1.2: 31,31,31
FF 2.0.0.16 : 53,53,53
FF 3.0.1 : 53,53,53
Opera 9.5.1: 32, 32, 32
IE 5.2.3: 54,53,54
iCab 3.0.5: 64,53,57

--Jorge.
 
J

Jorge

In comp.lang.javascript message <[email protected]
rlyn.invalid>, Fri, 25 Jul 2008 23:30:59, Dr J R Stockton
<[email protected]> posted:







Or    for (x = Math.random(), t = 0 ; x%1 > 0 ; t++) x *= 2

I have changed <URL:http://www.merlyn.demon.co.uk/js-randm.htm#MR>
subsection "For Your Browser".

I see, in WinXP, Safari & Opera showing as expected for a 32-bit PRBS
converted to Double.  Firefox has a PRBS of at least 53 bits, with my
results comparable with Math.random() returning only multiples of 2^-53.
But MSIE apparently can return an odd multiple of 2^-54, which seems
unsound.

ISO/IEC 16262 15.8.2.14 is somewhat liberal in its definition of what
Math.random() should give; but, as in its range the absolute resolution
of Number is lowest (for values of 0.5 and higher) at 2^-53, the
greatest uniformity is obtained by allowing return values of N * 2^-53
for 0 <= N < 2^53.  Anything else is less than ideal.

Comments?

On a Mac: resolA, resolB, resolC :

Safari 3.1.2: 31,31,31
FF 2.0.0.16 : 53,53,53
FF 3.0.1 : 53,53,53
Opera 9.5.1: 32, 32, 32
IE 5.2.3: 54,53,54
iCab 3.0.5: 64,53,57

--Jorge.
 
D

Dr J R Stockton

On a Mac: resolA, resolB, resolC :

Safari 3.1.2: 31,31,31
FF 2.0.0.16 : 53,53,53
FF 3.0.1 : 53,53,53
Opera 9.5.1: 32, 32, 32
IE 5.2.3: 54,53,54
iCab 3.0.5: 64,53,57


Thanks; I've updated the page. It may be that with various different
range parameters for ResolC a greater understanding of what iCab is
doing might be obtained. The author of iCab can be found via <http://
www.icab.de/>.
 
J

Jorge

Or    for (x = Math.random(), t = 0 ; x%1 > 0 ; t++) x *= 2

Or do {} while (t++, (x*= 2)%1 > 0)

Why there's not an "international obfuscated JavaScript code
contest" ?

--Jorge.
 
D

Dr J R Stockton

In comp.lang.javascript message <abae3935-396a-4fca-aaf7-ea9e7c832a91@i7
6g2000hsf.googlegroups.com>, Mon, 28 Jul 2008 08:02:04, Jorge
Or do {} while (t++, (x*= 2)%1 > 0)

or do {t++} while ((x*=2)%1)
Why there's not an "international obfuscated JavaScript code
contest" ?

You're in it! But that means "count doublings until integer", which
expresses the need rather exactly. Page js-randm.htm again updated;
results as before.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top