Hi all,
I read a lot of info on this issue, but still don't get it. Why does
this happen?
document.write( ".05" + " \+ " + ".05" + " \= " + (.05 + .05) +
"<br>");
document.write( "1.05" + " \+ " + ".05" + " \= " + (1.05 + .05) +
"<br>");
document.write( "2.05" + " \+ " + ".05" + " \= " + (2.05 + .05) +
"<br>");
Outputs:
..05 + .05 = 0.1
1.05 + .05 = 1.1
2.05 + .05 = 2.0999999999999 996
I also tried the code provided in this group's FAQ, and it worked
except for when it reaches an "even" number which should display 6.00
for example, it displays 5.100.
thx!
J. 8 5253
On 23/9/04 9:14 pm, janice wrote: Hi all,
I read a lot of info on this issue, but still don't get it. Why does this happen?
(snip) 2.05 + .05 = 2.0999999999999 996
In binary, these numbers go on for ever:
2.05 = 10.000011001100 110011001100110 0...
0.05 = 0.0000110011001 100110011001100 ...
2.10 = 10.000110011001 100110011001100 1...
With a fixed number of bits it is impossible to represent these numbers
exactly, so they contain errors right from the start. Addition makes the
errors even larger. For example, if I added together the first two binary
numbers shown above, the last digit of the result would be 0, not 1.
It's just like adding 1/3 to 2/3 in base 10:
2/3 = 0.6666666666666 66
+ 1/3 = 0.3333333333333 33
= 0.9999999999999 99 (not 1.000000)
It is just as impossible to write 1/3 in decimal as it is to write 0.05 in
binary.
I hope that makes sense.
--
Philip Ronan ph***********@v irgin.net
(Please remove the "z"s if replying by email)
> I read a lot of info on this issue, but still don't get it. Why does this happen?
.05 + .05 = 0.1 1.05 + .05 = 1.1 2.05 + .05 = 2.0999999999999 996
That's really annoying, isn't it? JavaScript uses binary floating point
notation internally to represent numbers. Floating point is very useful
for some scientific applications, but for other applications it has some
severe shortcomings.
In case, you are seeing that it cannot exactly represent decimal
fractions. That would be ok on a planet where people do not commonly use
decimal fractions. It might seem that floating point was a poor choice. http://www.crockford.com/#javascript
Douglas Crockford said: I read a lot of info on this issue, but still don't get it. Why does this happen?
.05 + .05 = 0.1 1.05 + .05 = 1.1 2.05 + .05 = 2.0999999999999 996
That's really annoying, isn't it? JavaScript uses binary floating point notation internally to represent numbers. Floating point is very useful for some scientific applications, but for other applications it has some severe shortcomings.
In case, you are seeing that it cannot exactly represent decimal fractions. That would be ok on a planet where people do not commonly use decimal fractions. It might seem that floating point was a poor choice.
The advantages of using standard IEEE floating point representation
far outweigh any disadvantage. The infinitesimal error encountered
in typical web-page arithmetic is easily corrected by rounding.
When representing numbers in binary, each bit represents either
0 or 1 times 2 raised to some positive or negative integer power,
so a binary representation of a value between 0 and 1 is the sum
of the series (b1)/2 + (b2)/4 + (b3)/16 + (b4)/32 + ...
where each (bn) value is either 0 or 1. It's mathematically
impossible to represent some decimal fractions as a finite
series of such terms.
Lee wrote: Douglas Crockford said:
I read a lot of info on this issue, but still don't get it. Why does this happen?
.05 + .05 = 0.1 1.05 + .05 = 1.1 2.05 + .05 = 2.0999999999999 996
That's really annoying, isn't it? JavaScript uses binary floating point notation internally to represent numbers. Floating point is very useful for some scientific applications, but for other applications it has some severe shortcomings.
In case, you are seeing that it cannot exactly represent decimal fractions. That would be ok on a planet where people do not commonly use decimal fractions. It might seem that floating point was a poor choice.
The advantages of using standard IEEE floating point representation far outweigh any disadvantage. The infinitesimal error encountered in typical web-page arithmetic is easily corrected by rounding.
When representing numbers in binary, each bit represents either 0 or 1 times 2 raised to some positive or negative integer power, so a binary representation of a value between 0 and 1 is the sum of the series (b1)/2 + (b2)/4 + (b3)/16 + (b4)/32 + ... where each (bn) value is either 0 or 1. It's mathematically impossible to represent some decimal fractions as a finite series of such terms.
That is precisely why floating point is poorly suited to most
applications. For applications that require decimal precision (such as
those involving money), there are other representations that are much
better. Scaled integers and BCD, for example.
2.0999999999999 996 shows 3.0 to 16 decimal places, nearly all of them
wrong. This result, to most people, is surprising.
Douglas Crockford said: That is precisely why floating point is poorly suited to most applications . For applications that require decimal precision (such as those involving money), there are other representations that are much better. Scaled integers and BCD, for example.
2.099999999999 9996 shows 3.0 to 16 decimal places, nearly all of them wrong. This result, to most people, is surprising.
When handling money, all you need to do is round to thousandths
and display to hundredths. It's pretty trivial.
The fact that it's surprising to neophytes isn't much of
a reason to change it.
Lee <RE************ **@cox.net> writes: When handling money, all you need to do is round to thousandths and display to hundredths. It's pretty trivial.
No code is so trivial that it can't contain an error (except perhaps
the empty statement :).
The fact that it's surprising to neophytes isn't much of a reason to change it.
It's perhaps too late to change it, although I don't think a change to
BCD-numbers like C#'s "decimal" type would be noticed by most
applications.
However, as a purely theoretical observation, I'd say that for a
script language that is used by so many (until recently)
non-programmers, it would probably be better to use a decimal number
format to avoid exactly these questions. It happens often enough
to be in the FAQ.
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
JRS: In article <5b************ **************@ posting.google. com>,
dated Thu, 23 Sep 2004 13:14:59, seen in news:comp.lang. javascript,
janice <go****@janic e-eirich.com> posted : I also tried the code provided in this group's FAQ,
The FAQ says : Operations on integers are exact if the true result and
all intermediates are integers within that range.
If you want results exact to the cent, then work in cents, not in euros.
Integers are exact up to as little over 9,000,000,000,0 00,000; enough
for most budgets.
--
© John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
JRS: In article <ek**********@h otpop.com>, dated Fri, 24 Sep 2004
19:20:58, seen in news:comp.lang. javascript, Lasse Reichstein Nielsen
<lr*@hotpop.com > posted : However, as a purely theoretical observation, I'd say that for a script language that is used by so many (until recently) non-programmers, it would probably be better to use a decimal number format to avoid exactly these questions. It happens often enough to be in the FAQ.
The same applies in Delphi groups of news:borland.*, except that they
have no newsgroup FAQs.
--
© John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demo n.co.uk/> - FAQish topics, acronyms, & links.
For news:borland.*, use their server newsgroups.borl and.com ; but first read
Guidelines <URL:http://www.borland.com/newsgroups/guide.html> ff. with care. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Roger Leigh |
last post by:
Hello,
I'm writing a fixed-precision floating point class, based on the ideas
in the example fixed_pt class in the "Practical C++ Programming" book
by Steve Oualline (O' Reilly). This uses a long int to store the
value, and the precision (number of decimal points) is variable (it's
a templated class):
template <size_t _decimal_places = 4>
class FixedFloat {
|
by: Dave |
last post by:
Hi folks,
I am trying to develop a routine that will handle sphere-sphere and
sphere-triangle collisions and interactions. My aim is to develop a
quake style collision engine where a player can interact with a rich
3D environment. Seem to be 90% of the way there! My problems are
related to calculations where the result tends to zero (or another
defined limit.)
Have loads of cases where this kind of interaction occurs but this one
|
by: cody |
last post by:
no this is no trollposting and please don't get it wrong but iam very
curious why people still use C instead of other languages especially C++.
i heard people say C++ is slower than C but i can't believe that. in pieces
of the application where speed really matters you can still use "normal"
functions or even static methods which is basically the same.
in C there arent the simplest things present like constants, each struct and
enum...
|
by: Vinoth |
last post by:
I'm working in an ARM (ARM9) system which does not have Floating point
co-processor or Floating point libraries. But it does support long long int
(64 bits).
Can you provide some link that would discuss about ways to emulate floating
point calculations with just long int or long long int. For eg., if i've a
formula X=(1-b)*Y + b*Z in floating point domain, i can calculate X with
just long ints (but, some data may be lost in final division;...
|
by: michael.mcgarry |
last post by:
Hi,
I have a question about floating point precision in C.
What is the minimum distinguishable difference between 2 floating point
numbers? Does this differ for various computers?
Is this the EPSILON? I know in float.h a FLT_EPSILON is defined to be
10^-5. Does this mean that the computer cannot distinguish between 2
numbers that differ by less than this epsilon?
| |
by: Bryan Parkoff |
last post by:
The guideline says to use %f in printf() function using the keyword
float and double.
For example
float a = 1.2345;
double b = 5.166666667;
printf("%.2f\n %f\n", a, b);
|
by: ultimatewarrior |
last post by:
Hi all,
first of all I beg your pardon if this question has been asked
before, but I was unable to find anything in the past posts.
I have written a piece of code that was supposed to be quite portable,
and uses a lot fp numbers. Everything goes well on PPC cpus, but on
some x86 CPU I get a dramatic loss of performance. After some
investigations I've discovered that the problem is caused by some
numbers becoming subnormal, PPC cpus...
|
by: Peter |
last post by:
I have written this small app to explain an issue I'm having with a larger
program.
In the following code I'm taking 10 ints from the keyboard. In the call to
average() these 10 ints are then added and Divided by the number of ints to
return the average, a float.
Can someone see why I get for example: if the total is 227 then divided by
10 I get a return of 22.00000 instead of the correct answer 22.7.
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |