Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

PLUG IT IN!!!


devel / comp.lang.fortran / Integer overflow

SubjectAuthor
* Integer overflowBeliavsky
+* Re: Integer overflowgah4
|`* Re: Integer overflowRobin Vowels
| `* Re: Integer overflowgah4
|  `* Re: Integer overflowRobin Vowels
|   `* Re: Integer overflowgah4
|    `* Re: Integer overflowRobin Vowels
|     `* Re: Integer overflowRon Shepard
|      `- Re: Integer overflowgah4
`- Re: Integer overflowRobin Vowels

1
Integer overflow

<6fdffd0b-47c8-41ae-9b87-98cee18e9e9an@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.fortran
X-Received: by 2002:a37:41d6:0:b0:67e:4494:c5e9 with SMTP id o205-20020a3741d6000000b0067e4494c5e9mr2507373qka.605.1649954092420;
Thu, 14 Apr 2022 09:34:52 -0700 (PDT)
X-Received: by 2002:a81:af63:0:b0:2ec:e6a:7281 with SMTP id
x35-20020a81af63000000b002ec0e6a7281mr2733221ywj.170.1649954092038; Thu, 14
Apr 2022 09:34:52 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.goja.nl.eu.org!2.eu.feeder.erje.net!feeder.erje.net!border1.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.fortran
Date: Thu, 14 Apr 2022 09:34:51 -0700 (PDT)
Injection-Info: google-groups.googlegroups.com; posting-host=173.76.111.99; posting-account=Ac_J3QkAAABih73tf3Yz4sHazwGUM-hW
NNTP-Posting-Host: 173.76.111.99
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <6fdffd0b-47c8-41ae-9b87-98cee18e9e9an@googlegroups.com>
Subject: Integer overflow
From: beliavsky@aol.com (Beliavsky)
Injection-Date: Thu, 14 Apr 2022 16:34:52 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 21
 by: Beliavsky - Thu, 14 Apr 2022 16:34 UTC

Playing with integers, I am surprised that even when i**2 overflows, the compiler may still give 2*i-1 for i**2-(i-1)**2, as this program shows.

program main
implicit none
integer, parameter :: imin = huge(1)/10, imax = huge(imin)-1
integer :: i,j,k,jk
logical, parameter :: print_all = .false.
print "(*(a18))","i","2*i-1","(i**2)-(i-1)**2","j=i**2","k=(i-1)**2","j-k"
do i=imin,imax
j = i**2
k = (i-1)**2
jk = j - k
if (print_all .or. i==imin .or. i==imax) &
print "(*(1x,i17))",i,2*i-1,i**2 - (i-1)**2,j,k,jk
if (jk /= (2*i-1)) stop "here"
end do
end program main

output:
i 2*i-1 (i**2)-(i-1)**2 j=i**2 k=(i-1)**2 j-k
214748364 429496727 429496727 687194768 257698041 429496727
2147483646 -5 -5 4 9 -5

Re: Integer overflow

<f993e62b-fd55-4432-afe6-5d6d1b8cb2d1n@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.fortran
X-Received: by 2002:a37:aec7:0:b0:69b:f27b:8784 with SMTP id x190-20020a37aec7000000b0069bf27b8784mr3510007qke.464.1649973037095;
Thu, 14 Apr 2022 14:50:37 -0700 (PDT)
X-Received: by 2002:a05:6902:150e:b0:641:996e:8ac2 with SMTP id
q14-20020a056902150e00b00641996e8ac2mr3529236ybu.499.1649973036893; Thu, 14
Apr 2022 14:50:36 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.fortran
Date: Thu, 14 Apr 2022 14:50:36 -0700 (PDT)
In-Reply-To: <6fdffd0b-47c8-41ae-9b87-98cee18e9e9an@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=2601:602:9700:4689:f086:1d92:ba9:5dc2;
posting-account=gLDX1AkAAAA26M5HM-O3sVMAXdxK9FPA
NNTP-Posting-Host: 2601:602:9700:4689:f086:1d92:ba9:5dc2
References: <6fdffd0b-47c8-41ae-9b87-98cee18e9e9an@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <f993e62b-fd55-4432-afe6-5d6d1b8cb2d1n@googlegroups.com>
Subject: Re: Integer overflow
From: gah4@u.washington.edu (gah4)
Injection-Date: Thu, 14 Apr 2022 21:50:37 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 43
 by: gah4 - Thu, 14 Apr 2022 21:50 UTC

On Thursday, April 14, 2022 at 9:34:54 AM UTC-7, Beliavsky wrote:
> Playing with integers, I am surprised that even when i**2 overflows, the compiler may still give 2*i-1 for i**2-(i-1)**2, as this program shows.
>
> program main
> implicit none
> integer, parameter :: imin = huge(1)/10, imax = huge(imin)-1
> integer :: i,j,k,jk
> logical, parameter :: print_all = .false.
> print "(*(a18))","i","2*i-1","(i**2)-(i-1)**2","j=i**2","k=(i-1)**2","j-k"
> do i=imin,imax
> j = i**2
> k = (i-1)**2
> jk = j - k
> if (print_all .or. i==imin .or. i==imax) &
> print "(*(1x,i17))",i,2*i-1,i**2 - (i-1)**2,j,k,jk
> if (jk /= (2*i-1)) stop "here"
> end do
> end program main
>
> output:
> i 2*i-1 (i**2)-(i-1)**2 j=i**2 k=(i-1)**2 j-k
> 214748364 429496727 429496727 687194768 257698041 429496727
> 2147483646 -5 -5 4 9 -5

Most hardware now gives the low bits of the full two's complement product.

If they both overflow the same amount, when you subtract then you get
the result that you would get without overflow.

You could put an IF in the loop, and compare them, but I suspect you are right.

Note that in the second case, both are overflowing.

The products give the low 32 bits as two's complement values.
When you subtract, you get the low 32 bits of the difference.
(In both cases, half the time they will have the wrong sign, though.)

It is slightly more interesting on ones' complement hardware.

Re: Integer overflow

<d62add67-57c5-4230-bf85-d2a2ae0b1e19n@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.fortran
X-Received: by 2002:ac8:110a:0:b0:2f1:ea84:b84 with SMTP id c10-20020ac8110a000000b002f1ea840b84mr3024235qtj.463.1650031449625;
Fri, 15 Apr 2022 07:04:09 -0700 (PDT)
X-Received: by 2002:a25:34f:0:b0:641:3840:3725 with SMTP id
76-20020a25034f000000b0064138403725mr5636481ybd.183.1650031449390; Fri, 15
Apr 2022 07:04:09 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.fortran
Date: Fri, 15 Apr 2022 07:04:09 -0700 (PDT)
In-Reply-To: <6fdffd0b-47c8-41ae-9b87-98cee18e9e9an@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=202.67.103.232; posting-account=S_MdrwoAAAD7T2pxG2e393dk6y0tc0Le
NNTP-Posting-Host: 202.67.103.232
References: <6fdffd0b-47c8-41ae-9b87-98cee18e9e9an@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <d62add67-57c5-4230-bf85-d2a2ae0b1e19n@googlegroups.com>
Subject: Re: Integer overflow
From: robin.vowels@gmail.com (Robin Vowels)
Injection-Date: Fri, 15 Apr 2022 14:04:09 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 25
 by: Robin Vowels - Fri, 15 Apr 2022 14:04 UTC

On Friday, April 15, 2022 at 2:34:54 AM UTC+10, Beliavsky wrote:
> Playing with integers, I am surprised that even when i**2 overflows, the compiler may still give 2*i-1 for i**2-(i-1)**2, as this program shows.
>
> program main
> implicit none
> integer, parameter :: imin = huge(1)/10, imax = huge(imin)-1
> integer :: i,j,k,jk
> logical, parameter :: print_all = .false.
> print "(*(a18))","i","2*i-1","(i**2)-(i-1)**2","j=i**2","k=(i-1)**2","j-k"
> do i=imin,imax
> j = i**2
> k = (i-1)**2
> jk = j - k
> if (print_all .or. i==imin .or. i==imax) &
> print "(*(1x,i17))",i,2*i-1,i**2 - (i-1)**2,j,k,jk
> if (jk /= (2*i-1)) stop "here"
> end do
> end program main
>
> output:
> i 2*i-1 (i**2)-(i-1)**2 j=i**2 k=(i-1)**2 j-k
> 214748364 429496727 429496727 687194768 257698041 429496727
> 2147483646 -5 -5 4 9 -5

