Solution for Floating-Point Errors

V

vunet.us

Is there a good solution for floating-point errors?
If I got:
0.00831 + 0.000001 = 0.00831109999999999999
can I avoid this by a better method than comparing 2 number length
after decimal point and using toFixed() to match shorter number with
another one?
Thanks.
 
D

David Mark

Is there a good solution for floating-point errors?
If I got:
0.00831 + 0.000001 = 0.00831109999999999999
can I avoid this by a better method than comparing 2 number length
after decimal point and using toFixed() to match shorter number with
another one?
Thanks.

Yes. To make it simpler, multiply each by 1000000 before adding:

8310 + 1 = 8311
 
D

David Mark

David Mark said the following on 7/23/2007 5:28 PM:




0.00831 + 0.000001 != 8311

To compare the result, as per my post, you have to multiply both sides
of the equation. I don't know how much more explicit I could have
made this and the OP obviously understood.
 
L

Lee

David Mark said:
To compare the result, as per my post, you have to multiply both sides
of the equation. I don't know how much more explicit I could have
made this and the OP obviously understood.

Saying "multiply each ... before adding" suggests that the things
referred to by "each" are also the things that you are adding, so
you certainly could have been much more clear about specifying
that you also have to multiply the comparison target.

How is it obvious that the OP understood? For all we know, he's
currently scratching his head because your solution, which seems
so simple, doesn't work when implemented.


--
 
V

vunet.us

How is it obvious that the OP understood?

Let's see: 0.00831 + 0.000001 = 0.008311

(0.00831*1000000) + (0.000001*1000000) == 0.008311/1000000
8310 + 1 = 8311/1000000

thus ==> 0.008311

What does everyone think? I'd appreciate a better way of handling this
if there is one...
Thanks
 
D

David Mark

David Mark said:









Saying "multiply each ... before adding" suggests that the things
referred to by "each" are also the things that you are adding, so

Of course they are.
you certainly could have been much more clear about specifying
that you also have to multiply the comparison target.

How much more clear could the example be? It explicitly shows the new
equation.
How is it obvious that the OP understood? For all we know, he's

Because he said so. And now two other posters have muddled the issue
and confused him. How do I know that? Read the thread.
currently scratching his head because your solution, which seems

No. Read the thread.
so simple, doesn't work when implemented.

The example equation "works" fine, in that both sides are equal.
 
D

David Mark

David Mark said the following on 7/23/2007 7:24 PM:








Answer:

Multiply both of your decimals by 100000 (in this case), add them, then
divide the answer by 100000 to get the decimal back.

You've done it again. There are six decimal places to shift (in this
case), not five. 100000 != 1000000.

And what the hell does "get the decimal back" mean anyway? Same for
"multiply both of your decimals."
You wanted more clear, you got it.

I didn't want anything. The OP wanted an answer and got it and
followed up to say it worked. Now you have made a real mess of
things.
 
L

Lee

David Mark said:
Of course they are.


How much more clear could the example be? It explicitly shows the new
equation.


Because he said so. And now two other posters have muddled the issue
and confused him. How do I know that? Read the thread.

You believe that, because a person says he understands your
solution, that he must actually "obviously" understand it?
Clearly you've never been involved in any sort of user support.

Your explanation is incomplete in that it doesn't clearly
specify that, in addition to multiplying each of the addends
before the addition, that you also multiply the value you're
using for comparison. As I said before, "multiply each ...
before adding" leads one to believe that you are talking only
about the numbers that you are adding.
No. Read the thread.


The example equation "works" fine, in that both sides are equal.

Only if you interpret it as you intended, as opposed to how
you described it.


--
 
L

Lee

(e-mail address removed) said:
Let's see: 0.00831 + 0.000001 = 0.008311

(0.00831*1000000) + (0.000001*1000000) == 0.008311/1000000
8310 + 1 = 8311/1000000

thus ==> 0.008311

You realize, don't you, that whether or not you actually
did understand it has nothing whatsoever to do with the
question of whether or not it was obvious that you did?


--
 
D

David Mark

David Mark said:











You believe that, because a person says he understands your
solution, that he must actually "obviously" understand it?
Clearly you've never been involved in any sort of user support.

Clearly you are more interested in me than in helping the OP, who has
long-since "signed off" on the solution.
Your explanation is incomplete in that it doesn't clearly
specify that, in addition to multiplying each of the addends
before the addition, that you also multiply the value you're
using for comparison. As I said before, "multiply each ...
before adding" leads one to believe that you are talking only
about the numbers that you are adding.

We're done talking here.
 
D

David Mark

You can count. I am impressed. You just cant add.

Oddly enough, the original equation posted still balances. Go back
and check it again if you think that has changed.
Word it how you want it. Your code, as originally posted, gave an

Word your own posts. And I posted no code. Re-read the post.
incorrect answer. You got corrected. Take it like a man and move on
sonny boy.



<quote cite="David Mark in this post">
How much more clear could the example be?
</quote>

You wanted to know how to make the example clearer, I showed you. You

Actually, you showed how to make it wrong by a factor of ten.
don't like it. I don't care if you like it or not.

We're done talking here.
 
D

Dr J R Stockton

In comp.lang.javascript message <1185222909.174869.169220@r34g2000hsd.
googlegroups.com>, Mon, 23 Jul 2007 20:35:09, (e-mail address removed) posted:
Is there a good solution for floating-point errors?
If I got:
0.00831 + 0.000001 = 0.00831109999999999999
can I avoid this by a better method than comparing 2 number length
after decimal point and using toFixed() to match shorter number with
another one?

