Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

Help fight continental drift.


devel / comp.lang.c / Re: Signed integer overflow

SubjectAuthor
* Signed integer overflowHowerd Oakford
+* Re: Signed integer overflowRichard Damon
|+- Re: Signed integer overflowDavid Brown
|`* Re: Signed integer overflowHowerd Oakford
| +- Re: Signed integer overflowRichard Damon
| +- Re: Signed integer overflowDavid Brown
| +* Re: Signed integer overflowBen Bacarisse
| |`* Re: Signed integer overflowKeith Thompson
| | `- Re: Signed integer overflowBen Bacarisse
| `* Re: Signed integer overflowAnton Shepelev
|  `* Re: Signed integer overflowManfred
|   `- Re: Signed integer overflowAnton Shepelev
+* Re: Signed integer overflowBart
|`* Re: Signed integer overflowDavid Brown
| `- Re: Signed integer overflowBart
+- Re: Signed integer overflowBarry Schwarz
+* Re: Signed integer overflowEric Sosman
|+* Re: Signed integer overflowHowerd Oakford
||`* Re: Signed integer overflowBart
|| +- Re: Signed integer overflowRichard Damon
|| `* Re: Signed integer overflowHowerd Oakford
||  +- Re: Signed integer overflowBen Bacarisse
||  +- Re: Signed integer overflowDavid Brown
||  +- Re: Signed integer overflowBart
||  `- Re: Signed integer overflowAnton Shepelev
|`- Re: Signed integer overflowBen Bacarisse
+- Re: Signed integer overflowJorgen Grahn
+* Re: Signed integer overflowÖö Tiib
|`* Re: Signed integer overflowHowerd Oakford
| +* Re: Signed integer overflowJames Kuyper
| |+- Re: Signed integer overflowChris M. Thomasson
| |`- Re: Signed integer overflowHowerd Oakford
| +* Re: Signed integer overflowBart
| |`* Re: Signed integer overflowHowerd Oakford
| | `- Re: Signed integer overflowAnton Shepelev
| `* Re: Signed integer overflowRichard Damon
|  `* Re: Signed integer overflowHowerd Oakford
|   +- Re: Signed integer overflowLew Pitcher
|   `- Re: Signed integer overflowRichard Damon
+- Re: Signed integer overflowBonita Montero
`* Re: Signed integer overflowBonita Montero
 `* Re: Signed integer overflowBonita Montero
  `* Re: Signed integer overflowBonita Montero
   `* Re: Signed integer overflowBart
    `- Re: Signed integer overflowBonita Montero

Pages:12
Re: Signed integer overflow

<smgp0g$tf6$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: jameskuy...@alumni.caltech.edu (James Kuyper)
Newsgroups: comp.lang.c
Subject: Re: Signed integer overflow
Date: Wed, 10 Nov 2021 10:37:52 -0500
Organization: A noiseless patient Spider
Lines: 34
Message-ID: <smgp0g$tf6$1@dont-email.me>
References: <38ba6059-8d52-4f64-9ce6-8d23b82203f6n@googlegroups.com>
<8afeebeb-053b-4c85-821b-19437e0ba5acn@googlegroups.com>
<7c19b204-be96-4f1e-aa1c-1c96d1a34733n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 10 Nov 2021 15:37:53 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="1f0b5ffa15880644b5e8d50f697a7dae";
logging-data="30182"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX195/ArnRCbNftkBNQ2Nn4z5lD+n+QIBA0w="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.13.0
Cancel-Lock: sha1:+FsHi1I/DwN1/jhb0g4ks7bR1hg=
In-Reply-To: <7c19b204-be96-4f1e-aa1c-1c96d1a34733n@googlegroups.com>
Content-Language: en-US
 by: James Kuyper - Wed, 10 Nov 2021 15:37 UTC

On 11/10/21 8:14 AM, Howerd Oakford wrote:
> On Tuesday, November 9, 2021 at 4:03:48 PM UTC+1, Öö Tiib wrote:
>> On Monday, 8 November 2021 at 19:56:23 UTC+2, how...@gmail.com wrote:
....
>>> In the actual system we have to store both latitude and longitude for several thousand points, so I don't really want to use uint64_t and manually do a 32 bit modulus...
>> Further distraction. If you need distances then converting from
>> spherical coordinates to Euclidian takes quite a trigonometry
>> and int32_t is not what one is using there. Use double.
>
> Hi Öö Tiib,
>
> My understanding is that you must scale the longitude difference by the cosine of the latitude, to first order.
> Is there something I am missing here ?

If you project the two points of interest to you onto a cylinder whose
axis coincides the with line between the north and south poles of the
earth, that approach will give you the length of the shortest path, on
that cylinder, between those two points. The size of that cylinder
depends upon which of the two latitudes you are taking the cosine of.

If d is the length in radians of the shortest path on the surface of the
Earth connecting those two points (ignoring the fact that the earth
isn't spherical), and those two points have latitudes lat1 and lat2, and
the difference in longitude between those two points is dlon, then

cos(d) = sin(lat1)*sin(lat2) - cos(lat1)*cos(lat2)*cos(dlon);

You'll have to solve that equation for d, and resolve the ambiguity due
to the fact that, for an give value of x between -1 and 1, there's two
different d's that satisfy cos(d) = x.

Notice that wrap-around it handled automatically by the fact that every
angle is used as an argument to a periodic trig function - there's no
need to worry about it explicitly.

Re: Signed integer overflow

<smgpld$358$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: bc...@freeuk.com (Bart)
Newsgroups: comp.lang.c
Subject: Re: Signed integer overflow
Date: Wed, 10 Nov 2021 15:49:05 +0000
Organization: A noiseless patient Spider
Lines: 40
Message-ID: <smgpld$358$1@dont-email.me>
References: <38ba6059-8d52-4f64-9ce6-8d23b82203f6n@googlegroups.com>
<8afeebeb-053b-4c85-821b-19437e0ba5acn@googlegroups.com>
<7c19b204-be96-4f1e-aa1c-1c96d1a34733n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 10 Nov 2021 15:49:01 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="9fcad5482fc717d5fd6593d067a5dec3";
logging-data="3240"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/0ZpwXzis8CBwERNTIdEC6JjgB+HbDJs8="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.3.0
Cancel-Lock: sha1:mln24iQVDuBB+SggndZgO6HXkEo=
In-Reply-To: <7c19b204-be96-4f1e-aa1c-1c96d1a34733n@googlegroups.com>
 by: Bart - Wed, 10 Nov 2021 15:49 UTC

On 10/11/2021 13:14, Howerd Oakford wrote:
> On Tuesday, November 9, 2021 at 4:03:48 PM UTC+1, Öö Tiib wrote:
>> On Monday, 8 November 2021 at 19:56:23 UTC+2, how...@gmail.com wrote:
>>> Hi C-folks,
>>>
>>> Following the extremely helpful responses to my last question, I would like to ask another one :
>>> I have a signed variable int32_t Latitude that represents the latitude on Earth, from -180 degrees West to 179.9999... degrees East.
>> What strange discussion you have here guys? What West what East?
>> Geodetic latitude: the angle between the normal and the equatorial plane.
>> So latitude can't be bigger than 90° (North pole) nor smaller than -90° (South pole).
>>> Since we are using an ARM64 processor and GCC cross compiler, the system is conventional two's complement.
>>>
>>> So is it OK, (with a comment to appease MISRA), to use the actual behaviour of the processor to wraparound (overflow?) from 179.99999... to -180 degrees?
>>>
>>> If not, is it OK to drop down to ARM assembler to avoid the "undefined behaviour" of C?
>>>
>>> In the actual system we have to store both latitude and longitude for several thousand points, so I don't really want to use uint64_t and manually do a 32 bit modulus...
>> Further distraction. If you need distances then converting from
>> spherical coordinates to Euclidian takes quite a trigonometry
>> and int32_t is not what one is using there. Use double.
>
> Hi Öö Tiib,
>
> My understanding is that you must scale the longitude difference by the cosine of the latitude, to first order.
> Is there something I am missing here ?

What you're missing is, how will you do such calculations using a scaled
integer value?

What type do you think cos() takes, and returns?

You will need to convert your uint32 value: scale it down by about 11.93
million, to get it to degrees, then by about 57.3 to get it to radians,
before you can pass it to cos().

The return value of cos() will be -1.0 to +1.0; a double (and maybe
negative).

You need to consider whether the considerable trouble of using a scaled
integer is worth it.

Re: Signed integer overflow

<wc_iJ.30229$Bu7.19863@fx26.iad>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!feeder1.feed.usenet.farm!feed.usenet.farm!peer01.ams4!peer.am4.highwinds-media.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx26.iad.POSTED!not-for-mail
MIME-Version: 1.0
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:91.0)
Gecko/20100101 Thunderbird/91.3.0
Subject: Re: Signed integer overflow
Content-Language: en-US
Newsgroups: comp.lang.c
References: <38ba6059-8d52-4f64-9ce6-8d23b82203f6n@googlegroups.com>
<8afeebeb-053b-4c85-821b-19437e0ba5acn@googlegroups.com>
<7c19b204-be96-4f1e-aa1c-1c96d1a34733n@googlegroups.com>
From: Rich...@Damon-Family.org (Richard Damon)
In-Reply-To: <7c19b204-be96-4f1e-aa1c-1c96d1a34733n@googlegroups.com>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Lines: 42
Message-ID: <wc_iJ.30229$Bu7.19863@fx26.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: Wed, 10 Nov 2021 20:19:24 -0500
X-Received-Bytes: 3194
 by: Richard Damon - Thu, 11 Nov 2021 01:19 UTC

On 11/10/21 8:14 AM, Howerd Oakford wrote:
> On Tuesday, November 9, 2021 at 4:03:48 PM UTC+1, Öö Tiib wrote:
>> On Monday, 8 November 2021 at 19:56:23 UTC+2, how...@gmail.com wrote:
>>> Hi C-folks,
>>>
>>> Following the extremely helpful responses to my last question, I would like to ask another one :
>>> I have a signed variable int32_t Latitude that represents the latitude on Earth, from -180 degrees West to 179.9999... degrees East.
>> What strange discussion you have here guys? What West what East?
>> Geodetic latitude: the angle between the normal and the equatorial plane.
>> So latitude can't be bigger than 90° (North pole) nor smaller than -90° (South pole).
>>> Since we are using an ARM64 processor and GCC cross compiler, the system is conventional two's complement.
>>>
>>> So is it OK, (with a comment to appease MISRA), to use the actual behaviour of the processor to wraparound (overflow?) from 179.99999... to -180 degrees?
>>>
>>> If not, is it OK to drop down to ARM assembler to avoid the "undefined behaviour" of C?
>>>
>>> In the actual system we have to store both latitude and longitude for several thousand points, so I don't really want to use uint64_t and manually do a 32 bit modulus...
>> Further distraction. If you need distances then converting from
>> spherical coordinates to Euclidian takes quite a trigonometry
>> and int32_t is not what one is using there. Use double.
>
> Hi Öö Tiib,
>
> My understanding is that you must scale the longitude difference by the cosine of the latitude, to first order.
> Is there something I am missing here ?
>
> Cheers,
> Howerd
>

No, that doesn't work.

if the two points are on the same latitude, that will give you the
length of the path that follows that line of latitude between the two
points.

If the longitude difference is significant, then there can be a
significantly shorter route that follows a 'great circle' route that
follow the real straight line between the points.

At the extreme of 180 degrees of latitude difference, the shortest line
goes straight over the pole to the other side.

Re: Signed integer overflow

<smhvu5$m6l$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: chris.m....@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c
Subject: Re: Signed integer overflow
Date: Wed, 10 Nov 2021 18:42:13 -0800
Organization: A noiseless patient Spider
Lines: 42
Message-ID: <smhvu5$m6l$1@dont-email.me>
References: <38ba6059-8d52-4f64-9ce6-8d23b82203f6n@googlegroups.com>
<8afeebeb-053b-4c85-821b-19437e0ba5acn@googlegroups.com>
<7c19b204-be96-4f1e-aa1c-1c96d1a34733n@googlegroups.com>
<smgp0g$tf6$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 11 Nov 2021 02:42:13 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="325a70a9edf8844bf46c338189657ce4";
logging-data="22741"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX193KF+twbwA22Azu+K7CqOI29ySiw3MnQ4="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.3.0
Cancel-Lock: sha1:2Rc1if5cO9jfkqAoVb0UDpLn3z4=
In-Reply-To: <smgp0g$tf6$1@dont-email.me>
Content-Language: en-US
 by: Chris M. Thomasson - Thu, 11 Nov 2021 02:42 UTC

On 11/10/2021 7:37 AM, James Kuyper wrote:
> On 11/10/21 8:14 AM, Howerd Oakford wrote:
>> On Tuesday, November 9, 2021 at 4:03:48 PM UTC+1, Öö Tiib wrote:
>>> On Monday, 8 November 2021 at 19:56:23 UTC+2, how...@gmail.com wrote:
> ...
>>>> In the actual system we have to store both latitude and longitude for several thousand points, so I don't really want to use uint64_t and manually do a 32 bit modulus...
>>> Further distraction. If you need distances then converting from
>>> spherical coordinates to Euclidian takes quite a trigonometry
>>> and int32_t is not what one is using there. Use double.
>>
>> Hi Öö Tiib,
>>
>> My understanding is that you must scale the longitude difference by the cosine of the latitude, to first order.
>> Is there something I am missing here ?
>
> If you project the two points of interest to you onto a cylinder whose
> axis coincides the with line between the north and south poles of the
> earth, that approach will give you the length of the shortest path, on
> that cylinder, between those two points. The size of that cylinder
> depends upon which of the two latitudes you are taking the cosine of.

For some reason this is reminding me of a distance estimated cylinder
using a ray marcher.

>
> If d is the length in radians of the shortest path on the surface of the
> Earth connecting those two points (ignoring the fact that the earth
> isn't spherical), and those two points have latitudes lat1 and lat2, and
> the difference in longitude between those two points is dlon, then
>
> cos(d) = sin(lat1)*sin(lat2) - cos(lat1)*cos(lat2)*cos(dlon);
>
> You'll have to solve that equation for d, and resolve the ambiguity due
> to the fact that, for an give value of x between -1 and 1, there's two
> different d's that satisfy cos(d) = x.
>
> Notice that wrap-around it handled automatically by the fact that every
> angle is used as an argument to a periodic trig function - there's no
> need to worry about it explicitly.
>

Re: Signed integer overflow

<smjeuv$be4$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c
Subject: Re: Signed integer overflow
Date: Thu, 11 Nov 2021 17:04:46 +0100
Organization: A noiseless patient Spider
Lines: 35
Message-ID: <smjeuv$be4$1@dont-email.me>
References: <38ba6059-8d52-4f64-9ce6-8d23b82203f6n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 11 Nov 2021 16:04:47 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="1a4793df1a295a3bf2da6a6c86d1fea0";
logging-data="11716"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19GNwpHqdX3y4U9+BJLWr3II4xFpLK0NF0="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.3.0
Cancel-Lock: sha1:Jw7jHHZrMXVrQU4dIgCO64HLfNo=
In-Reply-To: <38ba6059-8d52-4f64-9ce6-8d23b82203f6n@googlegroups.com>
Content-Language: de-DE
 by: Bonita Montero - Thu, 11 Nov 2021 16:04 UTC

Am 08.11.2021 um 18:56 schrieb Howerd Oakford:
> Hi C-folks,
>
> Following the extremely helpful responses to my last question, I would like to ask another one :
> I have a signed variable int32_t Latitude that represents the latitude on Earth, from -180 degrees West to 179.9999... degrees East.
>
> Since we are using an ARM64 processor and GCC cross compiler, the system is conventional two's complement.
>
> So is it OK, (with a comment to appease MISRA), to use the actual behaviour of the processor to wraparound (overflow?) from 179.99999... to -180 degrees?
>
> If not, is it OK to drop down to ARM assembler to avoid the "undefined behaviour" of C?
>
> In the actual system we have to store both latitude and longitude for several thousand points, so I don't really want to use uint64_t and manually do a 32 bit modulus...
>
> Cheers,
> Howerd

Look at this code:

bool f( int i )
{ return (int)((unsigned)i + 1u) < i;
}

Although g++ optimizes away signed overflows if the unsigned-casts would
be omitted here, the upper overflow check works. Casting negative values
to an unsigned value is UB, but you can rely on the upper code to work
because there are almost no computers today that use something different
than 2's complement values.

This is the code you will get:

cmpl $2147483647, %edi ; 0x7FFFFFFF
sete %al
ret

Re: Signed integer overflow

<ce51d395-3226-4f3d-9dd8-847c214af2ccn@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:ad4:4452:: with SMTP id l18mr10016556qvt.8.1636668428757;
Thu, 11 Nov 2021 14:07:08 -0800 (PST)
X-Received: by 2002:ae9:dc84:: with SMTP id q126mr9065755qkf.128.1636668428545;
Thu, 11 Nov 2021 14:07:08 -0800 (PST)
Path: i2pn2.org!i2pn.org!aioe.org!news.uzoreto.com!feeder1.cambriumusenet.nl!feed.tweak.nl!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.c
Date: Thu, 11 Nov 2021 14:07:08 -0800 (PST)
In-Reply-To: <smgpld$358$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=2a01:c23:74fe:cb00:34bf:8acc:6526:94a5;
posting-account=o1zNBwoAAABP2KiH5g6nPQp_sYWidVdt
NNTP-Posting-Host: 2a01:c23:74fe:cb00:34bf:8acc:6526:94a5
References: <38ba6059-8d52-4f64-9ce6-8d23b82203f6n@googlegroups.com>
<8afeebeb-053b-4c85-821b-19437e0ba5acn@googlegroups.com> <7c19b204-be96-4f1e-aa1c-1c96d1a34733n@googlegroups.com>
<smgpld$358$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <ce51d395-3226-4f3d-9dd8-847c214af2ccn@googlegroups.com>
Subject: Re: Signed integer overflow
From: howe...@gmail.com (Howerd Oakford)
Injection-Date: Thu, 11 Nov 2021 22:07:08 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
 by: Howerd Oakford - Thu, 11 Nov 2021 22:07 UTC

On Wednesday, November 10, 2021 at 4:49:14 PM UTC+1, Bart wrote:
> On 10/11/2021 13:14, Howerd Oakford wrote:
> > On Tuesday, November 9, 2021 at 4:03:48 PM UTC+1, Öö Tiib wrote:
> >> On Monday, 8 November 2021 at 19:56:23 UTC+2, how...@gmail.com wrote:
> >>> Hi C-folks,
> >>>
> >>> Following the extremely helpful responses to my last question, I would like to ask another one :
> >>> I have a signed variable int32_t Latitude that represents the latitude on Earth, from -180 degrees West to 179.9999... degrees East.
> >> What strange discussion you have here guys? What West what East?
> >> Geodetic latitude: the angle between the normal and the equatorial plane.
> >> So latitude can't be bigger than 90° (North pole) nor smaller than -90° (South pole).
> >>> Since we are using an ARM64 processor and GCC cross compiler, the system is conventional two's complement.
> >>>
> >>> So is it OK, (with a comment to appease MISRA), to use the actual behaviour of the processor to wraparound (overflow?) from 179.99999... to -180 degrees?
> >>>
> >>> If not, is it OK to drop down to ARM assembler to avoid the "undefined behaviour" of C?
> >>>
> >>> In the actual system we have to store both latitude and longitude for several thousand points, so I don't really want to use uint64_t and manually do a 32 bit modulus...
> >> Further distraction. If you need distances then converting from
> >> spherical coordinates to Euclidian takes quite a trigonometry
> >> and int32_t is not what one is using there. Use double.
> >
> > Hi Öö Tiib,
> >
> > My understanding is that you must scale the longitude difference by the cosine of the latitude, to first order.
> > Is there something I am missing here ?
> What you're missing is, how will you do such calculations using a scaled
> integer value?
>
> What type do you think cos() takes, and returns?
>
> You will need to convert your uint32 value: scale it down by about 11.93
> million, to get it to degrees, then by about 57.3 to get it to radians,
> before you can pass it to cos().
>
> The return value of cos() will be -1.0 to +1.0; a double (and maybe
> negative).
>
> You need to consider whether the considerable trouble of using a scaled
> integer is worth it.

Hi Bart,

> What you're missing is, how will you do such calculations using a scaled
> integer value?
My plan is to use Hart's approximation for cosine to create a pair of values and multiply/divide the input by them.
I have always thought that trig functions are best handled as pairs of numbers.

The application that I am working on is speed-critical, so I might use floats if it is faster on an ARM64.

Cheers,
Howerd

Re: Signed integer overflow

<9e054ef6-a2e9-4338-bc31-af86578997cdn@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a37:a8e:: with SMTP id 136mr9010674qkk.395.1636669675465;
Thu, 11 Nov 2021 14:27:55 -0800 (PST)
X-Received: by 2002:a05:622a:1a8f:: with SMTP id s15mr11452505qtc.281.1636669675292;
Thu, 11 Nov 2021 14:27:55 -0800 (PST)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!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.c
Date: Thu, 11 Nov 2021 14:27:55 -0800 (PST)
In-Reply-To: <smgp0g$tf6$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=2a01:c23:74fe:cb00:34bf:8acc:6526:94a5;
posting-account=o1zNBwoAAABP2KiH5g6nPQp_sYWidVdt
NNTP-Posting-Host: 2a01:c23:74fe:cb00:34bf:8acc:6526:94a5
References: <38ba6059-8d52-4f64-9ce6-8d23b82203f6n@googlegroups.com>
<8afeebeb-053b-4c85-821b-19437e0ba5acn@googlegroups.com> <7c19b204-be96-4f1e-aa1c-1c96d1a34733n@googlegroups.com>
<smgp0g$tf6$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <9e054ef6-a2e9-4338-bc31-af86578997cdn@googlegroups.com>
Subject: Re: Signed integer overflow
From: howe...@gmail.com (Howerd Oakford)
Injection-Date: Thu, 11 Nov 2021 22:27:55 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Lines: 70
 by: Howerd Oakford - Thu, 11 Nov 2021 22:27 UTC

On Wednesday, November 10, 2021 at 4:38:09 PM UTC+1, james...@alumni.caltech.edu wrote:
> On 11/10/21 8:14 AM, Howerd Oakford wrote:
> > On Tuesday, November 9, 2021 at 4:03:48 PM UTC+1, Öö Tiib wrote:
> >> On Monday, 8 November 2021 at 19:56:23 UTC+2, how...@gmail.com wrote:
> ...
> >>> In the actual system we have to store both latitude and longitude for several thousand points, so I don't really want to use uint64_t and manually do a 32 bit modulus...
> >> Further distraction. If you need distances then converting from
> >> spherical coordinates to Euclidian takes quite a trigonometry
> >> and int32_t is not what one is using there. Use double.
> >
> > Hi Öö Tiib,
> >
> > My understanding is that you must scale the longitude difference by the cosine of the latitude, to first order.
> > Is there something I am missing here ?
> If you project the two points of interest to you onto a cylinder whose
> axis coincides the with line between the north and south poles of the
> earth, that approach will give you the length of the shortest path, on
> that cylinder, between those two points. The size of that cylinder
> depends upon which of the two latitudes you are taking the cosine of.
>
> If d is the length in radians of the shortest path on the surface of the
> Earth connecting those two points (ignoring the fact that the earth
> isn't spherical), and those two points have latitudes lat1 and lat2, and
> the difference in longitude between those two points is dlon, then
>
> cos(d) = sin(lat1)*sin(lat2) - cos(lat1)*cos(lat2)*cos(dlon);
>
> You'll have to solve that equation for d, and resolve the ambiguity due
> to the fact that, for an give value of x between -1 and 1, there's two
> different d's that satisfy cos(d) = x.
>
> Notice that wrap-around it handled automatically by the fact that every
> angle is used as an argument to a periodic trig function - there's no
> need to worry about it explicitly.

Hi James,

The task is to calculate the linear distance between two points given their latitude and longitude, over small distances.

I am only interested in a very limited range (a few 100's of metres), on the Earth's surface.

At this scale, I think, that you can map the polar surface to a flat surface, and convert the longitude distance to linear distance using the latitude..
The latitude difference, dLat in degrees, maps to the length of the straight line distance between the two points :
dX = dLat * ( Earth's_Circumference / 360 )
The longitude difference, dLon in degrees, is the same except, that as the latitude gets higher the circle round the Earth gets smaller, so must be scaled down :
dY = dLon * ( Earth's_Circumference / 360 ) * cosine( Lat )
( I wish I could draw a picture here - it would save a thousand words ;-)

LinearDistance = sqrt( (dX*dX) + (dY*dY ) ) // Pythagorous

Thanks for your response,

Cheers,
Howerd

Re: Signed integer overflow

<3159cb4f-606e-4c11-871f-939828991638n@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:622a:54f:: with SMTP id m15mr11416632qtx.365.1636669832156;
Thu, 11 Nov 2021 14:30:32 -0800 (PST)
X-Received: by 2002:ac8:4159:: with SMTP id e25mr11249432qtm.69.1636669831930;
Thu, 11 Nov 2021 14:30:31 -0800 (PST)
Path: i2pn2.org!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.c
Date: Thu, 11 Nov 2021 14:30:31 -0800 (PST)
In-Reply-To: <wc_iJ.30229$Bu7.19863@fx26.iad>
Injection-Info: google-groups.googlegroups.com; posting-host=2a01:c23:74fe:cb00:34bf:8acc:6526:94a5;
posting-account=o1zNBwoAAABP2KiH5g6nPQp_sYWidVdt
NNTP-Posting-Host: 2a01:c23:74fe:cb00:34bf:8acc:6526:94a5
References: <38ba6059-8d52-4f64-9ce6-8d23b82203f6n@googlegroups.com>
<8afeebeb-053b-4c85-821b-19437e0ba5acn@googlegroups.com> <7c19b204-be96-4f1e-aa1c-1c96d1a34733n@googlegroups.com>
<wc_iJ.30229$Bu7.19863@fx26.iad>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <3159cb4f-606e-4c11-871f-939828991638n@googlegroups.com>
Subject: Re: Signed integer overflow
From: howe...@gmail.com (Howerd Oakford)
Injection-Date: Thu, 11 Nov 2021 22:30:32 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
 by: Howerd Oakford - Thu, 11 Nov 2021 22:30 UTC

On Thursday, November 11, 2021 at 2:19:39 AM UTC+1, Richard Damon wrote:
> On 11/10/21 8:14 AM, Howerd Oakford wrote:
> > On Tuesday, November 9, 2021 at 4:03:48 PM UTC+1, Öö Tiib wrote:
> >> On Monday, 8 November 2021 at 19:56:23 UTC+2, how...@gmail.com wrote:
> >>> Hi C-folks,
> >>>
> >>> Following the extremely helpful responses to my last question, I would like to ask another one :
> >>> I have a signed variable int32_t Latitude that represents the latitude on Earth, from -180 degrees West to 179.9999... degrees East.
> >> What strange discussion you have here guys? What West what East?
> >> Geodetic latitude: the angle between the normal and the equatorial plane.
> >> So latitude can't be bigger than 90° (North pole) nor smaller than -90° (South pole).
> >>> Since we are using an ARM64 processor and GCC cross compiler, the system is conventional two's complement.
> >>>
> >>> So is it OK, (with a comment to appease MISRA), to use the actual behaviour of the processor to wraparound (overflow?) from 179.99999... to -180 degrees?
> >>>
> >>> If not, is it OK to drop down to ARM assembler to avoid the "undefined behaviour" of C?
> >>>
> >>> In the actual system we have to store both latitude and longitude for several thousand points, so I don't really want to use uint64_t and manually do a 32 bit modulus...
> >> Further distraction. If you need distances then converting from
> >> spherical coordinates to Euclidian takes quite a trigonometry
> >> and int32_t is not what one is using there. Use double.
> >
> > Hi Öö Tiib,
> >
> > My understanding is that you must scale the longitude difference by the cosine of the latitude, to first order.
> > Is there something I am missing here ?
> >
> > Cheers,
> > Howerd
> >
> No, that doesn't work.
>
> if the two points are on the same latitude, that will give you the
> length of the path that follows that line of latitude between the two
> points.
>
> If the longitude difference is significant, then there can be a
> significantly shorter route that follows a 'great circle' route that
> follow the real straight line between the points.
>
> At the extreme of 180 degrees of latitude difference, the shortest line
> goes straight over the pole to the other side.

Hi Richard,

Sorry - I didn't make it clear that this only has to work over a range of a few 100 metres!

Cheers,
Howerd

Re: Signed integer overflow

<smk6pl$bup$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: lew.pitc...@digitalfreehold.ca (Lew Pitcher)
Newsgroups: comp.lang.c
Subject: Re: Signed integer overflow
Date: Thu, 11 Nov 2021 22:51:33 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 33
Message-ID: <smk6pl$bup$1@dont-email.me>
References: <38ba6059-8d52-4f64-9ce6-8d23b82203f6n@googlegroups.com>
<8afeebeb-053b-4c85-821b-19437e0ba5acn@googlegroups.com>
<7c19b204-be96-4f1e-aa1c-1c96d1a34733n@googlegroups.com>
<wc_iJ.30229$Bu7.19863@fx26.iad>
<3159cb4f-606e-4c11-871f-939828991638n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 11 Nov 2021 22:51:33 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="f3e15017d5b75a1cffd6817e737e1639";
logging-data="12249"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+j73Sp0zQ9MEB5fGcF5mzmNvng265+3MU="
User-Agent: Pan/0.139 (Sexual Chocolate; GIT bf56508
git://git.gnome.org/pan2)
Cancel-Lock: sha1:LDJ5wH2jD04pVf/lTvXeFjz/Ujw=
 by: Lew Pitcher - Thu, 11 Nov 2021 22:51 UTC

On Thu, 11 Nov 2021 14:30:31 -0800, Howerd Oakford wrote:

> On Thursday, November 11, 2021 at 2:19:39 AM UTC+1, Richard Damon wrote:
[snip]
>> If the longitude difference is significant, then there can be a
>> significantly shorter route that follows a 'great circle' route that
>> follow the real straight line between the points.
>>
>> At the extreme of 180 degrees of latitude difference, the shortest line
>> goes straight over the pole to the other side.
>
> Hi Richard,
>
> Sorry - I didn't make it clear that this only has to work over a range
> of a few 100 metres!
>
> Cheers,
> Howerd

Have you looked at haversines yet?
(https://en.wikipedia.org/wiki/Haversine_formula
See also:
https://www.movable-type.co.uk/scripts/gis-faq-5.1.html
http://mathforum.org/library/drmath/view/51879.html
https://rosettacode.org/wiki/Haversine_formula
)

"The haversine formula determines the great-circle distance between two
points on a sphere given their longitudes and latitudes."

--
Lew Pitcher
"In Skills, We Trust"

Re: Signed integer overflow

<WBhjJ.72821$g35.71924@fx11.iad>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!news.uzoreto.com!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!peer02.ams4!peer.am4.highwinds-media.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx11.iad.POSTED!not-for-mail
MIME-Version: 1.0
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:91.0)
Gecko/20100101 Thunderbird/91.3.0
Subject: Re: Signed integer overflow
Content-Language: en-US
Newsgroups: comp.lang.c
References: <38ba6059-8d52-4f64-9ce6-8d23b82203f6n@googlegroups.com>
<8afeebeb-053b-4c85-821b-19437e0ba5acn@googlegroups.com>
<7c19b204-be96-4f1e-aa1c-1c96d1a34733n@googlegroups.com>
<wc_iJ.30229$Bu7.19863@fx26.iad>
<3159cb4f-606e-4c11-871f-939828991638n@googlegroups.com>
From: Rich...@Damon-Family.org (Richard Damon)
In-Reply-To: <3159cb4f-606e-4c11-871f-939828991638n@googlegroups.com>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Lines: 58
Message-ID: <WBhjJ.72821$g35.71924@fx11.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: Thu, 11 Nov 2021 18:23:34 -0500
X-Received-Bytes: 4026
 by: Richard Damon - Thu, 11 Nov 2021 23:23 UTC

On 11/11/21 5:30 PM, Howerd Oakford wrote:
> On Thursday, November 11, 2021 at 2:19:39 AM UTC+1, Richard Damon wrote:
>> On 11/10/21 8:14 AM, Howerd Oakford wrote:
>>> On Tuesday, November 9, 2021 at 4:03:48 PM UTC+1, Öö Tiib wrote:
>>>> On Monday, 8 November 2021 at 19:56:23 UTC+2, how...@gmail.com wrote:
>>>>> Hi C-folks,
>>>>>
>>>>> Following the extremely helpful responses to my last question, I would like to ask another one :
>>>>> I have a signed variable int32_t Latitude that represents the latitude on Earth, from -180 degrees West to 179.9999... degrees East.
>>>> What strange discussion you have here guys? What West what East?
>>>> Geodetic latitude: the angle between the normal and the equatorial plane.
>>>> So latitude can't be bigger than 90° (North pole) nor smaller than -90° (South pole).
>>>>> Since we are using an ARM64 processor and GCC cross compiler, the system is conventional two's complement.
>>>>>
>>>>> So is it OK, (with a comment to appease MISRA), to use the actual behaviour of the processor to wraparound (overflow?) from 179.99999... to -180 degrees?
>>>>>
>>>>> If not, is it OK to drop down to ARM assembler to avoid the "undefined behaviour" of C?
>>>>>
>>>>> In the actual system we have to store both latitude and longitude for several thousand points, so I don't really want to use uint64_t and manually do a 32 bit modulus...
>>>> Further distraction. If you need distances then converting from
>>>> spherical coordinates to Euclidian takes quite a trigonometry
>>>> and int32_t is not what one is using there. Use double.
>>>
>>> Hi Öö Tiib,
>>>
>>> My understanding is that you must scale the longitude difference by the cosine of the latitude, to first order.
>>> Is there something I am missing here ?
>>>
>>> Cheers,
>>> Howerd
>>>
>> No, that doesn't work.
>>
>> if the two points are on the same latitude, that will give you the
>> length of the path that follows that line of latitude between the two
>> points.
>>
>> If the longitude difference is significant, then there can be a
>> significantly shorter route that follows a 'great circle' route that
>> follow the real straight line between the points.
>>
>> At the extreme of 180 degrees of latitude difference, the shortest line
>> goes straight over the pole to the other side.
>
> Hi Richard,
>
> Sorry - I didn't make it clear that this only has to work over a range of a few 100 metres!
>
> Cheers,
> Howerd
>

At ranges of a few 100 meters then the simple formula works (unless you
get very near the poles), but be warned at that scale you start to have
to worry about the shape of your reference Spheroid and reference marks.
Yes at the local level it isn't that important, but getting the numbers
in fhe first place you need to make sure everything is using the same
reference system.

Re: Signed integer overflow

<sn66oj$2r2$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c
Subject: Re: Signed integer overflow
Date: Thu, 18 Nov 2021 19:41:22 +0100
Organization: A noiseless patient Spider
Lines: 433
Message-ID: <sn66oj$2r2$1@dont-email.me>
References: <38ba6059-8d52-4f64-9ce6-8d23b82203f6n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 18 Nov 2021 18:41:23 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="712aa335b59025a8f2c81380e0414c5b";
logging-data="2914"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18lRYd80RMd4gB02eBJQKgCOT4WRDNHLjk="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.3.1
Cancel-Lock: sha1:zhwQrL3Ohl3t4cHqOLtfiwyIyyg=
In-Reply-To: <38ba6059-8d52-4f64-9ce6-8d23b82203f6n@googlegroups.com>
Content-Language: de-DE
 by: Bonita Montero - Thu, 18 Nov 2021 18:41 UTC

If you like C++: this is a C++20-class that takes a signed integral tem-
plate-parameter and if you do some arithmetic operations on it and do a
comparison afterwards, the comparison is done in every case, i.e. the
compiler doesn't optimize away overflows:

#include <iostream>
#include <random>
#include "sgi.h"

using namespace std;

short rnd();

int main()
{ using sgis = sgi<short>;
sgis a( rnd() ), b( rnd() );
bool f = a + b < a;
cout << f << endl;
}

#if defined(_MSC_VER)
__declspec(noinline)
#elif defined(__GNUC__)
__attribute((noinline))
#endif
short rnd()
{ static mt19937_64 mt( (random_device())() );
static uniform_int_distribution<short> uid( -10, +10 );
return uid( mt );
}

This generates the following code:

call "?rnd@@YAFXZ"
mov esi, eax
call "?rnd@@YAFXZ"
lea ecx, [rax + rsi]
cmp cx, si
setl cl
test ax, ax
setne dl
and dl, cl

This is the class for sgi:

#pragma once
#include <type_traits>
#include <concepts>
#include <compare>

template<typename T>
concept sgi_concept = std::is_integral_v<T> && std::is_signed_v<T>;

template<typename T>
requires sgi_concept<T>
struct sgi
{ sgi() = default;
template<typename T2>
requires sgi_concept<T2>
sgi( T2 value );
template<typename T2>
sgi( sgi<T2> const &other );
operator T();
template<typename T2>
sgi &operator |=( sgi<T2> const &other );
template<typename T2>
sgi &operator ^=( sgi<T2> const &other );
template<typename T2>
sgi &operator &=( sgi<T2> const &other );
sgi &operator >>=( unsigned shift );
sgi &operator <<=( unsigned shift );
template<typename T2>
sgi &operator %=( sgi<T2> const &other );
template<typename T2>
sgi &operator /=( sgi<T2> const &other );
template<typename T2>
sgi &operator *=( sgi<T2> const &other );
template<typename T2>
sgi &operator -=( sgi<T2> const &other);
template<typename T2>
sgi &operator +=( sgi<T2> const &other );
template<typename T2>
sgi &operator =( sgi<T2> const &other );
template<typename T2>
requires sgi_concept<T2>
sgi &operator =( T2 value );
template<typename T1, typename T2>
friend bool operator ||( sgi<T1> const &left, sgi<T2> const &right );
template<typename T1, typename T2>
friend bool operator &&( sgi<T1> const &left, sgi<T2> const &right );
template<typename T1, typename T2>
friend sgi<std::conditional_t<sizeof(T1) >= sizeof(T2), T1, T2>>
operator |( sgi<T1> const &left, sgi<T2> const &right );
template<typename T1, typename T2>
friend sgi<std::conditional_t<sizeof(T1) >= sizeof(T2), T1, T2>>
operator ^( sgi<T1> const &left, sgi<T2> const &right );
template<typename T1, typename T2>
friend sgi<std::conditional_t<sizeof(T1) >= sizeof(T2), T1, T2>>
operator &( sgi<T1> const &left, sgi<T2> const &right );
template<typename T1, typename T2>
friend bool operator !=( sgi<T1> const &left, sgi<T2> const &right );
template<typename T1, typename T2>
friend std::strong_ordering operator <=>( sgi<T1> const &left, sgi<T2>
const &right );
template<typename T2>
friend sgi<T2> operator <<( sgi<T2> const &left, unsigned shift );
template<typename T2>
friend sgi<T2> operator >>( sgi<T> const &left, unsigned shift );
template<typename T1, typename T2>
friend sgi<std::conditional_t<sizeof(T1) >= sizeof(T2), T1, T2>>
operator -( sgi<T1> const &left, sgi<T2> const &right );
template<typename T1, typename T2>
friend sgi<std::conditional_t<sizeof(T1) >= sizeof(T2), T1, T2>>
operator +( sgi<T1> const &left, sgi<T2> const &right );
template<typename T1, typename T2>
friend sgi<std::conditional_t<sizeof(T1) >= sizeof(T2), T1, T2>>
operator %( sgi<T1> const &left, sgi<T2> const &right );
template<typename T1, typename T2>
friend sgi<std::conditional_t<sizeof(T1) >= sizeof(T2), T1, T2>>
operator /( sgi<T1> const &left, sgi<T2> const &right );
template<typename T1, typename T2>
friend sgi<std::conditional_t<sizeof(T1) >= sizeof(T2), T1, T2>>
operator *( sgi<T1> const &left, sgi<T2> const &right );
sgi<T> &operator --();
sgi<T> &operator ++();
sgi<T> operator --( int );
sgi<T> operator ++( int );
private:
using TU = std::make_unsigned_t<T>;
TU value;
};

template<typename T>
requires sgi_concept<T>
template<typename T2>
inline
sgi<T>::sgi( sgi<T2> const &other ) :
value( (T)(T2)other.value )
{ }

template<typename T>
requires sgi_concept<T>
template<typename T2>
requires sgi_concept<T2>
inline
sgi<T>::sgi( T2 value ) :
value( (T)value )
{ }

template<typename T>
requires sgi_concept<T>
inline
sgi<T>::operator T()
{ return value;
}

template<typename T>
requires sgi_concept<T>
template<typename T2>
inline
sgi<T> &sgi<T>::operator |=( sgi<T2> const &value )
{ value |= (T)(T2)value.value;
return *this;
}

template<typename T>
requires sgi_concept<T>
template<typename T2>
inline
sgi<T> &sgi<T>::operator ^=( sgi<T2> const &value )
{ value ^= (T)(T2)value.value;
return *this;
}

template<typename T>
requires sgi_concept<T>
template<typename T2>
inline
sgi<T> &sgi<T>::operator &=( sgi<T2> const &value )
{ value &= (T)(T2)value.value;
return *this;
}

template<typename T>
requires sgi_concept<T>
inline
sgi<T> &sgi<T>::operator >>=( unsigned shift )
{ value = (T)value >> shift;
return *this;
}

template<typename T>
requires sgi_concept<T>
inline
sgi<T> &sgi<T>::operator <<=( unsigned shift )
{ value = (T)value << shift;
return *this;
}

template<typename T>
requires sgi_concept<T>
template<typename T2>
inline
sgi<T> &sgi<T>::operator %=( sgi<T2> const &other )
{ value = (T)((T)value % (T2)other.value);
return *this;
}

template<typename T>
requires sgi_concept<T>
template<typename T2>
inline
sgi<T> &sgi<T>::operator /=( sgi<T2> const &other )
{ value = (T)((T)value / (T2)other.value);
return *this;
}

template<typename T>
requires sgi_concept<T>
template<typename T2>
inline
sgi<T> &sgi<T>::operator *=( sgi<T2> const &other )
{ value = (T)((T)value * (T2)other.value);
return *this;
}

template<typename T>
requires sgi_concept<T>
template<typename T2>
inline
sgi<T> &sgi<T>::operator -=( sgi<T2> const &other )
{ value -= (T)(T2)other.value;
return *this;
}

template<typename T>
requires sgi_concept<T>
template<typename T2>
inline
sgi<T> &sgi<T>::operator +=( sgi<T2> const &other )
{ value += (T)(T2)other.value;
return *this;
}

template<typename T>
requires sgi_concept<T>
template<typename T2>
inline
sgi<T> &sgi<T>::operator =( sgi<T2> const &other )
{ value = (T)(T2)other.value;
return *this;
}

template<typename T>
requires sgi_concept<T>
template<typename T2>
requires sgi_concept<T2>
inline
sgi<T> &sgi<T>::operator =( T2 value )
{ this->value = (T)value;
return *this;
}

template<typename T1, typename T2>
inline
bool operator ||( sgi<T1> const &left, sgi<T2> const &right )
{ return left.value || right.value;
}

template<typename T1, typename T2>
inline
bool operator &&( sgi<T1> const &left, sgi<T2> const &right )
{ return left.value && right.value;
}

template<typename T1, typename T2>
inline
sgi<std::conditional_t<sizeof(T1) >= sizeof(T2), T1, T2>> operator |(
sgi<T1> const &left, sgi<T2> const &right )
{ return sgi<std::conditional_t<sizeof(T1) >= sizeof(T2), T1, T2>>(
(T1)left.value | (T2)right.value );
}


Click here to read the complete article
Re: Signed integer overflow

<sn68lf$fvf$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c
Subject: Re: Signed integer overflow
Date: Thu, 18 Nov 2021 20:13:51 +0100
Organization: A noiseless patient Spider
Lines: 422
Message-ID: <sn68lf$fvf$1@dont-email.me>
References: <38ba6059-8d52-4f64-9ce6-8d23b82203f6n@googlegroups.com>
<sn66oj$2r2$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 18 Nov 2021 19:13:52 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="712aa335b59025a8f2c81380e0414c5b";
logging-data="16367"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+hmTHrrxAIvU1lpBz8/z9V1io/mqhIE7E="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.3.1
Cancel-Lock: sha1:dzIJ0sF3fhNz66Ojvv+nj09i29M=
In-Reply-To: <sn66oj$2r2$1@dont-email.me>
Content-Language: de-DE
 by: Bonita Montero - Thu, 18 Nov 2021 19:13 UTC

The three way comparison operator is poorly optimizesd, so I implemented
>=, >, <= and < myself. This results in the followging code:

call "?rnd@@YAFXZ"
mov esi, eax
call "?rnd@@YAFXZ"
add eax, esi
cmp ax, si
setl dl

#pragma once
#include <type_traits>
#include <concepts>
#include <compare>

template<typename T>
concept sgi_concept = std::is_integral_v<T> && std::is_signed_v<T>;

template<typename T>
requires sgi_concept<T>
struct sgi
{ sgi() = default;
template<typename T2>
requires sgi_concept<T2>
sgi( T2 value );
template<typename T2>
sgi( sgi<T2> const &other );
operator T();
template<typename T2>
sgi &operator |=( sgi<T2> const &other );
template<typename T2>
sgi &operator ^=( sgi<T2> const &other );
template<typename T2>
sgi &operator &=( sgi<T2> const &other );
sgi &operator >>=( unsigned shift );
sgi &operator <<=( unsigned shift );
template<typename T2>
sgi &operator %=( sgi<T2> const &other );
template<typename T2>
sgi &operator /=( sgi<T2> const &other );
template<typename T2>
sgi &operator *=( sgi<T2> const &other );
template<typename T2>
sgi &operator -=( sgi<T2> const &other);
template<typename T2>
sgi &operator +=( sgi<T2> const &other );
template<typename T2>
sgi &operator =( sgi<T2> const &other );
template<typename T2>
requires sgi_concept<T2>
sgi &operator =( T2 value );
template<typename T1, typename T2>
friend bool operator ||( sgi<T1> const &left, sgi<T2> const &right );
template<typename T1, typename T2>
friend bool operator &&( sgi<T1> const &left, sgi<T2> const &right );
template<typename T1, typename T2>
friend sgi<std::conditional_t<sizeof(T1) >= sizeof(T2), T1, T2>>
operator |( sgi<T1> const &left, sgi<T2> const &right );
template<typename T1, typename T2>
friend sgi<std::conditional_t<sizeof(T1) >= sizeof(T2), T1, T2>>
operator ^( sgi<T1> const &left, sgi<T2> const &right );
template<typename T1, typename T2>
friend sgi<std::conditional_t<sizeof(T1) >= sizeof(T2), T1, T2>>
operator &( sgi<T1> const &left, sgi<T2> const &right );
template<typename T1, typename T2>
friend bool operator !=( sgi<T1> const &left, sgi<T2> const &right );
template<typename T1, typename T2>
friend bool operator >=( sgi<T1> const &left, sgi<T2> const &right );
template<typename T1, typename T2>
friend bool operator >( sgi<T1> const &left, sgi<T2> const &right );
template<typename T1, typename T2>
friend bool operator <=( sgi<T1> const &left, sgi<T2> const &right );
template<typename T1, typename T2>
friend bool operator <( sgi<T1> const &left, sgi<T2> const &right );
template<typename T2>
friend sgi<T2> operator <<( sgi<T2> const &left, unsigned shift );
template<typename T2>
friend sgi<T2> operator >>( sgi<T> const &left, unsigned shift );
template<typename T1, typename T2>
friend sgi<std::conditional_t<sizeof(T1) >= sizeof(T2), T1, T2>>
operator -( sgi<T1> const &left, sgi<T2> const &right );
template<typename T1, typename T2>
friend sgi<std::conditional_t<sizeof(T1) >= sizeof(T2), T1, T2>>
operator +( sgi<T1> const &left, sgi<T2> const &right );
template<typename T1, typename T2>
friend sgi<std::conditional_t<sizeof(T1) >= sizeof(T2), T1, T2>>
operator %( sgi<T1> const &left, sgi<T2> const &right );
template<typename T1, typename T2>
friend sgi<std::conditional_t<sizeof(T1) >= sizeof(T2), T1, T2>>
operator /( sgi<T1> const &left, sgi<T2> const &right );
template<typename T1, typename T2>
friend sgi<std::conditional_t<sizeof(T1) >= sizeof(T2), T1, T2>>
operator *( sgi<T1> const &left, sgi<T2> const &right );
sgi<T> &operator --();
sgi<T> &operator ++();
sgi<T> operator --( int );
sgi<T> operator ++( int );
private:
using TU = std::make_unsigned_t<T>;
TU value;
};

template<typename T>
requires sgi_concept<T>
template<typename T2>
inline
sgi<T>::sgi( sgi<T2> const &other ) :
value( (T)(T2)other.value )
{ }

template<typename T>
requires sgi_concept<T>
template<typename T2>
requires sgi_concept<T2>
inline
sgi<T>::sgi( T2 value ) :
value( (T)value )
{ }

template<typename T>
requires sgi_concept<T>
inline
sgi<T>::operator T()
{ return value;
}

template<typename T>
requires sgi_concept<T>
template<typename T2>
inline
sgi<T> &sgi<T>::operator |=( sgi<T2> const &value )
{ value |= (T)(T2)value.value;
return *this;
}

template<typename T>
requires sgi_concept<T>
template<typename T2>
inline
sgi<T> &sgi<T>::operator ^=( sgi<T2> const &value )
{ value ^= (T)(T2)value.value;
return *this;
}

template<typename T>
requires sgi_concept<T>
template<typename T2>
inline
sgi<T> &sgi<T>::operator &=( sgi<T2> const &value )
{ value &= (T)(T2)value.value;
return *this;
}

template<typename T>
requires sgi_concept<T>
inline
sgi<T> &sgi<T>::operator >>=( unsigned shift )
{ value = (T)value >> shift;
return *this;
}

template<typename T>
requires sgi_concept<T>
inline
sgi<T> &sgi<T>::operator <<=( unsigned shift )
{ value = (T)value << shift;
return *this;
}

template<typename T>
requires sgi_concept<T>
template<typename T2>
inline
sgi<T> &sgi<T>::operator %=( sgi<T2> const &other )
{ value = (T)((T)value % (T2)other.value);
return *this;
}

template<typename T>
requires sgi_concept<T>
template<typename T2>
inline
sgi<T> &sgi<T>::operator /=( sgi<T2> const &other )
{ value = (T)((T)value / (T2)other.value);
return *this;
}

template<typename T>
requires sgi_concept<T>
template<typename T2>
inline
sgi<T> &sgi<T>::operator *=( sgi<T2> const &other )
{ value = (T)((T)value * (T2)other.value);
return *this;
}

template<typename T>
requires sgi_concept<T>
template<typename T2>
inline
sgi<T> &sgi<T>::operator -=( sgi<T2> const &other )
{ value -= (T)(T2)other.value;
return *this;
}

template<typename T>
requires sgi_concept<T>
template<typename T2>
inline
sgi<T> &sgi<T>::operator +=( sgi<T2> const &other )
{ value += (T)(T2)other.value;
return *this;
}

template<typename T>
requires sgi_concept<T>
template<typename T2>
inline
sgi<T> &sgi<T>::operator =( sgi<T2> const &other )
{ value = (T)(T2)other.value;
return *this;
}

template<typename T>
requires sgi_concept<T>
template<typename T2>
requires sgi_concept<T2>
inline
sgi<T> &sgi<T>::operator =( T2 value )
{ this->value = (T)value;
return *this;
}

template<typename T1, typename T2>
inline
bool operator ||( sgi<T1> const &left, sgi<T2> const &right )
{ return left.value || right.value;
}

template<typename T1, typename T2>
inline
bool operator &&( sgi<T1> const &left, sgi<T2> const &right )
{ return left.value && right.value;
}

template<typename T1, typename T2>
inline
sgi<std::conditional_t<sizeof(T1) >= sizeof(T2), T1, T2>> operator |(
sgi<T1> const &left, sgi<T2> const &right )
{ return sgi<std::conditional_t<sizeof(T1) >= sizeof(T2), T1, T2>>(
(T1)left.value | (T2)right.value );
}

template<typename T1, typename T2>
inline
sgi<std::conditional_t<sizeof(T1) >= sizeof(T2), T1, T2>> operator ^(
sgi<T1> const &left, sgi<T2> const &right )
{ return sgi<std::conditional_t<sizeof(T1) >= sizeof(T2), T1, T2>>(
(T1)left.value ^ (T2)right.value );
}


Click here to read the complete article
Re: Signed integer overflow

<20211119134908.06e833c73097aae5f70ff11c@g{oogle}mail.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: anton....@g{oogle}mail.com (Anton Shepelev)
Newsgroups: comp.lang.c
Subject: Re: Signed integer overflow
Date: Fri, 19 Nov 2021 13:49:08 +0300
Organization: A noiseless patient Spider
Lines: 26
Message-ID: <20211119134908.06e833c73097aae5f70ff11c@g{oogle}mail.com>
References: <38ba6059-8d52-4f64-9ce6-8d23b82203f6n@googlegroups.com>
<2leiJ.15504$SW5.13829@fx45.iad>
<e716ee2e-e1ec-483e-a85d-ce1e3bb708d9n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Injection-Info: reader02.eternal-september.org; posting-host="a43323d89f99ea21a2a40a4dba423bb5";
logging-data="903"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/2dzTQregBQN86a2jFLueobpwWVV9FZCI="
Cancel-Lock: sha1:za62an0dUuxSBr8zf7KZAu2MLpk=
X-Newsreader: Sylpheed 3.5.0 (GTK+ 2.24.23; i686-pc-mingw32)
 by: Anton Shepelev - Fri, 19 Nov 2021 10:49 UTC

Howerd Oakford to Richard Damon:

> > I presume you are scaling the numbers so 179.999 degrees
> > would be 0x7FFFFFFF and -180 would be 0x80000000
>
> Exactly.

Why not store latitude and longitude in the `float' or
`double' type as degrees (or radians), and wrap tham explic-
itly:

lat_add( float a, float b )
{ const float MAG = 180.0;
float res;
res = a + b;
if( res >= MAG ) res -= 2*MAG;
else if( res < MAG ) res += 2*MAG;
return res;
}

Is it for reasons of optimisation that you use normalised
integers instead?

--
() ascii ribbon campaign - against html e-mail
/\ http://preview.tinyurl.com/qcy6mjc [archived]

Re: Signed integer overflow

<20211119142253.2a5dbe22aee87e4616e7f94c@g{oogle}mail.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: anton....@g{oogle}mail.com (Anton Shepelev)
Newsgroups: comp.lang.c
Subject: Re: Signed integer overflow
Date: Fri, 19 Nov 2021 14:22:53 +0300
Organization: A noiseless patient Spider
Lines: 19
Message-ID: <20211119142253.2a5dbe22aee87e4616e7f94c@g{oogle}mail.com>
References: <38ba6059-8d52-4f64-9ce6-8d23b82203f6n@googlegroups.com>
<10b5cc02-a50f-4b20-b651-d7fabe4af9f3n@googlegroups.com>
<7d185487-109c-4359-a44f-ae209f95285an@googlegroups.com>
<smccls$if3$1@dont-email.me>
<90a0bfb9-6af3-4ca6-8380-7cfe21a9dc49n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Injection-Info: reader02.eternal-september.org; posting-host="a43323d89f99ea21a2a40a4dba423bb5";
logging-data="903"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/zQXhJNTk5iVSYRRgZ2zUDF1iVGR38098="
Cancel-Lock: sha1:G01C4TKz6Au4OdEttQwSWDJr8XM=
X-Newsreader: Sylpheed 3.5.0 (GTK+ 2.24.23; i686-pc-mingw32)
 by: Anton Shepelev - Fri, 19 Nov 2021 11:22 UTC

Howerd Oakford:

> Because we receive the data as a string in the form
> "123456789" or "-123456789" , and the Schema for the data
> format refers to this as an int32_t. So naturally, I con-
> verted it to an int32_t.

On the contrary -- it is unnatural, because data-exchage
format serves a purpose different from that of the corre-
sponding data structure in the program. Most of the time,
the best approach is to convert the data to whatver internal
representation is most convenient for the task at hand. Re-
liance on low-level facilities instead of explicit logic
makes the program less readable and less portable (including
to other languages!).

--
() ascii ribbon campaign - against html e-mail
/\ http://preview.tinyurl.com/qcy6mjc [archived]

Re: Signed integer overflow

<20211119143554.c7e435fb38ce7deabba82058@g{oogle}mail.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: anton....@g{oogle}mail.com (Anton Shepelev)
Newsgroups: comp.lang.c
Subject: Re: Signed integer overflow
Date: Fri, 19 Nov 2021 14:35:54 +0300
Organization: A noiseless patient Spider
Lines: 17
Message-ID: <20211119143554.c7e435fb38ce7deabba82058@g{oogle}mail.com>
References: <38ba6059-8d52-4f64-9ce6-8d23b82203f6n@googlegroups.com>
<8afeebeb-053b-4c85-821b-19437e0ba5acn@googlegroups.com>
<7c19b204-be96-4f1e-aa1c-1c96d1a34733n@googlegroups.com>
<smgpld$358$1@dont-email.me>
<ce51d395-3226-4f3d-9dd8-847c214af2ccn@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Injection-Info: reader02.eternal-september.org; posting-host="a43323d89f99ea21a2a40a4dba423bb5";
logging-data="903"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/FwnAhoAn6bHcIqpgHnX+1I0H3tR0awa0="
Cancel-Lock: sha1:m2lobk18oCBYXzNx8HrfMo5rsP0=
X-Newsreader: Sylpheed 3.5.0 (GTK+ 2.24.23; i686-pc-mingw32)
 by: Anton Shepelev - Fri, 19 Nov 2021 11:35 UTC

Howerd Oakford:

> The application that I am working on is speed-critical

I recommend the following approach:

1. Create the simplest and easist implementation that you

can think of.
2. If measurements show it too slow, run it in the profi-
er and determine the narrowest bottleneck.

3. Optimise that away, and repeat.

--
() ascii ribbon campaign - against html e-mail
/\ http://preview.tinyurl.com/qcy6mjc [archived]

Re: Signed integer overflow

<sn8cg9$ics$1@gioia.aioe.org>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!Puiiztk9lHEEQC0y3uUjRA.user.46.165.242.75.POSTED!not-for-mail
From: non...@add.invalid (Manfred)
Newsgroups: comp.lang.c
Subject: Re: Signed integer overflow
Date: Fri, 19 Nov 2021 15:31:37 +0100
Organization: Aioe.org NNTP Server
Message-ID: <sn8cg9$ics$1@gioia.aioe.org>
References: <38ba6059-8d52-4f64-9ce6-8d23b82203f6n@googlegroups.com>
<2leiJ.15504$SW5.13829@fx45.iad>
<e716ee2e-e1ec-483e-a85d-ce1e3bb708d9n@googlegroups.com>
<20211119134908.06e833c73097aae5f70ff11c@g{oogle}mail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="18844"; posting-host="Puiiztk9lHEEQC0y3uUjRA.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.3.1
Content-Language: en-US
X-Notice: Filtered by postfilter v. 0.9.2
 by: Manfred - Fri, 19 Nov 2021 14:31 UTC

On 11/19/2021 11:49 AM, Anton Shepelev wrote:
> Howerd Oakford to Richard Damon:
>
>>> I presume you are scaling the numbers so 179.999 degrees
>>> would be 0x7FFFFFFF and -180 would be 0x80000000
>>
>> Exactly.
>
> Why not store latitude and longitude in the `float' or
> `double' type as degrees (or radians), and wrap tham explic-
> itly:
>
> lat_add( float a, float b )
> { const float MAG = 180.0;
> float res;
> res = a + b;
> if( res >= MAG ) res -= 2*MAG;
> else if( res < MAG ) res += 2*MAG;
> return res;
> }
>
> Is it for reasons of optimisation that you use normalised
> integers instead?
>

Or because it requires extra care:

> else if( res < MAG ) res += 2*MAG;

doesn't work, you need:

else if( res < -MAG ) res += 2*MAG;

You also have to consider rounding errors around +/-MAG.

Integer arithmetic has less kinks than floating point, there are
circumstances where it is preferable.
I am not saying that it is the good choice here, in fact I think there
is just not enough information in what the OP has given to give a
definite answer.

Re: Signed integer overflow

<snb2ai$s9o$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c
Subject: Re: Signed integer overflow
Date: Sat, 20 Nov 2021 15:56:18 +0100
Organization: A noiseless patient Spider
Lines: 843
Message-ID: <snb2ai$s9o$1@dont-email.me>
References: <38ba6059-8d52-4f64-9ce6-8d23b82203f6n@googlegroups.com>
<sn66oj$2r2$1@dont-email.me> <sn68lf$fvf$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 20 Nov 2021 14:56:18 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="d8dad1cda7832553e2e28667cf8732c8";
logging-data="28984"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18ZrKWg83rXVI+BrKAD8HE4np2FVqfLiaM="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.3.2
Cancel-Lock: sha1:hm8pAhl5H+tTL0oVl15m9E4BZbY=
In-Reply-To: <sn68lf$fvf$1@dont-email.me>
Content-Language: de-DE
 by: Bonita Montero - Sat, 20 Nov 2021 14:56 UTC

This is a completed implementation of the sgi-class. It now includes
all relevant operators and it includes all integral-promotions so that
f.a. sgi<char> * sgi<char> becomes a sgi<int>.
Look how flexible the return-type is deduced:

template<typename T1, typename T2>
inline
sgi<decltype((T1)1 * (T2)1)> operator *( sgi<T1> left, sgi<T2> right )
{ return sgi<decltype((T1)1 * (T2)1)>( (T1)left.value * (T2)right.value );
}

C++ is just the most powerful systems-programming language.

#pragma once
#include <type_traits>

template<typename T>
concept sgi_value = std::is_integral_v<T> && std::is_signed_v<T>;

template<typename T>
requires sgi_value<T>
struct sgi final
{ sgi() = default;
sgi( sgi_value auto value );
template<typename T2>
sgi( sgi<T2> other );
template<typename T2>
requires sgi_value<T2>
explicit operator T2() const;
template<typename T2>
sgi &operator |=( sgi<T2> other );
sgi &operator |=( sgi_value auto other );
template<typename T2>
sgi &operator ^=( sgi<T2> other );
sgi &operator ^=( sgi_value auto other );
template<typename T2>
sgi &operator &=( sgi<T2> other );
sgi &operator &=( sgi_value auto value );
sgi &operator >>=( unsigned shift );
sgi &operator <<=( unsigned shift );
template<typename T2>
sgi &operator %=( sgi<T2> other );
sgi &operator %=( sgi_value auto other );
template<typename T2>
sgi &operator /=( sgi<T2> other );
sgi &operator /=( sgi_value auto other );
template<typename T2>
sgi &operator *=( sgi<T2> other );
sgi &operator *=( sgi_value auto other );
template<typename T2>
sgi &operator -=( sgi<T2> other);
sgi &operator -=( sgi_value auto other );
template<typename T2>
sgi &operator +=( sgi<T2> other );
sgi &operator +=( sgi_value auto other );
template<typename T2>
sgi &operator =( sgi<T2> other );
sgi &operator =( sgi_value auto other );
template<typename T1, typename T2>
friend bool operator ||( sgi<T1> left, sgi<T2> right );
template<typename T2>
friend bool operator ||( sgi_value auto left, sgi<T2> right );
template<typename T1>
friend bool operator ||( sgi<T1> left, sgi_value auto right );
template<typename T1, typename T2>
friend bool operator &&( sgi<T1> left, sgi<T2> right );
template<typename T2>
friend bool operator &&( sgi_value auto left, sgi<T2> right );
template<typename T1>
friend bool operator &&( sgi<T1> left, sgi_value auto right );
template<typename T1, typename T2>
friend sgi<decltype((T1)1 | (T2)1)> operator |( sgi<T1> left, sgi<T2>
right );
template<typename T1, typename T2>
friend sgi<decltype((T1)1 | (T2)1)> operator |( T1 left, sgi<T2> right );
template<typename T1, typename T2>
friend sgi<decltype((T1)1 | (T2)1)> operator |( sgi<T1> left, T2 right );
template<typename T1, typename T2>
friend sgi<decltype((T1)1 ^ (T2)1)> operator ^( sgi<T1> left, sgi<T2>
right );
template<typename T1, typename T2>
friend sgi<decltype((T1)1 ^ (T2)1)> operator ^( T1 left, sgi<T2> right );
template<typename T1, typename T2>
friend sgi<decltype((T1)1 ^ (T2)1)> operator ^( sgi<T1> left, T2 right );
template<typename T1, typename T2>
friend sgi<decltype((T1)1 & (T2)1)> operator ^( sgi<T1> left, sgi<T2>
right );
template<typename T1, typename T2>
friend sgi<decltype((T1)1 & (T2)1)> operator ^( T1 left, sgi<T2> right );
template<typename T1, typename T2>
friend sgi<decltype((T1)1 & (T2)1)> operator ^( sgi<T1> left, T2 right );
template<typename T1, typename T2>
friend bool operator !=( sgi<T1> left, sgi<T2> right );
template<typename T2>
friend bool operator !=( sgi_value auto left, sgi<T2> right );
template<typename T1>
friend bool operator !=( sgi<T1> left, sgi_value auto right );
template<typename T1, typename T2>
friend bool operator ==( sgi<T1> left, sgi<T2> right );
template<typename T2>
friend bool operator ==( sgi_value auto left, sgi<T2> right );
template<typename T1>
friend bool operator ==( sgi<T1> left, sgi_value auto right );
template<typename T1, typename T2>
friend bool operator >=( sgi<T1> left, sgi<T2> right );
template<typename T2>
friend bool operator >=( sgi_value auto left, sgi<T2> right );
template<typename T1>
friend bool operator >=( sgi<T1> left, sgi_value auto right );
template<typename T1, typename T2>
friend bool operator >( sgi<T1> left, sgi<T2> right );
template<typename T2>
friend bool operator >( sgi_value auto left, sgi<T2> right );
template<typename T1>
friend bool operator >( sgi<T1> left, sgi_value auto right );
template<typename T1, typename T2>
friend bool operator <=( sgi<T1> left, sgi<T2> right );
template<typename T2>
friend bool operator <=( sgi_value auto left, sgi<T2> right );
template<typename T1>
friend bool operator <=( sgi<T1> left, sgi_value auto right );
template<typename T1, typename T2>
friend bool operator <( sgi<T1> left, sgi<T2> right );
template<typename T2>
friend bool operator <( sgi_value auto left, sgi<T2> right );
template<typename T1>
friend bool operator <( sgi<T1> left, sgi_value auto right );
template<typename T1, typename T2>
friend sgi<decltype((T1)1 - (T2)1)> operator -( sgi<T1> left, sgi<T2>
right );
template<typename T1, typename T2>
requires sgi_value<T1>
friend sgi<decltype((T1)1 - (T2)1)> operator -( T1 left, sgi<T2> right );
template<typename T1, typename T2>
requires sgi_value<T2>
friend sgi<decltype((T1)1 - (T2)1)> operator -( sgi<T1> left, T2 right );
template<typename T1, typename T2>
friend sgi<decltype((T1)1 + (T2)1)> operator +( sgi<T1> left, sgi<T2>
right );
template<typename T1, typename T2>
requires sgi_value<T1>
friend sgi<decltype((T1)1 + (T2)1)> operator +( T1 left, sgi<T2> right );
template<typename T1, typename T2>
requires sgi_value<T2>
friend sgi<decltype((T1)1 + (T2)1)> operator +( sgi<T1> left, T2 right );
template<typename T1, typename T2>
friend sgi<decltype((T1)1 % (T2)1)> operator %( sgi<T1> left, sgi<T2>
right );
template<typename T1, typename T2>
requires sgi_value<T1>
friend sgi<decltype((T1)1 % (T2)1)> operator %( T1 left, sgi<T2> right );
template<typename T1, typename T2>
requires sgi_value<T2>
friend sgi<decltype((T1)1 % (T2)1)> operator %( sgi<T1> left, T2 right );
template<typename T1, typename T2>
friend sgi<decltype((T1)1 / (T2)1)> operator /( sgi<T1> left, sgi<T2>
right );
template<typename T1, typename T2>
requires sgi_value<T1>
friend sgi<decltype((T1)1 / (T2)1)> operator /( T1 left, sgi<T2> right );
template<typename T1, typename T2>
requires sgi_value<T2>
friend sgi<decltype((T1)1 / (T2)1)> operator /( sgi<T1> left, T2 right );
template<typename T1, typename T2>
friend sgi<decltype((T1)1 * (T2)1)> operator *( sgi<T1> left, sgi<T2>
right );
template<typename T1, typename T2>
requires sgi_value<T1>
friend sgi<decltype((T1)1 * (T2)1)> operator *( T1 left, sgi<T2> right );
template<typename T1, typename T2>
requires sgi_value<T2>
friend sgi<decltype((T1)1 * (T2)1)> operator *( sgi<T1> left, T2 right );
sgi operator ~() const;
bool operator !() const;
sgi operator +() const;
sgi operator -() const;
sgi<T> &operator --();
sgi<T> &operator ++();
sgi<T> operator --( int );
sgi<T> operator ++( int );
private:
using TU = std::make_unsigned_t<T>;
TU value;
};

template<typename T>
requires sgi_value<T>
template<typename T2>
inline
sgi<T>::sgi( sgi<T2> other ) :
value( (T)(T2)other.value )
{ }

template<typename T>
requires sgi_value<T>
inline
sgi<T>::sgi( sgi_value auto other ) :
value( (T)other )
{ }

template<typename T>
requires sgi_value<T>
template<typename T2>
requires sgi_value<T2>
inline
sgi<T>::operator T2() const
{ return (T2)(T)value;
}

template<typename T>
requires sgi_value<T>
template<typename T2>
inline
sgi<T> &sgi<T>::operator |=( sgi<T2> other )
{
Click here to read the complete article

Re: Signed integer overflow

<snb3bs$2me$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: bc...@freeuk.com (Bart)
Newsgroups: comp.lang.c
Subject: Re: Signed integer overflow
Date: Sat, 20 Nov 2021 15:14:04 +0000
Organization: A noiseless patient Spider
Lines: 20
Message-ID: <snb3bs$2me$1@dont-email.me>
References: <38ba6059-8d52-4f64-9ce6-8d23b82203f6n@googlegroups.com>
<sn66oj$2r2$1@dont-email.me> <sn68lf$fvf$1@dont-email.me>
<snb2ai$s9o$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sat, 20 Nov 2021 15:14:04 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="1153000d80528f48a6c31e844e153496";
logging-data="2766"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18P7rMlZH4Q6H8PI/JZXaCjnMvW5L86P5M="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.3.1
Cancel-Lock: sha1:OiccaF2++Ftcp0Yb/7L42MdLaCs=
In-Reply-To: <snb2ai$s9o$1@dont-email.me>
 by: Bart - Sat, 20 Nov 2021 15:14 UTC

On 20/11/2021 14:56, Bonita Montero wrote:
> This is a completed implementation of the sgi-class. It now includes
> all relevant operators and it includes all integral-promotions so that
> f.a. sgi<char> * sgi<char> becomes a sgi<int>.
> Look how flexible the return-type is deduced:
>
> template<typename T1, typename T2>
> inline
> sgi<decltype((T1)1 * (T2)1)> operator *( sgi<T1> left, sgi<T2> right )
> {
>     return sgi<decltype((T1)1 * (T2)1)>( (T1)left.value *
> (T2)right.value );
> }

<snip>

Blimey, that's a lot of templates.

But what's an sgi-class, and what's it got to do with signed integer
overflow?

Re: Signed integer overflow

<snb3se$6rt$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c
Subject: Re: Signed integer overflow
Date: Sat, 20 Nov 2021 16:22:55 +0100
Organization: A noiseless patient Spider
Lines: 39
Message-ID: <snb3se$6rt$1@dont-email.me>
References: <38ba6059-8d52-4f64-9ce6-8d23b82203f6n@googlegroups.com>
<sn66oj$2r2$1@dont-email.me> <sn68lf$fvf$1@dont-email.me>
<snb2ai$s9o$1@dont-email.me> <snb3bs$2me$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sat, 20 Nov 2021 15:22:54 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="d8dad1cda7832553e2e28667cf8732c8";
logging-data="7037"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19wkf+eKuzBxmM3m9fQsGQ/VVV8OeN0rlM="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.3.2
Cancel-Lock: sha1:94VH/sMD59zLQ5g5l7Ys6Udh510=
In-Reply-To: <snb3bs$2me$1@dont-email.me>
Content-Language: de-DE
 by: Bonita Montero - Sat, 20 Nov 2021 15:22 UTC

Am 20.11.2021 um 16:14 schrieb Bart:
> On 20/11/2021 14:56, Bonita Montero wrote:
>> This is a completed implementation of the sgi-class. It now includes
>> all relevant operators and it includes all integral-promotions so that
>> f.a. sgi<char> * sgi<char> becomes a sgi<int>.
>> Look how flexible the return-type is deduced:
>>
>> template<typename T1, typename T2>
>> inline
>> sgi<decltype((T1)1 * (T2)1)> operator *( sgi<T1> left, sgi<T2> right )
>> {
>>      return sgi<decltype((T1)1 * (T2)1)>( (T1)left.value *
>> (T2)right.value );
>> }
>
> <snip>
>
> Blimey, that's a lot of templates.
>
> But what's an sgi-class, and what's it got to do with signed integer
> overflow?

Its a replacement for all signed integers and you can check overflows
with it. I.e. you could write:

int main()
{ using sgis = sgi<short>;
sgis a( rnd() );
bool f = a + 1 < a;
cout << f << endl;
}

And f is calculated at runtime and it doesn't become a constant though
optimization. So you can check for signed overflows at runtime. This
might not be required often, but the sgi-class is more convenient than
this casting-orgy which also prevents that the compiler optimizes away
the comparison.

Re: Signed integer overflow

<20211122140948.a95de2bf9a0b49bc3b141436@g{oogle}mail.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: anton....@g{oogle}mail.com (Anton Shepelev)
Newsgroups: comp.lang.c
Subject: Re: Signed integer overflow
Date: Mon, 22 Nov 2021 14:09:48 +0300
Organization: A noiseless patient Spider
Lines: 52
Message-ID: <20211122140948.a95de2bf9a0b49bc3b141436@g{oogle}mail.com>
References: <38ba6059-8d52-4f64-9ce6-8d23b82203f6n@googlegroups.com>
<2leiJ.15504$SW5.13829@fx45.iad>
<e716ee2e-e1ec-483e-a85d-ce1e3bb708d9n@googlegroups.com>
<20211119134908.06e833c73097aae5f70ff11c@g{oogle}mail.com>
<sn8cg9$ics$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Injection-Info: reader02.eternal-september.org; posting-host="119fbc2a697a6b98ca967fd655d79f0e";
logging-data="31725"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX192is95mgaa3oTC2P1be//zly00Or81dQ0="
Cancel-Lock: sha1:VzaL05abc0B5qeMdMkwqufpU/iA=
X-Newsreader: Sylpheed 3.5.0 (GTK+ 2.24.23; i686-pc-mingw32)
 by: Anton Shepelev - Mon, 22 Nov 2021 11:09 UTC

Manfred to Anton Shepelev:

> > lat_add( float a, float b )
> > { const float MAG = 180.0;
> > float res;
> > res = a + b;
> > if( res >= MAG ) res -= 2*MAG;
> > else if( res < MAG ) res += 2*MAG;
> > return res;
> > }
> >
> > Is it for reasons of optimisation that you use nor-
> > malised integers instead?
>
> Or because it requires extra care
> [...]
> you need:
>
> else if( res < -MAG ) res += 2*MAG;

Blame that on me rather than on my idea. I have negligible
experience with numeric calculations and wrote my post from
work after completing a super-urgent task and before switch-
ing to a merely urgent one.

> You also have to consider rounding errors around +/-MAG.

I had not thought of that. Can my function (with your cor-
rection) yield an out-of-range result? What then --

float lat_add( float a, float b )
{ const float MAG = 180.0;
float res;
res = a + b;
if( res >= MAG ) { res -= 2*MAG; if (res < -MAG) res = -MAG;
else if( res < -MAG ) { res += 2*MAG; if (res >= MAG) res = -MAG;
return res;
}

I am not sure whether this additional clamping is needed.

> Integer arithmetic has less kinks than floating point,
> there are circumstances where it is preferable. I am not
> saying that it is the good choice here, in fact I think
> there is just not enough information in what the OP has
> given to give a definite answer.

Thanks for the explanation.

--
() ascii ribbon campaign - against html e-mail
/\ http://preview.tinyurl.com/qcy6mjc [archived]

Pages:12
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor