Help ! Shopping Cart Problem



JRS: In article <5vidnaq4xJCLKP7eRVn-qw@xxxxxxxxxxx>, dated Sat, 29 Oct
2005 13:45:09, seen in news:comp.lang.javascript, Randy Webb
<HikksNotAtHome@xxxxxxx> posted :
>
>You are trying to round by multiplying by 100 and then dividing by 100.
>That introduces the error you see because computers cannot represent
>9.95 exactly in Base 2. So you get the error.

Non sequitur.

If the representation of 9.95, while not exact, had sufficient zeroes at
the end of the mantissa, then the multiplication would be exact, and the
division would be exact and would give the original value. There,
"sufficient zeroes" is a sufficient, but probably not entirely
necessary, condition.

If 9.95 being represented inexactly were the only criterion, then ISTM
that one would expect more wrong results than the OP's code actually
gives.


The real problem is that the OP's type of method effectively truncates
to cents using an inexact input; the input must be rounded to exact
cents, which has to be done into a representation where all such values
can be rendered exactly.

The ordinary conversions of a general Number to a String do that
rounding, as does the obvious one of the conversions to an integer Cent
number.

Note that one should always multiply or divide by 100 in preference to
dividing or multiplying by 0.01, since 100 is represented exactly but
0.01 is not.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
The Big-8 newsgroup management is attempting to legitimise its questionable
practices while retaining its elitist hegemony. Read <URL:news:news.groups>.
.



Relevant Pages

  • Re: dividing into
    ... I just don't see how that is multiplying one by three. ... terms of the number of items you end up with, just as dividing 2 pizzas ... dividing two pizzas into 8 equal pieces. ... To me, this illustrates that "dividing an object into 3 pieces" in normal English, is more like a multiplication than a division process in mathematics, since "division" in mathematics implies dividing into equal parts. ...
    (alt.usage.english)
  • Re: dividing into
    ... I suggest that mine is the common meaning as far as mathematics ... I just don't see how that is multiplying one by three. ... terms of the number of items you end up with, just as dividing 2 pizzas ... But I can't see any possibility of 4x2=8 representing the operation of dividing two pizzas into 8 equal pieces. ...
    (alt.usage.english)
  • Re: Kinda Miss TG...
    ... dividing by fractions than with multiplying by them. ... think of that as multiplying by the inversion of the fraction. ... Computing the number of photons hitting the camera sensor is just such ...
    (rec.music.classical.guitar)
  • Re: Kinda Miss TG...
    ... multiplying or dividing by a number less than 1. ... The local diesel power plant here on this tiny island runs at about ... Rest assured that I can multiply numbers too, even fractions! ... The case is clearer with dividing by fractions than with multiplying by them. ...
    (rec.music.classical.guitar)
  • Re: Evolution of PL/I
    ... big snip ... you add or remove a leading blank it's like multiplying or dividing by 10. ... It definitely is not WYSIWYG in as much as the blanks are invisible. ...
    (comp.lang.pl1)