# Solution for Floating-Point Errors

Discussion in 'Javascript' started by vunet.us@gmail.com, Jul 23, 2007.

1. ### Guest

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.

, Jul 23, 2007

2. ### David MarkGuest

On Jul 23, 4:35 pm, wrote:
> 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

David Mark, Jul 23, 2007

3. ### Guest

On Jul 23, 5:28 pm, David Mark <> wrote:
> On Jul 23, 4:35 pm, wrote:
>
> > 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

very good. thank you.

, Jul 23, 2007
4. ### David MarkGuest

On Jul 23, 6:08 pm, Randy Webb <> wrote:
> David Mark said the following on 7/23/2007 5:28 PM:
>
> > On Jul 23, 4:35 pm, wrote:
> >> 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

>
> 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.

David Mark, Jul 23, 2007
5. ### LeeGuest

David Mark said:
>
>On Jul 23, 6:08 pm, Randy Webb <> wrote:
>> David Mark said the following on 7/23/2007 5:28 PM:
>>
>> > On Jul 23, 4:35 pm, wrote:
>> >> 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

>>
>> 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.

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
so simple, doesn't work when implemented.

--

Lee, Jul 23, 2007
6. ### Guest

> 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

, Jul 23, 2007
7. ### David MarkGuest

On Jul 23, 6:31 pm, Lee <> wrote:
> David Mark said:
>
>
>
>
>
>
>
> >On Jul 23, 6:08 pm, Randy Webb <> wrote:
> >> David Mark said the following on 7/23/2007 5:28 PM:

>
> >> > On Jul 23, 4:35 pm, wrote:
> >> >> 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

>
> >> 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.

>
> 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

> so simple, doesn't work when implemented.

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

David Mark, Jul 24, 2007
8. ### David MarkGuest

On Jul 23, 7:35 pm, Randy Webb <> wrote:
> David Mark said the following on 7/23/2007 7:24 PM:
>
>
>
>
>
> > On Jul 23, 6:31 pm, Lee <> wrote:
> >> David Mark said:

>
> >>> On Jul 23, 6:08 pm, Randy Webb <> wrote:
> >>>> David Mark said the following on 7/23/2007 5:28 PM:
> >>>>> On Jul 23, 4:35 pm, wrote:
> >>>>>> 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
> >>>> 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.
> >> 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.

>
>
> 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

> 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.

David Mark, Jul 24, 2007
9. ### LeeGuest

David Mark said:
>
>On Jul 23, 6:31 pm, Lee <> wrote:
>> David Mark said:
>>
>>
>>
>>
>>
>>
>>
>> >On Jul 23, 6:08 pm, Randy Webb <> wrote:
>> >> David Mark said the following on 7/23/2007 5:28 PM:

>>
>> >> > On Jul 23, 4:35 pm, wrote:
>> >> >> 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

>>
>> >> 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.

>>
>> 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

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
before the addition, that you also multiply the value you're
using for comparison. As I said before, "multiply each ...

>
>
>> so simple, doesn't work when implemented.

>
>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.

--

Lee, Jul 24, 2007
10. ### LeeGuest

said:
>
>
>> 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

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?

--

Lee, Jul 24, 2007
11. ### David MarkGuest

On Jul 23, 8:22 pm, Lee <> wrote:
> David Mark said:
>
>
>
>
>
>
>
> >On Jul 23, 6:31 pm, Lee <> wrote:
> >> David Mark said:

>
> >> >On Jul 23, 6:08 pm, Randy Webb <> wrote:
> >> >> David Mark said the following on 7/23/2007 5:28 PM:

>
> >> >> > On Jul 23, 4:35 pm, wrote:
> >> >> >> 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

>
> >> >> 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.

>
> >> 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.

>
> 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
> 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

We're done talking here.

David Mark, Jul 24, 2007
12. ### LeeGuest

David Mark said:
>
>On Jul 23, 8:22 pm, Lee <> wrote:
>> David Mark said:
>>
>>
>>
>>
>>
>>
>>
>> >On Jul 23, 6:31 pm, Lee <> wrote:
>> >> David Mark said:

>>
>> >> >On Jul 23, 6:08 pm, Randy Webb <> wrote:
>> >> >> David Mark said the following on 7/23/2007 5:28 PM:

>>
>> >> >> > On Jul 23, 4:35 pm, wrote:
>> >> >> >> 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

>>
>> >> >> 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.

>>
>> >> 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.

>>
>> 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.

Welcome to USENET.

--

Lee, Jul 24, 2007
13. ### David MarkGuest

On Jul 23, 8:44 pm, Randy Webb <> wrote:
[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.

>
> > And what the hell does "get the decimal back" mean anyway? Same for
> > "multiply both of your decimals."

>
> 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.
>
> >> You wanted more clear, you got it.

>
> > I didn't want anything.

>
> <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.

David Mark, Jul 24, 2007
14. ### Dr J R StocktonGuest

In comp.lang.javascript message <1185222909.174869.169220@r34g2000hsd.
googlegroups.com>, Mon, 23 Jul 2007 20:35:09, 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.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.

Dr J R Stockton, Jul 24, 2007
15. ### Guest

On Jul 23, 9:31 pm, David Mark <> wrote:
> On Jul 23, 8:44 pm, Randy Webb <> wrote:
> [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.
>
>
>
> > > And what the hell does "get the decimal back" mean anyway? Same for
> > > "multiply both of your decimals."

>
> > 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.

>
> > >> You wanted more clear, you got it.

>
> > > I didn't want anything.

>
> > <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?

, Jul 24, 2007
16. ### LeeGuest

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
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.
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.

--

Lee, Jul 24, 2007
17. ### The Natural PhilosopherGuest

Lee wrote:
> 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
> 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.
> 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.
>
>

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..

"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..

The Natural Philosopher, Jul 24, 2007
18. ### LeeGuest

The Natural Philosopher said:
>
>Lee wrote:
>> 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
>> 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.
>> 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.
>>
>>

>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.

--

Lee, Jul 24, 2007
19. ### Guest

On Jul 24, 11:00 am, Lee <> wrote:
> The Natural Philosopher said:
>
>
>
>
>
> >Lee wrote:
> >> 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
> >> 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.
> >> 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.

>
> >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.
>
> --

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

, Jul 24, 2007
20. ### Dr J R StocktonGuest

In comp.lang.javascript message <>,
Mon, 23 Jul 2007 23:17:09, Randy Webb <> posted:

>
>Besides, don't make promises you don't intend to keep.
>

And keep the ones that you do make.

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

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.