Javascript Division Error
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company
Javascript Floating Point Error Example
Business Learn more about hiring developers or posting ads with us Stack Overflow Questions javascript decimal data type Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers,
Bigdecimal Javascript
just like you, helping each other. Join them; it only takes a minute: Sign up Javascript - slight division error [duplicate] up vote -1 down vote favorite Possible Duplicate: Javascript Math Error: Inexact Floats I did javascript toprecision this this.cues = [] for(var i, i = 1; i <= 8; i++){ cue = this.length * (i/8.0) this.cues.push(cue) where this.length is a double. I checked the division and found that I was off by .000002 up to about the fourth array cell. Is there any javascript-specific cause for this anybody can think of? It may just be the environment, so if not no big deal If so, can I fix it? Many thanks javascript integer precision in advance javascript math share|improve this question asked Jul 1 '12 at 19:27 jamesson 229114 marked as duplicate by Oliver Charlesworth, Anthony Grist, sachleen, David Titarenco, dsolimano Jul 1 '12 at 19:30 This question was marked as an exact duplicate of an existing question. Another double - which I get, it's just off by .000002 –jamesson Jul 1 '12 at 19:29 There should be no reason for this to be off, since 8.0 is perfectly representable in IEEE-754, as should your reasonably-sized integers divided by it. –Ignacio Vazquez-Abrams Jul 1 '12 at 19:29 What is the value of this.length anyways? –Ignacio Vazquez-Abrams Jul 1 '12 at 19:32 375.386658 - interesting, a single voice of dissent but an extremely high-rep one :) –jamesson Jul 1 '12 at 19:35 That would be why then; your original number isn't exactly precise. >>> (375.386658).hex() '0x1.7762fc04c8bcap+8' >>> '%.25f' % 375.386658 '375.3866580000000112704583444' –Ignacio Vazquez-Abrams Jul 1 '12 at 19:38 | show 1 more comment 2 Answers 2 active oldest votes up vote 0 down vote This is expected. Floating point operations on computers have small accuracy errors like this because of the way they are stored internally in a computer. For more information, I recommend you read What Every Computer Scientist Should Know About Floating-Point
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring
Javascript Int To Float
developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question javascript float precision 2 x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them;
Floating Point Rounding Error
it only takes a minute: Sign up Is floating point math broken? up vote 1298 down vote favorite 530 0.1 + 0.2 == 0.3 -> false 0.1 + 0.2 -> 0.30000000000000004 Any ideas why this happens? math language-agnostic floating-point http://stackoverflow.com/questions/11284981/javascript-slight-division-error floating-accuracy share|improve this question edited Dec 1 '14 at 10:47 James Donnelly 70.3k1787129 asked Feb 25 '09 at 21:39 Cato Johnston 8,23772740 32 Floating point variables typically have this behaviour. It's caused by how they are stored in hardware. For more info check out the Wikipedia article on floating point numbers. –Ben S Feb 25 '09 at 21:41 21 JavaScript treats decimals as floating point numbers, which means operations like addition might be subject to rounding error. You http://stackoverflow.com/questions/588004/is-floating-point-math-broken might want to take a look at this article: What Every Computer Scientist Should Know About Floating-Point Arithmetic –matt b Feb 25 '09 at 21:42 16 This might be something obvious to some people, but not to javascript users like myself that just expect simple addition to work. Nobody ever told ME this, and I studied CS in Belgium ..., It might be correct according to the standard, but it seems very crappy and confusing to me. –Mvision Jun 5 '12 at 9:45 82 It's not obvious, @Mvision; it's just well-trod ground. Anyone who has to program with floating-point numbers should, first, know what the heck floating-point numbers are, including their limitations. As for "crappy and confusing": if you're designing a programming system, you have to pick some mechanism for storing non-integer values. As with most things in life, there is no one ideal solution, but rather several to pick from, each with advantages and disadvantages. Due to the particular trade-offs involved, floating-point is the most popular choice in modern systems. –Mark Reed Sep 9 '12 at 14:55 8 Actually, the error is because there is no way to map 0.1 to a finite binary floating point number. –Umair Aziz Oct 25 '12 at 7:21 | show 14 more comments 27 Answers 27 active oldest votes up vote 1031 down vote accepted Binary floating point math is like this. In most programming languages,
Know About Floating Points What Every JavaScript Developer Should Know About Floating Points By Xuanyi Chew At some point in a JavaScript developer's career, she will come across strange bugs - basic mathematics that do not seem to make sense. And at some https://modernweb.com/2014/02/17/what-every-javascript-developer-should-know-about-floating-points/ point or another, she will be told that numbers in JavaScript are actually floating points. Any attempts to understand floating points or why they act so strangely will eventually lead to a long and a difficult-to-read article. The goal of this article is to make floating points simple for JavaScript developers. This article assumes the reader is familiar with base-2 (binary) representations of base-10 (decimal) numbers (i.e. 1 is written as 1b, 2 is 10b, 3 is 11b, floating point 4 is 100b… etc). To even further clarify things, in this article, the word “decimal” mostly refers to the decimal representation of numbers (for example: 2.718) in the machine. The word “binary” in this article refers to a machine representation. Written representations will be referred to as “base-10″ and “base-2″ respectively. Floating Points To figure out what a floating point is, we first start with the idea that there are many kinds of numbers, which we will go javascript division error through. We call 1 an integer - it is a whole number with no fractional values in it. ½ is what's called a fraction. It implies that the whole number 1 is being divided into 2. The concept of fractions is a very important one in deriving floating points. 0.5 is commonly known as a decimal number. However, a very important distinction needs to be made - 0.5 is actually the decimal (base-10) representation of the fraction ½. This is how ½ is represented when written as a base-10 number - for this article, we will call it the dot notation. We call 0.5 a finite representation because the numbers in the representation for the fraction are finite - there are no more numbers after 5 in 0.5. An infinite representation would for example be 0.3333... when representing ⅓. Again, this idea will be important later on in our discussion. There exists another way of representing numbers other than as whole numbers, fractions or decimal notations. You might have actually seen it before. It looks something like this: 6.022 x 1023 (Trivia: That is Avogadro's number, which is the number of molecules in a mole of chemical solution). It's commonly known as the standard form, or scientific notation. That form can be generalized to something that looks like this: D1.D2D3D4...Dp x BE The general form is called a floating p