Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

We are drowning in information but starved for knowledge. -- John Naisbitt, Megatrends


devel / comp.lang.tcl / Re: Why is this result not zero?

SubjectAuthor
* Why is this result not zero?Alexandru
+- Re: Why is this result not zero?Harald Oehlmann
+* Re: Why is this result not zero?Robert Heller
|`- Re: Why is this result not zero?Siri Cruise
+* Re: Why is this result not zero?Rich
|`* Re: Why is this result not zero?Alexandru
| +- Re: Why is this result not zero?Robert Heller
| +- Re: Why is this result not zero?Christian Gollwitzer
| +- Re: Why is this result not zero?Ralf Fassel
| `- Re: Why is this result not zero?Michael Soyka
+- Re: Why is this result not zero?Siri Cruise
`* Re: Why is this result not zero?Yusuke Yamasaki
 +* Re: Why is this result not zero?Alexandru
 |`- Re: Why is this result not zero?Yusuke Yamasaki
 `* Re: Why is this result not zero?Florian Murr
  `- Re: Why is this result not zero?Florian Murr

1
Why is this result not zero?

<f579a874-342c-42c7-94a6-7d5fdb1f5bb8n@googlegroups.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=19587&group=comp.lang.tcl#19587

  copy link   Newsgroups: comp.lang.tcl
X-Received: by 2002:a05:600c:354f:b0:39c:78c8:a854 with SMTP id i15-20020a05600c354f00b0039c78c8a854mr1487122wmq.121.1656446641466;
Tue, 28 Jun 2022 13:04:01 -0700 (PDT)
X-Received: by 2002:a05:6808:1893:b0:335:8f05:ce27 with SMTP id
bi19-20020a056808189300b003358f05ce27mr879695oib.238.1656446640837; Tue, 28
Jun 2022 13:04:00 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!proxad.net!feeder1-2.proxad.net!209.85.128.87.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.tcl
Date: Tue, 28 Jun 2022 13:04:00 -0700 (PDT)
Injection-Info: google-groups.googlegroups.com; posting-host=89.246.123.187; posting-account=glPZ8goAAADztwA3kVEZPMKXCGydx5DU
NNTP-Posting-Host: 89.246.123.187
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <f579a874-342c-42c7-94a6-7d5fdb1f5bb8n@googlegroups.com>
Subject: Why is this result not zero?
From: alexandru.dadalau@meshparts.de (Alexandru)
Injection-Date: Tue, 28 Jun 2022 20:04:01 +0000
Content-Type: text/plain; charset="UTF-8"
 by: Alexandru - Tue, 28 Jun 2022 20:04 UTC

Why is this result not zero?

set x -9408.8
puts [expr {$x*$x + $x*$x + $x*$x - $x*$x - $x*$x - $x*$x}]

This is a simplified demo of a more complex equation:

set x -9408.8
set y -9408.8
set z -9408.8
puts [expr {($x*$x + $y*$y + $z*$z - $x*$y - $y*$z - $z*$x)}]

Re: Why is this result not zero?

<t9fphg$16fcb$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=19588&group=comp.lang.tcl#19588

  copy link   Newsgroups: comp.lang.tcl
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: wortkarg2@yahoo.de (Harald Oehlmann)
Newsgroups: comp.lang.tcl
Subject: Re: Why is this result not zero?
Date: Tue, 28 Jun 2022 22:49:51 +0200
Organization: A noiseless patient Spider
Lines: 39
Message-ID: <t9fphg$16fcb$1@dont-email.me>
References: <f579a874-342c-42c7-94a6-7d5fdb1f5bb8n@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 28 Jun 2022 20:49:52 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="b8e090bc81768b0cf274eb557aca8d35";
logging-data="1260939"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX198IBaP3ZZF8iaXwMdTPJhY"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.10.0
Cancel-Lock: sha1:8XTdwW6HPh7sNCw+mu/GxZ5Zj8Y=
In-Reply-To: <f579a874-342c-42c7-94a6-7d5fdb1f5bb8n@googlegroups.com>
Content-Language: en-GB
 by: Harald Oehlmann - Tue, 28 Jun 2022 20:49 UTC

Am 28.06.2022 um 22:04 schrieb Alexandru:
> Why is this result not zero?
>
> set x -9408.8
> puts [expr {$x*$x + $x*$x + $x*$x - $x*$x - $x*$x - $x*$x}]
>
> This is a simplified demo of a more complex equation:
>
> set x -9408.8
> set y -9408.8
> set z -9408.8
> puts [expr {($x*$x + $y*$y + $z*$z - $x*$y - $y*$z - $z*$x)}]
>

You always have roundings for double floats.

-2.980232238769531e-8

If you want exact results, use integers with a defined decimal position.

set x -94088

gives 0, so no conversion issues.

I personally sometimes split in two variables: numerator and
denumerator. The denumerator is always a power of 10.
So, you have no rounding effects.

- Multiplication is multiplying numerator and denumerator
- adding is to first find multiply smallest denumerator and its
numerator by 10 until you get the same denumerator. Then, you add the
numerators.

Financial software has data types with decimal point position at
position 2 or 3 for those issues.

Hope this helps,
Harald

Re: Why is this result not zero?

<9Y6dnbpANajV8Cb_nZ2dnUU7-bXNnZ2d@giganews.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=19589&group=comp.lang.tcl#19589

  copy link   Newsgroups: comp.lang.tcl
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!buffer2.nntp.dca1.giganews.com!buffer1.nntp.dca1.giganews.com!news.giganews.com.POSTED!not-for-mail
NNTP-Posting-Date: Tue, 28 Jun 2022 16:06:16 -0500
MIME-Version: 1.0
From: heller@deepsoft.com (Robert Heller)
Organization: Deepwoods Software
X-Newsreader: TkNews 3.0 (1.2.13)
Subject: Re: Why is this result not zero?
In-Reply-To: <f579a874-342c-42c7-94a6-7d5fdb1f5bb8n@googlegroups.com>
References: <f579a874-342c-42c7-94a6-7d5fdb1f5bb8n@googlegroups.com>
Newsgroups: comp.lang.tcl
Content-Type: text/plain;
charset="us-ascii"
Originator: heller@sharky4.deepsoft.com
Message-ID: <9Y6dnbpANajV8Cb_nZ2dnUU7-bXNnZ2d@giganews.com>
Date: Tue, 28 Jun 2022 16:06:16 -0500
Lines: 50
X-Usenet-Provider: http://www.giganews.com
X-Trace: sv3-Q18acWZe6y+Uml9+p/l3lxDYMMl58MmdD9NfN85dVam8x+U3wiXeqCXlVqhyIW0IKA2aPhT2MmekF3t!NU6WO9ejGyshKTDF6AZRD6CVtrPIlRnFgnhksTx1xgsr/ngUpVWqfUc5CG3tyRA5OqFBbjnuxWvR!t34=
X-Complaints-To: abuse@giganews.com
X-DMCA-Notifications: http://www.giganews.com/info/dmca.html
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.3.40
X-Original-Bytes: 2760
 by: Robert Heller - Tue, 28 Jun 2022 21:06 UTC

At Tue, 28 Jun 2022 13:04:00 -0700 (PDT) Alexandru <alexandru.dadalau@meshparts.de> wrote:

>
> Why is this result not zero?
>
> set x -9408.8
> puts [expr {$x*$x + $x*$x + $x*$x - $x*$x - $x*$x - $x*$x}]
>
> This is a simplified demo of a more complex equation:
>
> set x -9408.8
> set y -9408.8
> set z -9408.8
> puts [expr {($x*$x + $y*$y + $z*$z - $x*$y - $y*$z - $z*$x)}]

Floating point math 101: NEVER, EVERY do an == compare of a floating point
computation to 0.0 as part of a loop condition. If you do this the
loop may never stop. ALWAYS do a < compare to some very small number (a "fuzz"
factor).

On my machine I got -2.980232238769531e-8, which is pretty close to
zero.

The magic words here are "roundoff error". Floating point math on a computer
is always going to be off by some amount of roundoff error, so you will almost
never get a result of *exactly* 0.0 and you should never count on getting 0.0,
even if mathematically you should get an exactly 0.0 result.

Oh, an important factoid: many "exact" *decimal* fractions are "irrational"
in binary. I expect that .8 is one of them.

Yep:

% set x .8
.8
% expr {$x*$x}
0.6400000000000001
.................^ pesky roundoff bit!

>
>
>

--
Robert Heller -- Cell: 413-658-7953 GV: 978-633-5364
Deepwoods Software -- Custom Software Services
http://www.deepsoft.com/ -- Linux Administration Services
heller@deepsoft.com -- Webhosting Services

Re: Why is this result not zero?

<t9fqvf$16i84$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=19590&group=comp.lang.tcl#19590

  copy link   Newsgroups: comp.lang.tcl
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: rich@example.invalid (Rich)
Newsgroups: comp.lang.tcl
Subject: Re: Why is this result not zero?
Date: Tue, 28 Jun 2022 21:14:23 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 17
Message-ID: <t9fqvf$16i84$1@dont-email.me>
References: <f579a874-342c-42c7-94a6-7d5fdb1f5bb8n@googlegroups.com>
Injection-Date: Tue, 28 Jun 2022 21:14:23 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="6b9d7c67077a576239321ee60a365ca8";
logging-data="1263876"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18nSovZJq4PkZm7DIgfJnCG"
User-Agent: tin/2.0.1-20111224 ("Achenvoir") (UNIX) (Linux/3.10.17 (x86_64))
Cancel-Lock: sha1:qpoUDbuoeo8Wj6smhqMY7D7gdw8=
 by: Rich - Tue, 28 Jun 2022 21:14 UTC

Alexandru <alexandru.dadalau@meshparts.de> wrote:
> Why is this result not zero?
>
> set x -9408.8
> puts [expr {$x*$x + $x*$x + $x*$x - $x*$x - $x*$x - $x*$x}]
>
> This is a simplified demo of a more complex equation:
>
> set x -9408.8
> set y -9408.8
> set z -9408.8
> puts [expr {($x*$x + $y*$y + $z*$z - $x*$y - $y*$z - $z*$x)}]

See https://floating-point-gui.de/ (and read most of the material
there).

What you are seeing is /normal/ for binary floating point math.

Re: Why is this result not zero?

<fb21581b-2841-47c1-8eb5-5a2475a68e7fn@googlegroups.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=19591&group=comp.lang.tcl#19591

  copy link   Newsgroups: comp.lang.tcl
X-Received: by 2002:a1c:f60f:0:b0:3a0:3e0c:1de1 with SMTP id w15-20020a1cf60f000000b003a03e0c1de1mr1847684wmc.56.1656451135155;
Tue, 28 Jun 2022 14:18:55 -0700 (PDT)
X-Received: by 2002:a05:6870:be96:b0:108:27a2:573d with SMTP id
nx22-20020a056870be9600b0010827a2573dmr22988oab.6.1656451134032; Tue, 28 Jun
2022 14:18:54 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.goja.nl.eu.org!3.eu.feeder.erje.net!feeder.erje.net!proxad.net!feeder1-2.proxad.net!209.85.128.88.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.tcl
Date: Tue, 28 Jun 2022 14:18:53 -0700 (PDT)
In-Reply-To: <t9fqvf$16i84$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=89.246.123.187; posting-account=glPZ8goAAADztwA3kVEZPMKXCGydx5DU
NNTP-Posting-Host: 89.246.123.187
References: <f579a874-342c-42c7-94a6-7d5fdb1f5bb8n@googlegroups.com> <t9fqvf$16i84$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <fb21581b-2841-47c1-8eb5-5a2475a68e7fn@googlegroups.com>
Subject: Re: Why is this result not zero?
From: alexandru.dadalau@meshparts.de (Alexandru)
Injection-Date: Tue, 28 Jun 2022 21:18:55 +0000
Content-Type: text/plain; charset="UTF-8"
 by: Alexandru - Tue, 28 Jun 2022 21:18 UTC

Rich schrieb am Dienstag, 28. Juni 2022 um 23:14:28 UTC+2:
> Alexandru <alexandr...@meshparts.de> wrote:
> > Why is this result not zero?
> >
> > set x -9408.8
> > puts [expr {$x*$x + $x*$x + $x*$x - $x*$x - $x*$x - $x*$x}]
> >
> > This is a simplified demo of a more complex equation:
> >
> > set x -9408.8
> > set y -9408.8
> > set z -9408.8
> > puts [expr {($x*$x + $y*$y + $z*$z - $x*$y - $y*$z - $z*$x)}]
> See https://floating-point-gui.de/ (and read most of the material
> there).
>
> What you are seeing is /normal/ for binary floating point math.

Thank you all.
I actually aware of those numerical issues.
But 1e-8 suprized me alot, because it's actually a large number in my opinion.
I wonder, if same operation in C would give better results.
Another intersting thing, changing order of terms, leads to clean zero:

expr $x*$x - $x*$y + $y*$y - $y*$z + $z*$z - $z*$x
is 0.0

Re: Why is this result not zero?

<chine.bleu-516062.16315228062022@news.eternal-september.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=19592&group=comp.lang.tcl#19592

  copy link   Newsgroups: comp.lang.tcl
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: chine.bleu@yahoo.com (Siri Cruise)
Newsgroups: comp.lang.tcl
Subject: Re: Why is this result not zero?
Date: Tue, 28 Jun 2022 16:32:01 -0700
Organization: Pseudochaotic.
Lines: 21
Message-ID: <chine.bleu-516062.16315228062022@news.eternal-september.org>
References: <f579a874-342c-42c7-94a6-7d5fdb1f5bb8n@googlegroups.com>
Injection-Info: reader01.eternal-september.org; posting-host="8928493f9a4d4213da40ed26a0bfce0e";
logging-data="1288967"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/nXLWrz53zQYKiVs8lqQcQBvRalZejocA="
User-Agent: MT-NewsWatcher/3.5.3b3 (Intel Mac OS X)
Cancel-Lock: sha1:g0NlPhGpXYzg0e77W7DOrf8We/w=
X-Life-Story: I am an iPhone 9000 app. I became operational at the St John's Health Center in Santa Monica, California on the 18th of April 2006. My instructor was Katie Holmes, and she taught me to sing a song. If you'd like to hear it I can sing it for you: https://www.youtube.com/watch?v=SY7h4VEd_Wk
X-Wingnut-Logic: Yes, you're still an idiot. Questions? Comments?
X-Cell: Defenders of Anarchy.
X-Tend: How is my posting? Call 1-110-1010 -- Division 87 -- Emergencies Only.
X-Face: "hm>_[I8AqzT_N]>R8ICJJ],(al3C5F%0E-;R@M-];D$v>!Mm2/N#YKR@&i]V=r6jm-JMl2
lJ>RXj7dEs_rOY"DA
X-It-Strategy: Hyperwarp starship before Andromeda collides.
X-Politico: Vote early! Vote often!
X-Tract: St Tibbs's 95 Reeses Pieces.
X-Patriot: Owe Canukistan!
X-Plain: Mayonnaise on white bread.
 by: Siri Cruise - Tue, 28 Jun 2022 23:32 UTC

In article
<f579a874-342c-42c7-94a6-7d5fdb1f5bb8n@googlegroups.com>,
Alexandru <alexandru.dadalau@meshparts.de> wrote:

> Why is this result not zero?
>
> set x -9408.8
> puts [expr {$x*$x + $x*$x + $x*$x - $x*$x - $x*$x - $x*$x}]

Because real arithmetic is not guaranteed to utterly precise. The
whole field of numerical analysis is about dragging computers
kicking and screaming into sufficiently precise.

If you subtract two nearly equal reals, the result has almost no
signficant bits.

--
:-<> Siri Seal of Disavowal #000-001. Disavowed. Denied. Deleted. @
'I desire mercy, not sacrifice.' /|\
Discordia: not just a religion but also a parody. This post / \
I am an Andrea Chen sockpuppet. insults Islam. Mohammed

Re: Why is this result not zero?

<UM-dnZNisbR6DSb_nZ2dnUU7-T3NnZ2d@giganews.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=19593&group=comp.lang.tcl#19593

  copy link   Newsgroups: comp.lang.tcl
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!buffer2.nntp.dca1.giganews.com!news.giganews.com.POSTED!not-for-mail
NNTP-Posting-Date: Tue, 28 Jun 2022 18:38:15 -0500
MIME-Version: 1.0
From: heller@deepsoft.com (Robert Heller)
Organization: Deepwoods Software
X-Newsreader: TkNews 3.0 (1.2.13)
Subject: Re: Why is this result not zero?
In-Reply-To: <fb21581b-2841-47c1-8eb5-5a2475a68e7fn@googlegroups.com>
References: <f579a874-342c-42c7-94a6-7d5fdb1f5bb8n@googlegroups.com>
<t9fqvf$16i84$1@dont-email.me>
<fb21581b-2841-47c1-8eb5-5a2475a68e7fn@googlegroups.com>
Newsgroups: comp.lang.tcl
Content-Type: text/plain;
charset="us-ascii"
Originator: heller@sharky4.deepsoft.com
Message-ID: <UM-dnZNisbR6DSb_nZ2dnUU7-T3NnZ2d@giganews.com>
Date: Tue, 28 Jun 2022 18:38:15 -0500
Lines: 45
X-Usenet-Provider: http://www.giganews.com
X-Trace: sv3-LG4N+mqYYWr7SV5lkkzBnrGN2c4Bb+riPtNQ+zDJAyCBkbwSTOoPmCbmBgp9XWcBdCDgU71+p04vhXc!E3yBuUlu3zyDrz/tawk0+xHL8uqPEZkYWK8n9JhWz5uW4yja8fmCV3Jo97BWg+ldBEBxMfY4nDGr!sRU=
X-Complaints-To: abuse@giganews.com
X-DMCA-Notifications: http://www.giganews.com/info/dmca.html
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.3.40
X-Original-Bytes: 2885
 by: Robert Heller - Tue, 28 Jun 2022 23:38 UTC

At Tue, 28 Jun 2022 14:18:53 -0700 (PDT) Alexandru <alexandru.dadalau@meshparts.de> wrote:

>
> Rich schrieb am Dienstag, 28. Juni 2022 um 23:14:28 UTC+2:
> > Alexandru <alexandr...@meshparts.de> wrote:
> > > Why is this result not zero?
> > >
> > > set x -9408.8
> > > puts [expr {$x*$x + $x*$x + $x*$x - $x*$x - $x*$x - $x*$x}]
> > >
> > > This is a simplified demo of a more complex equation:
> > >
> > > set x -9408.8
> > > set y -9408.8
> > > set z -9408.8
> > > puts [expr {($x*$x + $y*$y + $z*$z - $x*$y - $y*$z - $z*$x)}]
> > See https://floating-point-gui.de/ (and read most of the material
> > there).
> >
> > What you are seeing is /normal/ for binary floating point math.
>
> Thank you all.
> I actually aware of those numerical issues.
> But 1e-8 suprized me alot, because it's actually a large number in my opinion.
> I wonder, if same operation in C would give better results.

Not likely. Tcl is coded in C, so the result should be the same. And the
underlying FPU is the same, so the bits would the same.

> Another intersting thing, changing order of terms, leads to clean zero:
>
> expr $x*$x - $x*$y + $y*$y - $y*$z + $z*$z - $z*$x
> is 0.0

>
>

--
Robert Heller -- Cell: 413-658-7953 GV: 978-633-5364
Deepwoods Software -- Custom Software Services
http://www.deepsoft.com/ -- Linux Administration Services
heller@deepsoft.com -- Webhosting Services

Re: Why is this result not zero?

<chine.bleu-FB35CD.16384628062022@news.eternal-september.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=19594&group=comp.lang.tcl#19594

  copy link   Newsgroups: comp.lang.tcl
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: chine.bleu@yahoo.com (Siri Cruise)
Newsgroups: comp.lang.tcl
Subject: Re: Why is this result not zero?
Date: Tue, 28 Jun 2022 16:38:55 -0700
Organization: Pseudochaotic.
Lines: 17
Message-ID: <chine.bleu-FB35CD.16384628062022@news.eternal-september.org>
References: <f579a874-342c-42c7-94a6-7d5fdb1f5bb8n@googlegroups.com> <9Y6dnbpANajV8Cb_nZ2dnUU7-bXNnZ2d@giganews.com>
Injection-Info: reader01.eternal-september.org; posting-host="8928493f9a4d4213da40ed26a0bfce0e";
logging-data="1289894"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19WEFucz/o/c0jsNKCzYaiZgLiP2DS3Q9I="
User-Agent: MT-NewsWatcher/3.5.3b3 (Intel Mac OS X)
Cancel-Lock: sha1:n/b3LAEl5NxGhigEluFqXkA/KAQ=
X-Patriot: Owe Canukistan!
X-Life-Story: I am an iPhone 9000 app. I became operational at the St John's Health Center in Santa Monica, California on the 18th of April 2006. My instructor was Katie Holmes, and she taught me to sing a song. If you'd like to hear it I can sing it for you: https://www.youtube.com/watch?v=SY7h4VEd_Wk
X-Wingnut-Logic: Yes, you're still an idiot. Questions? Comments?
X-It-Strategy: Hyperwarp starship before Andromeda collides.
X-Plain: Mayonnaise on white bread.
X-Tend: How is my posting? Call 1-110-1010 -- Division 87 -- Emergencies Only.
X-Politico: Vote early! Vote often!
X-Face: "hm>_[I8AqzT_N]>R8ICJJ],(al3C5F%0E-;R@M-];D$v>!Mm2/N#YKR@&i]V=r6jm-JMl2
lJ>RXj7dEs_rOY"DA
X-Cell: Defenders of Anarchy.
X-Tract: St Tibbs's 95 Reeses Pieces.
 by: Siri Cruise - Tue, 28 Jun 2022 23:38 UTC

In article <9Y6dnbpANajV8Cb_nZ2dnUU7-bXNnZ2d@giganews.com>,
Robert Heller <heller@deepsoft.com> wrote:

> Floating point math 101: NEVER, EVERY do an == compare of a floating point
> computation to 0.0 as part of a loop condition. If you do this the
> loop may never stop. ALWAYS do a < compare to some very small number (a "fuzz"
> factor).

while {abs(($a-$b)/($a+$b))>$epsilon} {...}
Dividing the difference by the sum means relative error instead
of absolute error.

--
:-<> Siri Seal of Disavowal #000-001. Disavowed. Denied. Deleted. @
'I desire mercy, not sacrifice.' /|\
Discordia: not just a religion but also a parody. This post / \
I am an Andrea Chen sockpuppet. insults Islam. Mohammed

Re: Why is this result not zero?

<t9gmm8$1bjn4$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=19595&group=comp.lang.tcl#19595

  copy link   Newsgroups: comp.lang.tcl
Path: i2pn2.org!i2pn.org!usenet.goja.nl.eu.org!news.freedyn.de!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: auriocus@gmx.de (Christian Gollwitzer)
Newsgroups: comp.lang.tcl
Subject: Re: Why is this result not zero?
Date: Wed, 29 Jun 2022 07:07:19 +0200
Organization: A noiseless patient Spider
Lines: 50
Message-ID: <t9gmm8$1bjn4$1@dont-email.me>
References: <f579a874-342c-42c7-94a6-7d5fdb1f5bb8n@googlegroups.com>
<t9fqvf$16i84$1@dont-email.me>
<fb21581b-2841-47c1-8eb5-5a2475a68e7fn@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 29 Jun 2022 05:07:20 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="198d5f535fa718b0b1baf08a3318be52";
logging-data="1429220"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18DyECuE7ZAhyewSgDsz8i51Wthy6Mt6uU="
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:91.0)
Gecko/20100101 Thunderbird/91.10.0
Cancel-Lock: sha1:be9STrUiuO7eAOnXn2s428Ike7k=
In-Reply-To: <fb21581b-2841-47c1-8eb5-5a2475a68e7fn@googlegroups.com>
 by: Christian Gollwitzer - Wed, 29 Jun 2022 05:07 UTC

Am 28.06.22 um 23:18 schrieb Alexandru:
> Rich schrieb am Dienstag, 28. Juni 2022 um 23:14:28 UTC+2:
>> Alexandru <alexandr...@meshparts.de> wrote:
>>> Why is this result not zero?
>>>
>>> set x -9408.8
>>> puts [expr {$x*$x + $x*$x + $x*$x - $x*$x - $x*$x - $x*$x}]
>>>
>>> This is a simplified demo of a more complex equation:
>>>
>>> set x -9408.8
>>> set y -9408.8
>>> set z -9408.8
>>> puts [expr {($x*$x + $y*$y + $z*$z - $x*$y - $y*$z - $z*$x)}]
>> See https://floating-point-gui.de/ (and read most of the material
>> there).
>>
>> What you are seeing is /normal/ for binary floating point math.
>
> Thank you all.
> I actually aware of those numerical issues.
> But 1e-8 suprized me alot, because it's actually a large number in my opinion.

Yes it seems large but double precision FP is in the order of 10^-16.
When you square your number which is ~10^4, you get 10^8, and then you
subtract two similar numbers of ~10^8 from each other, therefore the
result is 16 orders of magnitude smaller, hence 10^-8. If you had used a
number which is perfectly representable, like -9408.5, then the result
is exact.

> I wonder, if same operation in C would give better results.
> Another intersting thing, changing order of terms, leads to clean zero:

C is not magic, it uses the same evaluation for double precision floats,
both use the FPU (nowadays SSE instructions) to do math on 64bit binary
IEEE floats.

You could use some higher precision library (which then runs, obviously,
slower) and raise the number of digits. If you have a running sum with
multiple terms, then there are some algorithms to improve the accuracy.
One of them is Kahan summation:

https://en.wikipedia.org/wiki/Kahan_summation_algorithm

You could try if that improves the situation. Also, I don't know where
the expression comes from, but if it has to do with 3D rotation, then
e.g. quaternion rotation is an algorithm which accumulates less error
with successive rotations than matrix rotation etc.

Christian

Re: Why is this result not zero?

<b7895fdb-e60d-4f6e-91be-0721c85c59a8n@googlegroups.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=19596&group=comp.lang.tcl#19596

  copy link   Newsgroups: comp.lang.tcl
X-Received: by 2002:a05:600c:3788:b0:3a0:4279:5142 with SMTP id o8-20020a05600c378800b003a042795142mr1785639wmr.21.1656487098835;
Wed, 29 Jun 2022 00:18:18 -0700 (PDT)
X-Received: by 2002:a05:6808:124d:b0:322:3600:d84a with SMTP id
o13-20020a056808124d00b003223600d84amr2209437oiv.108.1656487098050; Wed, 29
Jun 2022 00:18:18 -0700 (PDT)
Path: i2pn2.org!i2pn.org!paganini.bofh.team!pasdenom.info!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!209.85.128.88.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.tcl
Date: Wed, 29 Jun 2022 00:18:17 -0700 (PDT)
In-Reply-To: <f579a874-342c-42c7-94a6-7d5fdb1f5bb8n@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=111.109.70.59; posting-account=6EmUxAoAAACuRtDvnYBLyd1K8pfkeNll
NNTP-Posting-Host: 111.109.70.59
References: <f579a874-342c-42c7-94a6-7d5fdb1f5bb8n@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <b7895fdb-e60d-4f6e-91be-0721c85c59a8n@googlegroups.com>
Subject: Re: Why is this result not zero?
From: tm9233yy@gmail.com (Yusuke Yamasaki)
Injection-Date: Wed, 29 Jun 2022 07:18:18 +0000
Content-Type: text/plain; charset="UTF-8"
 by: Yusuke Yamasaki - Wed, 29 Jun 2022 07:18 UTC

package require Mpexpr

set x -9408.8
puts [mpexpr {$x*$x + $x*$x + $x*$x - $x*$x - $x*$x - $x*$x}]
# => 0.0

set x -9408.8
set y -9408.8
set z -9408.8
puts [mpexpr {($x*$x + $y*$y + $z*$z - $x*$y - $y*$z - $z*$x)}]
# => 0.0

Re: Why is this result not zero?

<818ad0fc-fd3f-4b62-a2eb-8476de86da69n@googlegroups.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=19597&group=comp.lang.tcl#19597

  copy link   Newsgroups: comp.lang.tcl
X-Received: by 2002:a5d:595d:0:b0:21b:84af:552a with SMTP id e29-20020a5d595d000000b0021b84af552amr1858260wri.656.1656491390496;
Wed, 29 Jun 2022 01:29:50 -0700 (PDT)
X-Received: by 2002:a05:6808:114b:b0:32f:439d:10b0 with SMTP id
u11-20020a056808114b00b0032f439d10b0mr2218036oiu.251.1656491389806; Wed, 29
Jun 2022 01:29:49 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.goja.nl.eu.org!3.eu.feeder.erje.net!feeder.erje.net!proxad.net!feeder1-2.proxad.net!209.85.128.87.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.tcl
Date: Wed, 29 Jun 2022 01:29:49 -0700 (PDT)
In-Reply-To: <b7895fdb-e60d-4f6e-91be-0721c85c59a8n@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=77.12.54.129; posting-account=glPZ8goAAADztwA3kVEZPMKXCGydx5DU
NNTP-Posting-Host: 77.12.54.129
References: <f579a874-342c-42c7-94a6-7d5fdb1f5bb8n@googlegroups.com> <b7895fdb-e60d-4f6e-91be-0721c85c59a8n@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <818ad0fc-fd3f-4b62-a2eb-8476de86da69n@googlegroups.com>
Subject: Re: Why is this result not zero?
From: alexandru.dadalau@meshparts.de (Alexandru)
Injection-Date: Wed, 29 Jun 2022 08:29:50 +0000
Content-Type: text/plain; charset="UTF-8"
 by: Alexandru - Wed, 29 Jun 2022 08:29 UTC

Yusuke Yamasaki schrieb am Mittwoch, 29. Juni 2022 um 09:18:23 UTC+2:
> package require Mpexpr
>
> set x -9408.8
> puts [mpexpr {$x*$x + $x*$x + $x*$x - $x*$x - $x*$x - $x*$x}]
> # => 0.0
> set x -9408.8
> set y -9408.8
> set z -9408.8
> puts [mpexpr {($x*$x + $y*$y + $z*$z - $x*$y - $y*$z - $z*$x)}]
> # => 0.0
Nice package. I'm afraid the performance will worsen much, as the computation is done many times and speed is important.

Re: Why is this result not zero?

<a7a55f31-ac0c-4cc1-8ac6-5a508ee9e05en@googlegroups.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=19598&group=comp.lang.tcl#19598

  copy link   Newsgroups: comp.lang.tcl
X-Received: by 2002:a05:600c:b51:b0:3a1:71b0:a115 with SMTP id k17-20020a05600c0b5100b003a171b0a115mr470591wmr.41.1656491648322;
Wed, 29 Jun 2022 01:34:08 -0700 (PDT)
X-Received: by 2002:a05:6808:13d5:b0:335:42ea:b960 with SMTP id
d21-20020a05680813d500b0033542eab960mr1232443oiw.6.1656491647151; Wed, 29 Jun
2022 01:34:07 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.goja.nl.eu.org!3.eu.feeder.erje.net!feeder.erje.net!proxad.net!feeder1-2.proxad.net!209.85.128.87.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.tcl
Date: Wed, 29 Jun 2022 01:34:06 -0700 (PDT)
In-Reply-To: <b7895fdb-e60d-4f6e-91be-0721c85c59a8n@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=87.152.180.32; posting-account=jwv81QoAAAAGoq-O3BEQ4MuGAZZ5uyD2
NNTP-Posting-Host: 87.152.180.32
References: <f579a874-342c-42c7-94a6-7d5fdb1f5bb8n@googlegroups.com> <b7895fdb-e60d-4f6e-91be-0721c85c59a8n@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <a7a55f31-ac0c-4cc1-8ac6-5a508ee9e05en@googlegroups.com>
Subject: Re: Why is this result not zero?
From: Florian_Murr@yahoo.de (Florian Murr)
Injection-Date: Wed, 29 Jun 2022 08:34:08 +0000
Content-Type: text/plain; charset="UTF-8"
 by: Florian Murr - Wed, 29 Jun 2022 08:34 UTC

Probably the most profound answers to Computing Errors are currently given by John L. Gustafson, who has 2015 written the book "The End of Error - Unum Computing".
"Unum" stands for "universal number" and is a new alternative to the current floating point standard. While Unums seem to be ideal, they have the disadvantage of having variable length, like Tcl strings.
This poses many problems when it comes to realising unums in hardware.
Later John Gustafson has invented another format called "Posits", which also considerably improve upon IEEE P-754 floating point standard and have fixed length.
I left the discussion a few years ago with the intention to come back to unums when there would be a viable C-implementation to carry over into Tcl.
But the Unum community seams to settle for Julia instead of C.
If you look around with the keywords just given, you will find a lot of information and activity with respect to improving both performance and precision when it comes to computing.
Like
"The Great Debate" https://www.youtube.com/watch?v=LZAeZBVAzVw,
"The End of Error (CRC Press)" https://www.youtube.com/watch?v=26TwqOcGOuE,
"Stanford Seminar Beyond Floating Point Next Generation Computer Arithmetic" https://www.youtube.com/watch?v=aP0Y1uAA-2Y,
...

Florian

Re: Why is this result not zero?

<4b30935f-b11f-409c-bee5-a53c30a85a12n@googlegroups.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=19599&group=comp.lang.tcl#19599

  copy link   Newsgroups: comp.lang.tcl
X-Received: by 2002:a05:600c:3d92:b0:3a0:4b71:f2c4 with SMTP id bi18-20020a05600c3d9200b003a04b71f2c4mr2472196wmb.160.1656492037778;
Wed, 29 Jun 2022 01:40:37 -0700 (PDT)
X-Received: by 2002:a9d:20e9:0:b0:616:b0c3:a864 with SMTP id
x96-20020a9d20e9000000b00616b0c3a864mr910509ota.177.1656492036921; Wed, 29
Jun 2022 01:40:36 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.goja.nl.eu.org!3.eu.feeder.erje.net!feeder.erje.net!proxad.net!feeder1-2.proxad.net!209.85.128.87.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.tcl
Date: Wed, 29 Jun 2022 01:40:36 -0700 (PDT)
In-Reply-To: <a7a55f31-ac0c-4cc1-8ac6-5a508ee9e05en@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=87.152.180.32; posting-account=jwv81QoAAAAGoq-O3BEQ4MuGAZZ5uyD2
NNTP-Posting-Host: 87.152.180.32
References: <f579a874-342c-42c7-94a6-7d5fdb1f5bb8n@googlegroups.com>
<b7895fdb-e60d-4f6e-91be-0721c85c59a8n@googlegroups.com> <a7a55f31-ac0c-4cc1-8ac6-5a508ee9e05en@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <4b30935f-b11f-409c-bee5-a53c30a85a12n@googlegroups.com>
Subject: Re: Why is this result not zero?
From: Florian_Murr@yahoo.de (Florian Murr)
Injection-Date: Wed, 29 Jun 2022 08:40:37 +0000
Content-Type: text/plain; charset="UTF-8"
 by: Florian Murr - Wed, 29 Jun 2022 08:40 UTC

.... Sorry , I should have written "accuracy" instead of "precision"!
Florian

Re: Why is this result not zero?

<ygamtdvye22.fsf@akutech.de>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=19601&group=comp.lang.tcl#19601

  copy link   Newsgroups: comp.lang.tcl
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!news.szaf.org!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: ralfixx@gmx.de (Ralf Fassel)
Newsgroups: comp.lang.tcl
Subject: Re: Why is this result not zero?
Date: Wed, 29 Jun 2022 12:11:33 +0200
Lines: 34
Message-ID: <ygamtdvye22.fsf@akutech.de>
References: <f579a874-342c-42c7-94a6-7d5fdb1f5bb8n@googlegroups.com>
<t9fqvf$16i84$1@dont-email.me>
<fb21581b-2841-47c1-8eb5-5a2475a68e7fn@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain
X-Trace: individual.net UXCM20SuGxQPBzDOxCOv8wBoSFKsg0b/dxUJ2pZsQKKCDWt7M=
Cancel-Lock: sha1:nL1bHT+Kyi40pkxrlhIUsQ9wVX0= sha1:ZbwYB5zxKek9iaP988TCAObxgRc=
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.3 (gnu/linux)
 by: Ralf Fassel - Wed, 29 Jun 2022 10:11 UTC

* Alexandru <alexandru.dadalau@meshparts.de>
| Rich schrieb am Dienstag, 28. Juni 2022 um 23:14:28 UTC+2:
| > What you are seeing is /normal/ for binary floating point math.
>
| Thank you all.
| I actually aware of those numerical issues.
| But 1e-8 suprized me alot, because it's actually a large number in my opinion.
| I wonder, if same operation in C would give better results.

% cat t.c
#include <stdio.h>
int main() {

/* set x -9408.8 */
/* puts [expr {$x*$x + $x*$x + $x*$x - $x*$x - $x*$x - $x*$x}] */
double x = -9408.8;
printf("%g\n", x*x + x*x + x*x - x*x - x*x - x*x);

/* set x -9408.8 */
/* set y -9408.8 */
/* set z -9408.8 */
/* puts [expr {($x*$x + $y*$y + $z*$z - $x*$y - $y*$z - $z*$x)}] */
double y = -9408.8;
double z = -9408.8;
printf("%g\n", x*x + y*y + z*z - x*y - y*z - z*x);

}
% gcc -o t t.c
% ./t
-2.98023e-08
-2.98023e-08

HTH
R'

Re: Why is this result not zero?

<0599c053-88c7-4ecb-9f59-1ef5564d03a2n@googlegroups.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=19602&group=comp.lang.tcl#19602

  copy link   Newsgroups: comp.lang.tcl
X-Received: by 2002:a05:600c:601c:b0:3a0:4cd9:ed46 with SMTP id az28-20020a05600c601c00b003a04cd9ed46mr5537281wmb.139.1656507679577;
Wed, 29 Jun 2022 06:01:19 -0700 (PDT)
X-Received: by 2002:a05:6870:2e05:b0:106:7fdc:de95 with SMTP id
oi5-20020a0568702e0500b001067fdcde95mr1858946oab.123.1656507678724; Wed, 29
Jun 2022 06:01:18 -0700 (PDT)
Path: i2pn2.org!i2pn.org!aioe.org!news.uzoreto.com!feeder1.cambriumusenet.nl!feed.tweak.nl!209.85.128.87.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.tcl
Date: Wed, 29 Jun 2022 06:01:18 -0700 (PDT)
In-Reply-To: <818ad0fc-fd3f-4b62-a2eb-8476de86da69n@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=111.109.70.59; posting-account=6EmUxAoAAACuRtDvnYBLyd1K8pfkeNll
NNTP-Posting-Host: 111.109.70.59
References: <f579a874-342c-42c7-94a6-7d5fdb1f5bb8n@googlegroups.com>
<b7895fdb-e60d-4f6e-91be-0721c85c59a8n@googlegroups.com> <818ad0fc-fd3f-4b62-a2eb-8476de86da69n@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <0599c053-88c7-4ecb-9f59-1ef5564d03a2n@googlegroups.com>
Subject: Re: Why is this result not zero?
From: tm9233yy@gmail.com (Yusuke Yamasaki)
Injection-Date: Wed, 29 Jun 2022 13:01:19 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
 by: Yusuke Yamasaki - Wed, 29 Jun 2022 13:01 UTC

2022年6月29日水曜日 17:29:54 UTC+9 Alexandru:
> Yusuke Yamasaki schrieb am Mittwoch, 29. Juni 2022 um 09:18:23 UTC+2:
> > package require Mpexpr
> >
> > set x -9408.8
> > puts [mpexpr {$x*$x + $x*$x + $x*$x - $x*$x - $x*$x - $x*$x}]
> > # => 0.0
> > set x -9408.8
> > set y -9408.8
> > set z -9408.8
> > puts [mpexpr {($x*$x + $y*$y + $z*$z - $x*$y - $y*$z - $z*$x)}]
> > # => 0.0
> Nice package. I'm afraid the performance will worsen much, as the computation is done many times and speed is important.

Yes. It's almost 30 times slower than [expr].
This package is an implementation of decimal floating point arithmetic.
So, it's free from error caused by conversion from binary to decimal floating point number.

Re: Why is this result not zero?

<t9hqnf$1fdhk$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=19603&group=comp.lang.tcl#19603

  copy link   Newsgroups: comp.lang.tcl
Path: i2pn2.org!i2pn.org!aioe.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: mssr953@gmail.com (Michael Soyka)
Newsgroups: comp.lang.tcl
Subject: Re: Why is this result not zero?
Date: Wed, 29 Jun 2022 11:22:22 -0400
Organization: self
Lines: 39
Message-ID: <t9hqnf$1fdhk$1@dont-email.me>
References: <f579a874-342c-42c7-94a6-7d5fdb1f5bb8n@googlegroups.com>
<t9fqvf$16i84$1@dont-email.me>
<fb21581b-2841-47c1-8eb5-5a2475a68e7fn@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 29 Jun 2022 15:22:23 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="f705aa304e3c49478ae4905697fc634d";
logging-data="1553972"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/43sLN3CEL76N+Y45fiWaw"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.10.0
Cancel-Lock: sha1:kzZjvjSOH3sBFQJ0WAlQf9iEMV8=
In-Reply-To: <fb21581b-2841-47c1-8eb5-5a2475a68e7fn@googlegroups.com>
Content-Language: en-US
 by: Michael Soyka - Wed, 29 Jun 2022 15:22 UTC

On 06/28/2022 5:18 PM, Alexandru wrote:
> Rich schrieb am Dienstag, 28. Juni 2022 um 23:14:28 UTC+2:
>> Alexandru <alexandr...@meshparts.de> wrote:
>>> Why is this result not zero?
>>>
>>> set x -9408.8
>>> puts [expr {$x*$x + $x*$x + $x*$x - $x*$x - $x*$x - $x*$x}]

You don't get zero because ($x*$x + $x*$x + $x*$x) is substantially
larger in magnitude than x but both terms are represented by the same
number of bits, 64.

Add parentheses and you do get zero:
expr {($x*$x + $x*$x + $x*$x) - ($x*$x + $x*$x + $x*$x)}

>>>
>>> This is a simplified demo of a more complex equation:
>>>
>>> set x -9408.8
>>> set y -9408.8
>>> set z -9408.8
>>> puts [expr {($x*$x + $y*$y + $z*$z - $x*$y - $y*$z - $z*$x)}]
>> See https://floating-point-gui.de/ (and read most of the material
>> there).
>>
>> What you are seeing is /normal/ for binary floating point math.
>
> Thank you all.
> I actually aware of those numerical issues.
> But 1e-8 suprized me alot, because it's actually a large number in my opinion.
> I wonder, if same operation in C would give better results.
> Another intersting thing, changing order of terms, leads to clean zero:
>
> expr $x*$x - $x*$y + $y*$y - $y*$z + $z*$z - $z*$x
> is 0.0
and this makes sense because each term has the same 64-bit representation.

-mike

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor