Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

CCI Power 6/40: one board, a megabyte of cache, and an attitude...


devel / comp.lang.tcl / Double trouble

SubjectAuthor
* Double troubleLuc
`* Double troubleRich
 `- Double troubleTorsten Berg

1
Double trouble

<20221126152917.7df718c8@lud1.home>

  copy mid

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

  copy link   Newsgroups: comp.lang.tcl
Path: i2pn2.org!i2pn.org!aioe.org!ORaKy86VnTSITJ5zjn3gjw.user.46.165.242.75.POSTED!not-for-mail
From: no...@no.no (Luc)
Newsgroups: comp.lang.tcl
Subject: Double trouble
Date: Sat, 26 Nov 2022 15:29:17 -0300
Organization: Aioe.org NNTP Server
Message-ID: <20221126152917.7df718c8@lud1.home>
Mime-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="9099"; posting-host="ORaKy86VnTSITJ5zjn3gjw.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
X-Notice: Filtered by postfilter v. 0.9.2
X-Newsreader: Claws Mail 3.14.1 (GTK+ 2.24.31; x86_64-pc-linux-gnu)
 by: Luc - Sat, 26 Nov 2022 18:29 UTC

An old Tcl problem has come back to haunt me.

--------------------
TYPES, OVERFLOW, AND PRECISION

Conversion among internal representations for integer, floating-point,
and string operands is done automatically as needed. For arithmetic
computations, integers are used until some floating-point number is
introduced, after which floating-point is used. For example,

expr {5 / 4}

returns 1, while
expr {5 / 4.0}
expr {5 / ( [string length "abcd"] + 0.0 )}

both return 1.25. Floating-point values are always returned with a "."
or an e so that they will not look like integer values. For example,

expr {20.0/5.0}

returns 4.0, not 4.
--------------------

That sounds like a horrible pitfall to me.

I've been writing some code, stunned because a certain division operation
was adamantly returning 0. It took me some time to realize I had to convert
at least one of the numbers into float:

set ::tcl_precision 8
set x [::tcl::mathfunc::double $x]
set result [expr $x / $y]

I don't really mind doing that, but what if I run a series of calculations
whose numbers I cannot predict and a couple of integers are the results of
some of those calculations and they end up being calculated against each
other and Tcl outputs an incorrect integer result merely because those
two numbers happened to be integers?

That's how trains get derailed and spaceships explode, isn't it?

What do you sage Tclers have to say about this?

--
Luc
>>

Re: Double trouble

<tltmpq$1ebe2$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.tcl
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: ric...@example.invalid (Rich)
Newsgroups: comp.lang.tcl
Subject: Re: Double trouble
Date: Sat, 26 Nov 2022 18:44:10 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 34
Message-ID: <tltmpq$1ebe2$1@dont-email.me>
References: <20221126152917.7df718c8@lud1.home>
Injection-Date: Sat, 26 Nov 2022 18:44:10 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="4acb3f9f349a4304f8dd4ea1570f30a3";
logging-data="1519042"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+53ZRxzumi39tDW8oCgIg5"
User-Agent: tin/2.0.1-20111224 ("Achenvoir") (UNIX) (Linux/3.10.17 (x86_64))
Cancel-Lock: sha1:w1yiySJmiaHZURXAfeZ2tkIFTww=
 by: Rich - Sat, 26 Nov 2022 18:44 UTC

Luc <no@no.no> wrote:
> I've been writing some code, stunned because a certain division operation
> was adamantly returning 0. It took me some time to realize I had to convert
> at least one of the numbers into float:
>
> set ::tcl_precision 8
> set x [::tcl::mathfunc::double $x]
> set result [expr $x / $y]
>
> I don't really mind doing that, but what if I run a series of calculations
> whose numbers I cannot predict and a couple of integers are the results of
> some of those calculations and they end up being calculated against each
> other and Tcl outputs an incorrect integer result merely because those
> two numbers happened to be integers?
>
> That's how trains get derailed and spaceships explode, isn't it?
>
> What do you sage Tclers have to say about this?

For any expr's where you want fractional answers, and you are not 100%
sure at least one input is always going to be a float, always wrap a
'double' around at least one part of the expression:

set answer [expr {double($x) / $y + $z}]

If x is already a float, the double() effectively becomes a no-op. If x is
integer, the double() promotes it to float.

And please don't suggest that Tcl should just promote to float for all
expr operations. Down that path lies Javascript and it's lack of
integer support entirely. And that path is haunted by a different set
of deamons ready to poke you with their red hot pitchforks when you do
other math operations.

Re: Double trouble

<52311e31-826e-4b7f-8934-7654255ce7b5n@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.tcl
X-Received: by 2002:ae9:e919:0:b0:6e0:90df:228f with SMTP id x25-20020ae9e919000000b006e090df228fmr23121377qkf.756.1669507106707;
Sat, 26 Nov 2022 15:58:26 -0800 (PST)
X-Received: by 2002:a05:6214:3688:b0:4bb:9180:638f with SMTP id
nl8-20020a056214368800b004bb9180638fmr24022261qvb.98.1669507106573; Sat, 26
Nov 2022 15:58:26 -0800 (PST)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.tcl
Date: Sat, 26 Nov 2022 15:58:26 -0800 (PST)
In-Reply-To: <tltmpq$1ebe2$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=77.8.88.23; posting-account=ZxsDiAkAAAC9vQQOtXQfXsMvri8SBAlg
NNTP-Posting-Host: 77.8.88.23
References: <20221126152917.7df718c8@lud1.home> <tltmpq$1ebe2$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <52311e31-826e-4b7f-8934-7654255ce7b5n@googlegroups.com>
Subject: Re: Double trouble
From: ber...@typoscriptics.de (Torsten Berg)
Injection-Date: Sat, 26 Nov 2022 23:58:26 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 2910
 by: Torsten Berg - Sat, 26 Nov 2022 23:58 UTC

Yes, you are right. I just try to remind myself all the time that Tcl works like C in this respect and when I do a division I make sure to explicitly make one of the numbers a float using double().

On Saturday, November 26, 2022 at 7:44:14 PM UTC+1, Rich wrote:
> Luc <n...@no.no> wrote:
> > I've been writing some code, stunned because a certain division operation
> > was adamantly returning 0. It took me some time to realize I had to convert
> > at least one of the numbers into float:
> >
> > set ::tcl_precision 8
> > set x [::tcl::mathfunc::double $x]
> > set result [expr $x / $y]
> >
> > I don't really mind doing that, but what if I run a series of calculations
> > whose numbers I cannot predict and a couple of integers are the results of
> > some of those calculations and they end up being calculated against each
> > other and Tcl outputs an incorrect integer result merely because those
> > two numbers happened to be integers?
> >
> > That's how trains get derailed and spaceships explode, isn't it?
> >
> > What do you sage Tclers have to say about this?
> For any expr's where you want fractional answers, and you are not 100%
> sure at least one input is always going to be a float, always wrap a
> 'double' around at least one part of the expression:
>
> set answer [expr {double($x) / $y + $z}]
>
> If x is already a float, the double() effectively becomes a no-op. If x is
> integer, the double() promotes it to float.
>
> And please don't suggest that Tcl should just promote to float for all
> expr operations. Down that path lies Javascript and it's lack of
> integer support entirely. And that path is haunted by a different set
> of deamons ready to poke you with their red hot pitchforks when you do
> other math operations.


devel / comp.lang.tcl / Double trouble

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor