Evertjan.
8/27/2014 10:08:00 PM
"Christoph M. Becker" <cmbecker69@arcor.de> wrote on 27 aug 2014 in
comp.lang.javascript:
>> Both are and must be wrong in the sense that the binary converions
>> of floting point fractional decimal values NEVER can be right.
>
> Wrong. Consider e.g. 0 or 2.
No 0 and 2 are integers.
>> So why use a standard that defines how wrong they must be?
>
> To be able to quantify the inaccuracy.
There is no need for that.
>> Just remember every time,
>> that you should not compare such values without prior rounding.
>
> AFAIK it is common to compare for equality with
>
> Math.abs(a - b) < epsilon
What has "common" to do with it?
It is a way,
but a and b stay unrounded,
and if reused and reused
you might find yourself in trouble
with the wrong value of epsilon.
> See also Number.EPSILON[1].
I don't see that.
>
>> If you are a sticker for correctness and these are monitary values,
>> like $ or , use integer cent values in variables, and write decimals
>> with string manupulation.
>
> Are you suggesting that storing $123.45 as 12345 is better than storing
> it as 123.45?
I am not 'suggesting' anything,
I am not saying 'better',
just read my words.
But indeed, it is safer to manipulate integers.
> As ECMAScript implementations typically have no integer
> type, there is no difference regarding the precision of further
> calculations.
I wes not talking about the integer type,
I was not talking about Javascript [ECMA if you like],
but about integers.
There is a rather a big difference
'regarding the precision of further calculations.'
that difference is called safety of exactness.
Integers in binary storage are exact
and so are simple arithmetic with those numbers, except for division,
as long as the precision stays within practical limits.
input '23.45' string, store as a = 2345
input '23.4' string, store as b = 2340
do c = a - b
c contains 5
convert c to string d 0.05
write d
Compare this to:
document.write(23.45-23.4); // 0.05000000000000071
You are quite in your right
not to call the first method "better",
but in monetary calculus, I would disagree.
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)