Bottom of This Page |
In this article the period (.) is used as decimal separator at text shown with a teletype (monospace) font. / I denne artikel er punktum (.) brugt som decimal separator på tekst vist med en fjernskriver (fast tegnafstand) skrift.
The only decimal separator known to JavaScript (new releases possibly excepted) is the decimal point, "."; and the language knows no thousands separator. / Den eneste decimal separator som JavaScript kender (nye udgivelser muligvis undtaget) er decimaltegnet, "."; og sproget kender ikke til tusindtalsseparator.
JavaScript storage of and arithmetic on non-integers is
necessarily inexact (almost always); this is based on JavaScript
Maths (Mathematics) and Floating-Point Numbers.
For example, displaying
Most real numbers, in particular those with finite base-10 representations,
cannot be held exactly in a "Double" (variable type, which
stores a double precision floating point number). This is why, for instance,
The above-mentioned function RoundToNdp, which arithmetically
rounds off a floating-point number to # decimal places is in this page's
source code version of the function called roundit. And it
uses the JavaScript
One can write 1.3550, or "1.3550", in code, or read the string "1.3550" from a control, and then store it in a variable V as type Number, and then convert it to a String "1.355"; but that does not mean that V actually has a value exactly equal to 1.355 (which cannot be represented exactly). Rounding to two decimal places by arithmetic will, quite correctly, be controlled by the sign of the difference from 1.355. Alternatively, one might convert the value of V to a String, which act itself includes some rounding; and then round that to two places by operations on the individual characters. / Man kan skrive 1.3550, eller "1.3550", i kode, eller læse strengen "1.3550" fra en kontrol, og derefter gemme det i en variabel V som typen Number (Decimaltal), og derefter konvertere det til en String (Streng) "1.355"; men det betyder ikke, at V faktisk har en værdi nøjagtigt svarende til 1.355 (som ikke kan gengives nøjagtigt). Afrunding til to decimaler ved aritmetik vil, helt korrekt, blive kontrolleret af tegnet på forskellen fra 1.355. Alternativt kan man konvertere værdien af V til en String (Streng), hvilken handling i sig selv indeholder nogen afrunding; og så runde af til to decimaler ved operationer på de enkelte tegn.
If doing arithmetic other than addition, subtraction, and multiplication by an integer, one should consider what sort of intermediate rounding is indeed proper for the field; rounding to pence internally is not necessarily legitimate. / Hvis der udføres aritmetik andet end addition, subtraktion og multiplikation med et heltal, bør man overveje, hvilken slags mellem-afrunding som er rigtig korrekt for feltet; afrunding til pence (øre) internt, er ikke nødvendigvis legitim.
When a calculated Number is to be converted to a String with, say, two decimal places, it must be rounded arithmetically; it cannot just be extended or truncated if nominally a multiple of 0.1 or 1.0; this is because of imperfect accuracy in calculation. / Når et beregnet Number (Decimaltal) skal konverteres til en String (Streng) med, lad os sige, to decimaler, skal det afrundes aritmetisk; det kan ikke bare forlænges eller afkortes, hvis det nominelt er et multiplum af 0.1 eller 1.0; dette er på grund af ufuldkommen nøjagtighed i beregningen.
Because of floating-point rounding errors, any code used to round all non-negative numbers should allow for slightly negative numbers practically equivalent to zero. / På grund af afrundingsfejl i decimaltal, bør enhver kode, der bruges til at afrunde alle ikke-negative tal tillade lidt negative tal, som stort set svarer til nul.
JavaScript stores the value of a number as an IEEE "Double", in binary floating-point. Most values, including most of those with one, two, or three decimal places, are not stored exactly. / JavaScript gemmer værdien af et tal som en IEEE "Double" i binær decimaltal. De fleste værdier, herunder de fleste af dem med en, to eller tre decimaler, gemmes ikke nøjagtigt.
JavaScript currently only has one number type, floating-point,
which is encapsulated as the object type "Number". The
actual value is held in binary as an
Integers can only be held accurately up to 53 bits plus
sign, and numbers are only, at best, accurate to 15-16
significant decimal digits. Any integer from 0 up to
Given that Numbers are "Doubles", arithmetic is as exact as possible, but no more. Simple binary fractions are exact, provided no more than 53 bits in all are needed. Operations on integers are exact if the true result and all intermediates are integers within that range, except where bitwise operators are used, when the range is signed 32-bit. / Givet at tal er "Doubles", er aritmetik så nøjagtig som muligt, men ikke mere. Simple binære brøker er nøjagtige, forudsat at ikke mere end 53 bit i alt er nødvendig. Operationer på heltal er nøjagtige, hvis det sande resultat og alle mellem-resultater er heltal inden for dette område, medmindre bitvise operatører anvendes, når området er med angivelse af om tallet er positivt eller negativt 32-bit.
NOTE: If too many figures are demanded, existing rounding errors will show, and arithmetic may introduce others. JavaScript itself supports only up to 15 significant figures. / Bemærk: Hvis for mange cifre kræves, vil de eksisterende afrundingsfejl vise sig, og aritmetik kan indføre andre. JavaScript selv understøtter kun op til 15 betydende cifre.
Where it is known that a calculated number should have an integer
value, one can use
Non-integer computed results should not normally be compared for equality (if necessary use fuzzy comparisons); and generally need rounding for display. / Ikke-heltal beregnede resultater bør normalt ikke sammenlignes for lighed (eventuelt brug fuzzy sammenligninger); og har generelt behov for afrunding til visning.
For example,
Fuzzy Comparison: The logical AND and
OR operators also provide us with one solution
to the problem of floating-point comparison. While it may
not be possible ever to determine whether x is
exactly equal to the constant 3.0, you can be
certain that it is close using a combined test such as:
You can see the JavaScript by using View Source. / Man kan se JavaScript'et ved at bruge Vis Kilde.
Sources: Various books, the Internet, and various encyclopedias.
Kilder: Forskellige bøger, internettet og forskellige leksikoner.
|