scrip drops a penny when amount ends with .d0

D

Dr John Stockton

JRS: In article <[email protected]>
, dated Wed, 10 May 2006 10:08:51 remote, seen in
Richard Cornford
Once again the output from this function should not be expected to
produce differing results for input of quantity '3' or '6' and unit
price '.7' and '.70', and it does not:-

But you did not use format.

In code posted by an inexperienced or naive user, one should half-expect
that code formatting float dollars to dollars point integer cents will
be wrong.

The code earlier recommended in this group was wrong (I can't recall
whether it was in the FAQ; ISTR that it failed with 0.07).

Code posted here by newcomers since the present FAQ 4.6 was written has
been wrong.

The OP's job should evidently produce exact integer cents as a result of
the arithmetic, if implemented in decimal arithmetic. It may well be
that it actually produces, sometimes, an undersize Double, and that the
OP's format includes Math.floor().

If there is a difference in numerical value between input strings ".7"
and ".70". then the OP has a problem outside format.


To get anywhere, the OP should either learn javascript including the
implications of using IEEE Doubles, or he should post a cut-down version
of his code, complete and ready to run, with an exact statement of
sample data that it uses and what good and bad results that data gives.
 
R

RobG

The Question again..... [...]

3 * .7 = 2.0999999999999996
6 * .7 = 4.199999999999999
WRONG ANSWERS

Use your calculators
A correct response should be:
3 * .7 = 2.10
6 * .7 = 4.20


The above code gives the correct resonse to all other calculations that

I have tried.

You were pointed to FAQ 4.6: How do I convert a Number into a String
*with exactly 2 decimal places*?

The explanation is there: "Rounding of x.xx5 is uncertain, as such
numbers are not represented exactly."

The very next FAQ 4.7: "Why does 5 * 1.015 != 5.075 or 0.05+0.01 !=
0.06?" also fits your question:

" Javascript numbers are represented in binary as IEEE-754 Doubles,
with a resolution of 53 bits, giving an accuracy of 15-16 decimal
digits; integers up to about 9e15 are precise, but few decimal
fractions are. Given this, arithmetic is as exact as possible,
but no more. Operations on integers are exact if the true result
and all intermediates are integers within that range.

"In particular, non-integer results should not normally be compared
for equality; and non-integer computed results generally need
rounding; see 4.6."


Search this news group for questions on rounding, you will find this
topic discussed at length and also that you are not alone in your
frustration and confusion. :)
 
D

Dr John Stockton

JRS: In article <[email protected]>,
dated Wed, 10 May 2006 12:55:07 remote, seen in
news:comp.lang.javascript, (e-mail address removed) posted :
eval()'s removed...the problem is still there.

Of course. Your real problem has been personal ignorance combined with
an unwillingless to accept the advice which you have been given. It may
be combined with an inability to learn; but it's hard to distinguish
that from mere obstinacy.

The results of your arithmetic manipulations, before format, are
necessarily inexact; you have chosen to work in a unit that you need to
represent hundredths of, and you are using a binary number
representation in which 96% of hundredths cannot be represented exactly.

You have chosen to display those results, which are close approximations
to the ideal, by a method which, at least sometimes, truncates removing
0.999... pennies. You see the consequences.

Post your format function.

Moreover, you're still not complying with FAQ 2.3.

One can only hope that you are a sophomore or janitor, and not a member
of the teaching staff or an employee of a computer service.
 
T

Thomas 'PointedEars' Lahn

Lasse said:
eval("quant" + i + "=document.RFO.quant_" + i + ".value");

Try:
window["quant"+i] = document.RFO["quant_"+i].value;
or more standards compliant:
window["quant"+i] = document.forms['RFO'].elements['quant_'+i].value;

We don't want to give people the idea that they have to use eval
for property access.

And we don't want to give them the idea that `window' is
a generally available reference for that purpose either:

var _global = this;

function ...(...)
{
// ...
_global["quant" + i] =
document.forms['RFO'].elements['quant_' + i].value;
// ...
}


PointedEars
 
R

Randy Webb

Thomas 'PointedEars' Lahn said the following on 5/18/2006 11:50 AM:
Lasse said:
eval("quant" + i + "=document.RFO.quant_" + i + ".value");
Try:
window["quant"+i] = document.RFO["quant_"+i].value;
or more standards compliant:
window["quant"+i] = document.forms['RFO'].elements['quant_'+i].value;

We don't want to give people the idea that they have to use eval
for property access.

And we don't want to give them the idea that `window' is
a generally available reference for that purpose either:

And once again, name a *BROWSER* that doesn't use window as the global
object. After all, this group is mainly geared towards browsers and
Internet web sites.
 
V

VK

Randy said:
And once again, name a *BROWSER* that doesn't use window as the global
object.

That was easy: Internet Explorer 4.0 or higher. You meant to say "a
browser not having window object among host objects". That's indeed
/very/ challenging. Yet Mr. Lahn's beloved spooky-booh is "there is
Global object but there is not window object".
 
R

Randy Webb

VK said the following on 5/18/2006 3:06 PM:
That was easy: Internet Explorer 4.0 or higher.

Really? So in IE4 or higher, this will produce different results:

var myVar = 'something';
var _global = this;
alert(window[myVar]);
alert(_global[myVar]);

Different results eh?
You meant to say "a browser not having window object among host objects".

No, I meant "a browser where window does not refer to the global object".
That's indeed /very/ challenging.

No more challenging than what I asked.
Yet Mr. Lahn's beloved spooky-booh is "there is Global object but there
is not window object".

The same spooky-booh can be said about a Global object as well.

Prove that every UA in existence has a Global object.

Now there is a challenge. It can no more be proven than the opposite can
be proven with access to every UA available.
 
V

VK

Randy said:
VK said the following on 5/18/2006 3:06 PM:
That was easy: Internet Explorer 4.0 or higher.

Really? So in IE4 or higher, this will produce different results:

var myVar = 'something';
var _global = this;
alert(window[myVar]);
alert(_global[myVar]);

Different results eh?

Oh com'on... There was a discussion already with the participation of
Mr. Cornford and all MSDN links. If Microsoft itself says that Global
object and window object are not the same, Mr. Webb's opinion will not
change the picture.

Actually Global === window is a bogus in Gecko either, a nice imitation
atop of the reality to "visually" conform with ECMA specs.

I promised to show how to use parallel Global spaces to implement
private/protected members a while ago. It just happens that your own
things may get not really your own if you are not careful enough :-(
Now the problem is solved. Here is "Ajax" behavior you are free to
attach to any amount of block elements on the page like say DIV. Of
course you should set URI attribute for each element to some existing
file/url. You also can set optional RATE attribute to define refresh
rate (3sec default).

Please note that within the binding there is Global space. For each
bound element there is its own Global space from where you still can
address window host object. At the same time these Global spaces are
not visible to /that/ Global space which is "the same as window
object". It doesn't see any of global variables in these parallel
spaces and it can communicate with them only over pre-defined public
methods. The same story with the binding part for Gecko (coming soon if
one wants).

"Welcome to the real world, Neo"
("Matrix" movie)
;-)

// sample HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<title>Bx2</title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<style type="text/css">
body {
font: 1em Verdana, Geneva, sans-serif;
color: #000000;
background-color: #FFFFFF;
}

p {
width: 50%;
height: 10em;
overflow: auto;
margin: 0.5em 0.5em;
border: thin inset;
padding: 0.5em 0.5em;
}

#test {
behavior: url(ajax.htc);
}
</style>
</head>

<body>

<p id="test" uri="test.txt">No content</p>

</body>
</html>



// ajax.htc

<?xml version="1.0"?>
<!--
**
* Bx2 Twinpair::HTC
* IXMLHTTPRequest
*
* VK [[email protected]]
-->

<public>
<component>
<method name="setRequest" />
<method name="start" />
<method name="stop" />

<property name="randomizeURI"
value="yes" />
<property name="lastError"
value=""
get="getError"
put="readOnly" />

<attach
event="oncontentready"
onevent="constructor()" />
<attach
event="ondetach"
onevent="destructor()" />
</component>


<script type="text/Jscript"><!--

var $errors = [
"",
"Couldn't instantiate IXMLHTTPRequest object",
"Connection error"
];

var $err = $errors[0];

var $URI = '';

var $req = null;

var $callback = null;

var $rate = 3;

var $timer = null;


try {
$req = new ActiveXObject('Msxml2.XMLHTTP');
}
catch(e) {
$err = $errors[1] + ': ' + e.message;
}

if (($req == null)&&('undefined' != typeof XMLHttpRequest)) {
/* in hope of IE7 or higher */
try {
$req = new XMLHttpRequest();
}
catch(e) {
$err = $errors[1] + ': ' + e.message;
}
}

$callback = function() {
if ($req.readyState == 4) {
if (($req.status == 200)||($req.status == 0)) {
$err = $errors[0];
element.innerHTML = $req.responseText;
}
else if (($req.status == 204)||($req.status == 304)) {
$err = $errors[0];
}
else {
$err = $errors[2] + ': ' + $req.statusText;
}
if ($rate) {
$timer = window.setTimeout(this.makeRequest,$rate*1000);
}
}
}


function constructor() {
$URI = element.getAttribute('URI',2) || '';
$rate = parseInt(element.getAttribute('RATE'),10) || 3;
if ($URI != '') {
makeRequest();
}
}


function destructor() {
if ($timer != null) {
window.clearTimeout($timer);
}
if ($req.readyState != 4) {
$req.abort();
}
}


function setRequest(URI, RATE) {
$URI = URI || '';
$rate = isNaN(parseInt(RATE,10)) ? 3 : RATE;
}


function makeRequest() {
if ('yes' == element.randomizeURI) {
var rnd = ($URI.lastIndexOf('?') != -1)
? ('&rnd='+(new Date()).getTime())
: ('?rnd='+(new Date()).getTime());
var URI = $URI + rnd;
}
else {
var URI = $URI;
}
$req.open('GET', URI, true);
$req.onreadystatechange = $callback;
$req.send('');
}


function start() {
makeRequest();
}


function stop() {
if ($timer != null) {
window.clearTimeout($timer);
}
}


function getError() {
return $err;
}


function readOnly(val) {
return val;
}

//--></script>


</public>
 
E

Evertjan.

VK wrote on 18 mei 2006 in comp.lang.javascript:
If Microsoft itself says that Global
object and window object are not the same, Mr. Webb's opinion will not
change the picture.

Microsoft != God

Picture that!
 
T

Thomas 'PointedEars' Lahn

VK said:
That was easy: Internet Explorer 4.0 or higher.
Nonsense.

You meant to say "a browser not having window object among host objects".
That's indeed /very/ challenging. Yet Mr. Lahn's beloved spooky-booh
is "there is Global object but there is not window object".

You got my point -- almost. `window' is a host-defined property of the
Global Object, a reference to a host object; not a built-in reference to a
native object. However, one can /not/ expect a host object to work like a
native object, including augmentation with new properties. See the
ECMAScript Language Specification, Edition 3 Final, 4.3.8 (Definitions:
Host Object) and 8.6.2 (Internal Properties and Methods).


PointedEars
 
V

VK

VK wrote on 18 mei 2006 in comp.lang.javascript:
Microsoft != God

I /never/ stated the opposite. Mozilla is not God neither (equivalent
binding for Gecko browsers will follow).

"God is dead"
Nietzsche

"What we call matter is a certain systematic combination of the
elements (sensations)"
Mach

:)
 
E

Evertjan.

VK wrote on 18 mei 2006 in comp.lang.javascript:
VK wrote on 18 mei 2006 in comp.lang.javascript:

I /never/ stated the opposite.

You used MSes word for infallible final argument.
I objected to MS being the global object.
Mozilla is not God neither (equivalent
binding for Gecko browsers will follow).

Mozilla is dead, Firefox is it's phoenix.
"God is dead", Nietzsche

And God said: "Nietzsche is dead". And it was likewize.
"What we call matter is a certain systematic combination of the
elements (sensations)"
Mach

What we call Micosoft is neiter the measure of things nor always wrong.

However this matter doesn't matter in practice.
 
R

Richard Cornford

VK said:
Randy said:
VK said the following on 5/18/2006 3:06 PM:
Randy Webb wrote:
And once again, name a *BROWSER* that doesn't use
window as the global object.

That was easy: Internet Explorer 4.0 or higher.

Really? So in IE4 or higher, this will produce different
results:

var myVar = 'something';
var _global = this;
alert(window[myVar]);
alert(_global[myVar]);

Different results eh?

Oh com'on... There was a discussion already with the
participation of Mr. Cornford

You mean the thread where you made some claims about IE and failed to
demonstrate an concrete reality in those claims and I posted a
demonstration that the object referred to by the global window property
and the ECMAScript global object in IE were indistinguishable?

and all MSDN links.

One reference to an MSDN article that stated the consequences of some
internal implementation details in IE, and I then posted code that
demonstrated that those consequences were false in IE.

< >

I realise that your grasp of logic is defective but if you cannot
produce _anything_ to demonstrate your claims you should stop making
them.
If Microsoft itself says that Global object and window
object are not the same, Mr. Webb's opinion will not
change the picture.

Microsoft did not make that claim. It is your interpretation of what
they wrote in a page that has been demonstrated to be, at minimum,
incorrect in its conclusions. You have repeatedly demonstrated that your
interpretation of written English is so poor that you cannot understand
what others write so there is little point in your posting conclusions
based upon your faulty interpretations of documents unless you can back
them up with concrete demonstrations of your reality.
Actually Global === window is a bogus in Gecko either, a nice
imitation atop of the reality to "visually" conform with ECMA
specs.

And that claim also evaporated when you made it in the previous thread.
I promised to show how to use parallel Global spaces to
implement private/protected members a while ago.
<snip>

Nobody else cares. You are the only person perverse enough to attempt to
reproduce a capability that is inherit in the language by specification,
and do so in a way that is dependent upon the host environment,
differently dependent in different host environments (requiring separate
implementations for each environment), smeared in little chunks
throughout the document and several subsidiary files, and only possible
in precisely two host environments. Another example of your preferring
to things in the worst possible way available.

Richard.
 
V

VK

Richard said:
You mean the thread where you made some claims about IE and failed to
demonstrate an concrete reality in those claims

I explained the reason of the delay, but you are free to not believe
me. Now take the sample I linked and try it.

And there is a code with equal behavior for Gecko browsers (Firefox,
Mozilla Suite, Camino, Netscape 8 in Gecko mode). I would never post an
IE-only solution or proof of anything in c.l.j.
 
R

Richard Cornford

VK said:
Richard said:
VK said:
Randy Webb wrote:
VK said the following on 5/18/2006 3:06 PM:
Randy Webb wrote:
And once again, name a *BROWSER* that doesn't use ^^^^^^^^^^^
window as the global object. ^^^^^^^^^^^^^^^^^^^^^^^^^^^

That was easy: Internet Explorer 4.0 or higher. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Really? So in IE4 or higher, this will produce different
results:

var myVar = 'something';
var _global = this;
alert(window[myVar]);
alert(_global[myVar]);

Different results eh? ^^^^^^^^^^^^^^^^^^^^

Oh com'on... There was a discussion already with the ^^^^^^^^^^^^^^^^^^^^^^^^
participation of Mr. Cornford
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

You mean the thread where you made some claims about IE ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
and failed to demonstrate an concrete reality in those claims
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
I explained the reason of the delay, but you are free to not
believe me.

What delay, and in what? You have never demonstrated any difference
between the object referred to by the global window property and the
ECMAScript global object in IE, and you have never proposed that you
will do so, and if you had we would still be waiting.
Now take the sample I linked and try it.
<snip>

That script has no relevance to the issue. (Or anything else for that
matter).

Richard.
 

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,774
Messages
2,569,599
Members
45,163
Latest member
Sasha15427
Top