Use Silverfrost FTN95.
The object program stops when integer overflow occurs.

Re: Integer overflow

<10bf047c-56d2-493f-9034-215cdeab3441n@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.fortran
X-Received: by 2002:ac8:75d1:0:b0:2ed:161f:1f0a with SMTP id z17-20020ac875d1000000b002ed161f1f0amr5431023qtq.173.1650031794171;
Fri, 15 Apr 2022 07:09:54 -0700 (PDT)
X-Received: by 2002:a05:6902:c8:b0:633:ee0c:bca2 with SMTP id
i8-20020a05690200c800b00633ee0cbca2mr5922666ybs.82.1650031794027; Fri, 15 Apr
2022 07:09:54 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.fortran
Date: Fri, 15 Apr 2022 07:09:53 -0700 (PDT)
In-Reply-To: <f993e62b-fd55-4432-afe6-5d6d1b8cb2d1n@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=202.67.103.232; posting-account=S_MdrwoAAAD7T2pxG2e393dk6y0tc0Le
NNTP-Posting-Host: 202.67.103.232
References: <6fdffd0b-47c8-41ae-9b87-98cee18e9e9an@googlegroups.com> <f993e62b-fd55-4432-afe6-5d6d1b8cb2d1n@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <10bf047c-56d2-493f-9034-215cdeab3441n@googlegroups.com>
Subject: Re: Integer overflow
From: robin.vowels@gmail.com (Robin Vowels)
Injection-Date: Fri, 15 Apr 2022 14:09:54 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 46
 by: Robin Vowels - Fri, 15 Apr 2022 14:09 UTC

On Friday, April 15, 2022 at 7:50:38 AM UTC+10, gah4 wrote:
> On Thursday, April 14, 2022 at 9:34:54 AM UTC-7, Beliavsky wrote:
> > Playing with integers, I am surprised that even when i**2 overflows, the compiler may still give 2*i-1 for i**2-(i-1)**2, as this program shows.
> >
> > program main
> > implicit none
> > integer, parameter :: imin = huge(1)/10, imax = huge(imin)-1
> > integer :: i,j,k,jk
> > logical, parameter :: print_all = .false.
> > print "(*(a18))","i","2*i-1","(i**2)-(i-1)**2","j=i**2","k=(i-1)**2","j-k"
> > do i=imin,imax
> > j = i**2
> > k = (i-1)**2
> > jk = j - k
> > if (print_all .or. i==imin .or. i==imax) &
> > print "(*(1x,i17))",i,2*i-1,i**2 - (i-1)**2,j,k,jk
> > if (jk /= (2*i-1)) stop "here"
> > end do
> > end program main
> >
> > output:
> > i 2*i-1 (i**2)-(i-1)**2 j=i**2 k=(i-1)**2 j-k
> > 214748364 429496727 429496727 687194768 257698041 429496727
> > 2147483646 -5 -5 4 9 -5
..
> Most hardware now gives the low bits of the full two's complement product.
..
The product is positive (see the initial values).
The low-order bits are not 2's complement. They are the
the truncated low-order bits of the positive product.
>
> If they both overflow the same amount, when you subtract then you get
> the result that you would get without overflow.
>
> You could put an IF in the loop, and compare them, but I suspect you are right.
>
> Note that in the second case, both are overflowing.
>
> The products give the low 32 bits as two's complement values.

No they aren't. The product s positive, and he gets the
truncated low-order bits. (see above)

> When you subtract, you get the low 32 bits of the difference.
> (In both cases, half the time they will have the wrong sign, though.)
>
> It is slightly more interesting on ones' complement hardware.