Before considering what merit previous responses may have, you should
try to understand exactly what is happening.

In the machine, 0.00831 will not be held exactly; I think it is held as
the bit pattern represented in Hex by 3f81 04d5 51d6 8c69 whose
exact value is +0.00830999999999999967859043437101718154735863208770751953125
and 0.000001 is held as 3eb0 c6f7 a0b5 ed8d with exact value
+0.000000999999999999999954748111825886258685613938723690807819366455078125

If they are added, one gets 3f81 055b 8993 9218 with exact value
+0.00831099999999999894395585897655109874904155731201171875 .

Only when that hex value is converted to String does one get
"0.008310999999999999" .

FAQ 4.7 <URL:http://www.merlyn.demon.co.uk/js-maths.htm> and
<URL:http://www.merlyn.demon.co.uk/js-misc1.htm> refer; FAQ 4.6 may
help; see their references too.

It's a good idea to read the newsgroup c.l.j and its FAQ. See below.
 
V

vunet.us

[snip]


You can count. I am impressed. You just cant add.

Oddly enough, the original equation posted still balances. Go back
and check it again if you think that has changed.


Word it how you want it. Your code, as originally posted, gave an

Word your own posts. And I posted no code. Re-read the post.
incorrect answer. You got corrected. Take it like a man and move on
sonny boy.
<quote cite="David Mark in this post">
How much more clear could the example be?
</quote>
You wanted to know how to make the example clearer, I showed you. You

Actually, you showed how to make it wrong by a factor of ten.
don't like it. I don't care if you like it or not.

We're done talking here.

wow. i am lucky enough to get at least one answer out of 18 replies.
thanks.
PS: should I also add links to my websites in my signature?
 
L

Lee

(e-mail address removed) said:
wow. i am lucky enough to get at least one answer out of 18 replies.

I only see one reply to your question, not 18. The other
posts to this thread were discussing that answer. That's
very normal and desirable. One of the strengths of this
newsgroup is the relative assurance that debatable answers
will be challenged and clarified and/or improved upon.
It doesn't cost you anything, and provides more information
that you can use in deciding whether or not to accept the
suggested solution.
PS: should I also add links to my websites in my signature?

As a general rule, if you're in the position of asking for
help, you should avoid acting like a jerk. I don't know
why so many people seem to have trouble with that concept.


--
 
T

The Natural Philosopher

Lee said:
(e-mail address removed) said:


I only see one reply to your question, not 18. The other
posts to this thread were discussing that answer. That's
very normal and desirable. One of the strengths of this
newsgroup is the relative assurance that debatable answers
will be challenged and clarified and/or improved upon.
It doesn't cost you anything, and provides more information
that you can use in deciding whether or not to accept the
suggested solution.


As a general rule, if you're in the position of asking for
help, you should avoid acting like a jerk. I don't know
why so many people seem to have trouble with that concept.
If you aren't a jerk, you probably know the right answer..

Judging by appearance, a visitor from Mars might think that cricket
balls were as edible as apples.

Perhaps, to Martians, they are.

Its a natural human trait to think that your problams are the hard ones,
and stuff you have known since knee high is 'whet everybody (except
jerks)' knows..

Exposure to slightly wider cultural experience will in time disabuse you
of this notion..

It is interesting to note that the following response are *not answers*
to the simple question..e.g.

"How do I get Apache 2.0 working with RedHat 6.0?"

"Upgrade to Fedora core 27 or whatever"
"I'd try Slackware if I were you"
"*There is no compiled suite of Apache 2.0 for Redhat 6.0"
"Only a dork would want to put either of those on a decent computer"
"Apache 2.0 is more full of security holes than Redhat 6.0"
"With a brain like yours, perhaps you had better stick to Windows Vista"
"I don't know., but I wrote my own web server on DOS 2.1 using PHP"
"** why exactly would you want to do this?"
And so on..

*at least this one leads to the most likely correct answer

"You will have to compile the sources and due to the obsolescent nature
of it all, you will largely be on your own"

** and this one at least is a POLITE enquiry as to the context of the
question, rather than an insulting put-down..
 
L

Lee

The Natural Philosopher said:
If you aren't a jerk, you probably know the right answer..

Judging by appearance, a visitor from Mars might think that cricket
balls were as edible as apples.

Perhaps, to Martians, they are.

Its a natural human trait to think that your problams are the hard ones,
and stuff you have known since knee high is 'whet everybody (except
jerks)' knows..

You seem to have misunderstood my point about being a jerk.
The OP made a lame attempt to insult Randy. That's being
a jerk on several levels.

It is interesting to note that the following response are *not answers*
to the simple question..e.g.

"How do I get Apache 2.0 working with RedHat 6.0?"

"Upgrade to Fedora core 27 or whatever"
"I'd try Slackware if I were you"

They are not answers to the question, but they may be very
valuable responses to it.

A direct answer to the question "How do I pull the trigger
on my shotgun when the barrel us under my chin" is probably
not the most valuable response.


--
 
V

vunet.us

The Natural Philosopher said:










You seem to have misunderstood my point about being a jerk.
The OP made a lame attempt to insult Randy. That's being
a jerk on several levels.




They are not answers to the question, but they may be very
valuable responses to it.

A direct answer to the question "How do I pull the trigger
on my shotgun when the barrel us under my chin" is probably
not the most valuable response.

--

Lee, I see only one person acting as a jerk. It is you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top