Re: Integer overflow

<1d08e929-64b6-4f76-8e9b-ef2d8bdeb7dfn@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.fortran
X-Received: by 2002:a05:6214:e88:b0:446:383a:62ec with SMTP id hf8-20020a0562140e8800b00446383a62ecmr1310781qvb.68.1650070917513;
Fri, 15 Apr 2022 18:01:57 -0700 (PDT)
X-Received: by 2002:a0d:eb8f:0:b0:2eb:f1e4:2653 with SMTP id
u137-20020a0deb8f000000b002ebf1e42653mr1329504ywe.401.1650070917341; Fri, 15
Apr 2022 18:01:57 -0700 (PDT)
Path: i2pn2.org!rocksolid2!i2pn.org!weretis.net!feeder8.news.weretis.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.fortran
Date: Fri, 15 Apr 2022 18:01:57 -0700 (PDT)
In-Reply-To: <10bf047c-56d2-493f-9034-215cdeab3441n@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=2601:602:9700:4689:e80f:fb3f:f38e:126c;
posting-account=gLDX1AkAAAA26M5HM-O3sVMAXdxK9FPA
NNTP-Posting-Host: 2601:602:9700:4689:e80f:fb3f:f38e:126c
References: <6fdffd0b-47c8-41ae-9b87-98cee18e9e9an@googlegroups.com>
<f993e62b-fd55-4432-afe6-5d6d1b8cb2d1n@googlegroups.com> <10bf047c-56d2-493f-9034-215cdeab3441n@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <1d08e929-64b6-4f76-8e9b-ef2d8bdeb7dfn@googlegroups.com>
Subject: Re: Integer overflow
From: gah4@u.washington.edu (gah4)
Injection-Date: Sat, 16 Apr 2022 01:01:57 +0000
Content-Type: text/plain; charset="UTF-8"
 by: gah4 - Sat, 16 Apr 2022 01:01 UTC

On Friday, April 15, 2022 at 7:09:56 AM UTC-7, Robin Vowels wrote:

(snip)

> > Most hardware now gives the low bits of the full two's complement product.
> The product is positive (see the initial values).
> The low-order bits are not 2's complement. They are the
> the truncated low-order bits of the positive product.

Read carefully:

Most hardware now gives the low bits of the full two's complement product.

That is, (the low bits of) (the full two's complement product).

That is, multiply them and generate a double length two's complement
product, then return the low bits. And yes often enough they have
a different sign, about half the time.

Maybe I should say it again:

Most hardware now gives the low bits of the full two's complement product.

Re: Integer overflow

<912621a0-6124-411f-bd32-bb0e71c0c68cn@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.fortran
X-Received: by 2002:a05:620a:2415:b0:69e:784d:3a4c with SMTP id d21-20020a05620a241500b0069e784d3a4cmr24051qkn.14.1650076740741;
Fri, 15 Apr 2022 19:39:00 -0700 (PDT)
X-Received: by 2002:a25:32d0:0:b0:641:d802:171c with SMTP id
y199-20020a2532d0000000b00641d802171cmr1671399yby.498.1650076740560; Fri, 15
Apr 2022 19:39:00 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!1.us.feeder.erje.net!feeder.erje.net!border1.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.fortran
Date: Fri, 15 Apr 2022 19:39:00 -0700 (PDT)
In-Reply-To: <1d08e929-64b6-4f76-8e9b-ef2d8bdeb7dfn@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=202.67.103.232; posting-account=S_MdrwoAAAD7T2pxG2e393dk6y0tc0Le
NNTP-Posting-Host: 202.67.103.232
References: <6fdffd0b-47c8-41ae-9b87-98cee18e9e9an@googlegroups.com>
<f993e62b-fd55-4432-afe6-5d6d1b8cb2d1n@googlegroups.com> <10bf047c-56d2-493f-9034-215cdeab3441n@googlegroups.com>
<1d08e929-64b6-4f76-8e9b-ef2d8bdeb7dfn@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <912621a0-6124-411f-bd32-bb0e71c0c68cn@googlegroups.com>
Subject: Re: Integer overflow
From: robin.vowels@gmail.com (Robin Vowels)
Injection-Date: Sat, 16 Apr 2022 02:39:00 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 38
 by: Robin Vowels - Sat, 16 Apr 2022 02:39 UTC

On Saturday, April 16, 2022 at 11:01:59 AM UTC+10, gah4 wrote:
> On Friday, April 15, 2022 at 7:09:56 AM UTC-7, Robin Vowels wrote:
>
> (snip)
> > > Most hardware now gives the low bits of the full two's complement product.
> > The product is positive (see the initial values).
> > The low-order bits are not 2's complement. They are the
> > the truncated low-order bits of the positive product.
> Read carefully:
..
> Most hardware now gives the low bits of the full two's complement product.
> That is, (the low bits of) (the full two's complement product).
..
You still don't get it.
See the initial values.
The products are ALWAYS positive.
The products are NOT twos complement.
The products are ALWAYS positive.
..
> That is, multiply them and generate a double length two's complement
> product,
..
They are NOT twos complement products.
The products are ALWAYS positive.
..
It is only negative values that are represented in twos complement form.
..
> then return the low bits. And yes often enough they have
> a different sign, about half the time.
..
Because the high bits are removed, what remains are the truncated
low-order bits.
The most significant bit of that can be 0 or 1.
If it is 1, the value appears to be negative.
..
> Maybe I should say it again:
> Most hardware now gives the low bits of the full two's complement product.
..
See above.

Re: Integer overflow

<10b92453-5f80-4477-ad07-9b28e2e75b67n@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.fortran
X-Received: by 2002:a05:622a:188e:b0:2e2:3c47:9cab with SMTP id v14-20020a05622a188e00b002e23c479cabmr1243926qtc.559.1650079118881;
Fri, 15 Apr 2022 20:18:38 -0700 (PDT)
X-Received: by 2002:a81:2485:0:b0:2ec:354d:d45a with SMTP id
k127-20020a812485000000b002ec354dd45amr1676656ywk.213.1650079118688; Fri, 15
Apr 2022 20:18:38 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!1.us.feeder.erje.net!feeder.erje.net!border1.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.fortran
Date: Fri, 15 Apr 2022 20:18:38 -0700 (PDT)
In-Reply-To: <912621a0-6124-411f-bd32-bb0e71c0c68cn@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=2601:602:9700:4689:132:c5b2:cb62:f63d;
posting-account=gLDX1AkAAAA26M5HM-O3sVMAXdxK9FPA
NNTP-Posting-Host: 2601:602:9700:4689:132:c5b2:cb62:f63d
References: <6fdffd0b-47c8-41ae-9b87-98cee18e9e9an@googlegroups.com>
<f993e62b-fd55-4432-afe6-5d6d1b8cb2d1n@googlegroups.com> <10bf047c-56d2-493f-9034-215cdeab3441n@googlegroups.com>
<1d08e929-64b6-4f76-8e9b-ef2d8bdeb7dfn@googlegroups.com> <912621a0-6124-411f-bd32-bb0e71c0c68cn@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <10b92453-5f80-4477-ad07-9b28e2e75b67n@googlegroups.com>
Subject: Re: Integer overflow
From: gah4@u.washington.edu (gah4)
Injection-Date: Sat, 16 Apr 2022 03:18:38 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 25
 by: gah4 - Sat, 16 Apr 2022 03:18 UTC

On Friday, April 15, 2022 at 7:39:02 PM UTC-7, Robin Vowels wrote:

> They are NOT twos complement products.
> The products are ALWAYS positive.

Two's complement is the representation used on most machines now.

It can represent both negative and positive values. It is still called
two's complement even when the values are positive.

> It is only negative values that are represented in twos complement form.

The "two's complement" operation is used to change the sign of
a value that is represented in two's complement, but no, it is not only
negative values. They are negative when the sign bit is set, and
positive or zero when it isn't.

And, just to be sure, Unisys last I knew still sold some ones' complement
machines. There are complications with C on such machines, as
unsigned arithmetic doesn't do what it is supposed to do. And ones'
complement machines can also represent negative, positive,
and zero values.

Re: Integer overflow

<4f3f0eb4-8911-4e48-9ca3-b7a7c5234ec1n@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.fortran
X-Received: by 2002:a05:622a:56:b0:2f1:fbea:c68d with SMTP id y22-20020a05622a005600b002f1fbeac68dmr1143189qtw.58.1650201706646;
Sun, 17 Apr 2022 06:21:46 -0700 (PDT)
X-Received: by 2002:a25:d507:0:b0:63d:a541:1a8c with SMTP id
r7-20020a25d507000000b0063da5411a8cmr6282645ybe.92.1650201706414; Sun, 17 Apr
2022 06:21:46 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.fortran
Date: Sun, 17 Apr 2022 06:21:46 -0700 (PDT)
In-Reply-To: <10b92453-5f80-4477-ad07-9b28e2e75b67n@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=202.67.103.232; posting-account=S_MdrwoAAAD7T2pxG2e393dk6y0tc0Le
NNTP-Posting-Host: 202.67.103.232
References: <6fdffd0b-47c8-41ae-9b87-98cee18e9e9an@googlegroups.com>
<f993e62b-fd55-4432-afe6-5d6d1b8cb2d1n@googlegroups.com> <10bf047c-56d2-493f-9034-215cdeab3441n@googlegroups.com>
<1d08e929-64b6-4f76-8e9b-ef2d8bdeb7dfn@googlegroups.com> <912621a0-6124-411f-bd32-bb0e71c0c68cn@googlegroups.com>
<10b92453-5f80-4477-ad07-9b28e2e75b67n@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <4f3f0eb4-8911-4e48-9ca3-b7a7c5234ec1n@googlegroups.com>
Subject: Re: Integer overflow
From: robin.vowels@gmail.com (Robin Vowels)
Injection-Date: Sun, 17 Apr 2022 13:21:46 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 43
 by: Robin Vowels - Sun, 17 Apr 2022 13:21 UTC

On Saturday, April 16, 2022 at 1:18:40 PM UTC+10, gah4 wrote:
> On Friday, April 15, 2022 at 7:39:02 PM UTC-7, Robin Vowels wrote:
>
>
> > They are NOT twos complement products. [when an integer is squared]
> > The products are ALWAYS positive.
..
> Two's complement is the representation used on most machines now.
>
> It can represent both negative and positive values. It is still called
> two's complement even when the values are positive.
..
You are mistaken. It is only negative values that are represented
in twos complement form.
..
> > It is only negative values that are represented in twos complement form.
..
> The "two's complement" operation is used to change the sign of
> a value that is represented in two's complement,
..
You are still mistaken. A twos complement operation is used to
negate a value. When the argument represents a positive integer,
the operation produces the negative form. When the argument
represents a negative integer, the operation produces a positive integer
(with one exception).
..
However, it is only negative values that are held in two's complement form.
..
It might surprise you to know that when values are summed
(be they negative, positive, or zero), the adder merely forms
the sum by adding corresponding bits and adding to that any carry
from a lower-order bit, and forwarding any carry to the next-higher bit.
..
> but no, it is not only
> negative values. They are negative when the sign bit is set, and
> positive or zero when it isn't.
..
There is no sign bit. All bits are data bits.
..
> And, just to be sure, Unisys last I knew still sold some ones' complement
> machines. There are complications with C on such machines, as
> unsigned arithmetic doesn't do what it is supposed to do. And ones'
> complement machines can also represent negative, positive,
> and zero values.

Re: Integer overflow

<mIY6K.53321$rWve.16617@fx01.iad>

  copy mid

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

  copy link   Newsgroups: comp.lang.fortran
Path: i2pn2.org!i2pn.org!usenet.goja.nl.eu.org!news.freedyn.de!newsreader4.netcologne.de!news.netcologne.de!peer03.ams1!peer.ams1.xlned.com!news.xlned.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx01.iad.POSTED!not-for-mail
MIME-Version: 1.0
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:91.0)
Gecko/20100101 Thunderbird/91.8.0
Subject: Re: Integer overflow
Content-Language: en-US
Newsgroups: comp.lang.fortran
References: <6fdffd0b-47c8-41ae-9b87-98cee18e9e9an@googlegroups.com>
<f993e62b-fd55-4432-afe6-5d6d1b8cb2d1n@googlegroups.com>
<10bf047c-56d2-493f-9034-215cdeab3441n@googlegroups.com>
<1d08e929-64b6-4f76-8e9b-ef2d8bdeb7dfn@googlegroups.com>
<912621a0-6124-411f-bd32-bb0e71c0c68cn@googlegroups.com>
<10b92453-5f80-4477-ad07-9b28e2e75b67n@googlegroups.com>
<4f3f0eb4-8911-4e48-9ca3-b7a7c5234ec1n@googlegroups.com>
From: nospam@nowhere.org (Ron Shepard)
In-Reply-To: <4f3f0eb4-8911-4e48-9ca3-b7a7c5234ec1n@googlegroups.com>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Lines: 65
Message-ID: <mIY6K.53321$rWve.16617@fx01.iad>
X-Complaints-To: abuse@easynews.com
Organization: Forte - www.forteinc.com
X-Complaints-Info: Please be sure to forward a copy of ALL headers otherwise we will be unable to process your complaint properly.
Date: Sun, 17 Apr 2022 13:08:18 -0500
X-Received-Bytes: 4561
 by: Ron Shepard - Sun, 17 Apr 2022 18:08 UTC

On 4/17/22 8:21 AM, Robin Vowels wrote:
> On Saturday, April 16, 2022 at 1:18:40 PM UTC+10, gah4 wrote:
>> On Friday, April 15, 2022 at 7:39:02 PM UTC-7, Robin Vowels wrote:
>>
>>
>>> They are NOT twos complement products. [when an integer is squared]
>>> The products are ALWAYS positive.
> .
>> Two's complement is the representation used on most machines now.
>>
>> It can represent both negative and positive values. It is still called
>> two's complement even when the values are positive.
> .
> You are mistaken. It is only negative values that are represented
> in twos complement form.

Here is the wikipedia article about twos-complement arithmetic.

https://en.wikipedia.org/wiki/Two%27s_complement

As you can see there, the term "twos-complement" can be regarded both as
an operation applied to a string of bits and also as the name of the
storage representation used for integers.

For this discussion, the important feature of twos-complement arithmetic
(i.e. the storage representation) is that the bits that result from
integer operations for the signed twos-complement values are the same as
would result from treating those bits as an unsigned integer and
ignoring any overflow of the high-order bit. Thus the "twos-complement
operation" never appears explicitly in the addition and multiplication
integer operations, the correct result just happens naturally.

Another interesting way to view the twos-complement representation
(which is discussed in that wiki article) is that the integer value of
an unsigned integer is simply the result of the polynomial evaluation

Sum(i=0:N-1) b(i) * 2**(i)

over all the bits. The twos-complement value for those same bits is the
same expression, except the high-order bit term is replaced by
-b(N-1)*2**N. I learned this long ago from one of Hamming's books, and
over the years, that expression has clarified a lot of algorithms for me.

This then brings up the lack of an unsigned integer type in fortran. If
all hardware supported two-complement arithmetic, then I think it would
be straightforward to allow this type into the language. Compilers would
need to do little to support the type and its semantics. But those
computers that use different integer representations (e.g.
ones-complement and signed-magnitude), the twos-complement results would
need to be simulated, and that could be expensive and slow. I'm not
familiar with the current C standard, but in the past the C language
punted on this issue by defining results for unsigned arithmetic only
when there is no overflow (i.e. when the high-order bit can be ignored).
If a C compiler happens to give correct results also when there is
overflow (i.e. when the high-order bit comes into play), then that is
great, but it was not required by the language. Fortran could have taken
that same approach, but it didn't.

This issue also arises in the discussions over the decades for fortran
to support an intrinsic bit data type. When those bits are moved between
the bit data type and integer variables, then the meaning of the
high-order bit, or the sign bit, must be accounted for correctly, making
portable code difficult to write.

$.02 -Ron Shepard

Re: Integer overflow

<cef9a4a9-ce41-4cda-a162-4afdcb02fc52n@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.fortran
X-Received: by 2002:a05:6214:1643:b0:42c:2865:d1e7 with SMTP id f3-20020a056214164300b0042c2865d1e7mr5817014qvw.52.1650220839198;
Sun, 17 Apr 2022 11:40:39 -0700 (PDT)
X-Received: by 2002:a25:b0a5:0:b0:641:4c43:db00 with SMTP id
f37-20020a25b0a5000000b006414c43db00mr7040439ybj.478.1650220839040; Sun, 17
Apr 2022 11:40:39 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.fortran
Date: Sun, 17 Apr 2022 11:40:38 -0700 (PDT)
In-Reply-To: <mIY6K.53321$rWve.16617@fx01.iad>
Injection-Info: google-groups.googlegroups.com; posting-host=2601:602:9700:4689:6817:9b1a:4a41:f99e;
posting-account=gLDX1AkAAAA26M5HM-O3sVMAXdxK9FPA
NNTP-Posting-Host: 2601:602:9700:4689:6817:9b1a:4a41:f99e
References: <6fdffd0b-47c8-41ae-9b87-98cee18e9e9an@googlegroups.com>
<f993e62b-fd55-4432-afe6-5d6d1b8cb2d1n@googlegroups.com> <10bf047c-56d2-493f-9034-215cdeab3441n@googlegroups.com>
<1d08e929-64b6-4f76-8e9b-ef2d8bdeb7dfn@googlegroups.com> <912621a0-6124-411f-bd32-bb0e71c0c68cn@googlegroups.com>
<10b92453-5f80-4477-ad07-9b28e2e75b67n@googlegroups.com> <4f3f0eb4-8911-4e48-9ca3-b7a7c5234ec1n@googlegroups.com>
<mIY6K.53321$rWve.16617@fx01.iad>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <cef9a4a9-ce41-4cda-a162-4afdcb02fc52n@googlegroups.com>
Subject: Re: Integer overflow
From: gah4@u.washington.edu (gah4)
Injection-Date: Sun, 17 Apr 2022 18:40:39 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 35
 by: gah4 - Sun, 17 Apr 2022 18:40 UTC

On Sunday, April 17, 2022 at 11:08:22 AM UTC-7, Ron Shepard wrote:

(snip)

> This then brings up the lack of an unsigned integer type in fortran. If
> all hardware supported two-complement arithmetic, then I think it would
> be straightforward to allow this type into the language. Compilers would
> need to do little to support the type and its semantics. But those
> computers that use different integer representations (e.g.
> ones-complement and signed-magnitude), the twos-complement results would
> need to be simulated, and that could be expensive and slow. I'm not
> familiar with the current C standard, but in the past the C language
> punted on this issue by defining results for unsigned arithmetic only
> when there is no overflow (i.e. when the high-order bit can be ignored).
> If a C compiler happens to give correct results also when there is
> overflow (i.e. when the high-order bit comes into play), then that is
> great, but it was not required by the language. Fortran could have taken
> that same approach, but it didn't.
C allows for two's complement, ones' complement, or sign magnitude
for signed integers, but only one representation for unsigned.

Last I knew, Unisys was still selling ones' complement hardware, along
with a C compiler. The C compiler has funny rules for its unsigned,
because of what the hardware does.

> This issue also arises in the discussions over the decades for fortran
> to support an intrinsic bit data type. When those bits are moved between
> the bit data type and integer variables, then the meaning of the
> high-order bit, or the sign bit, must be accounted for correctly, making
> portable code difficult to write.

Well, Fortran, according to the standard, allows any integer radix
greater than one. It isn't obviously what it should do for bitwise
operators other than for radix two.

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor