Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

Between infinite and short there is a big difference. -- G. H. Gonnet


devel / comp.lang.c / 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
Signed integer overflow

<38ba6059-8d52-4f64-9ce6-8d23b82203f6n@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:ac8:5e4e:: with SMTP id i14mr1421415qtx.129.1636394178228;
Mon, 08 Nov 2021 09:56:18 -0800 (PST)
X-Received: by 2002:ac8:5a89:: with SMTP id c9mr1496113qtc.202.1636394177998;
Mon, 08 Nov 2021 09:56:17 -0800 (PST)
Path: i2pn2.org!i2pn.org!paganini.bofh.team!news.dns-netz.com!news.freedyn.net!newsreader4.netcologne.de!news.netcologne.de!peer01.ams1!peer.ams1.xlned.com!news.xlned.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Mon, 8 Nov 2021 09:56:17 -0800 (PST)
Injection-Info: google-groups.googlegroups.com; posting-host=2a01:c22:bd89:ce00:51c2:ed23:8a4:ceac;
posting-account=o1zNBwoAAABP2KiH5g6nPQp_sYWidVdt
NNTP-Posting-Host: 2a01:c22:bd89:ce00:51c2:ed23:8a4:ceac
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <38ba6059-8d52-4f64-9ce6-8d23b82203f6n@googlegroups.com>
Subject: Signed integer overflow
From: howe...@gmail.com (Howerd Oakford)
Injection-Date: Mon, 08 Nov 2021 17:56:18 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 1809
 by: Howerd Oakford - Mon, 8 Nov 2021 17:56 UTC

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

Re: Signed integer overflow

<2leiJ.15504$SW5.13829@fx45.iad>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!news.uzoreto.com!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!fx45.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>
From: Rich...@Damon-Family.org (Richard Damon)
In-Reply-To: <38ba6059-8d52-4f64-9ce6-8d23b82203f6n@googlegroups.com>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Lines: 42
Message-ID: <2leiJ.15504$SW5.13829@fx45.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: Mon, 8 Nov 2021 13:51:42 -0500
X-Received-Bytes: 2940
 by: Richard Damon - Mon, 8 Nov 2021 18:51 UTC

On 11/8/21 12:56 PM, Howerd Oakford 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.
>
> 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
>
>
>

Signed overflow is NOT defined by the C standard, though a given
implementation may do so. (GCC has an option to force it to not optimize
based on signed overflow not existing). Without using some
implementation specific option to make it add that promise, you run the
risk of an agresive optimizer giving you some unexpected results. The
biggest case would be if you ever did test to detect the wrap arround,
that is a key thing that might get optimized away.

The physical hardware will work perfectly fine.

And alternative is to store the values as uint32_t, as unsigned
arithmetic is defined to be defined as modulo arithmetic. You can then
convert the uit32_t to int32_t to get -180 to 179.999 range.

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

The other option is just scale things a bit smaller and manually do the
wrap around yourself. You do realize you are locating things to the
centimeter if I am doing my math right. At that scale, you need a lot of
corrections for the geodetic to keep things accurate.

Re: Signed integer overflow

<smbrns$i60$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!paganini.bofh.team!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: Mon, 8 Nov 2021 18:53:51 +0000
Organization: A noiseless patient Spider
Lines: 40
Message-ID: <smbrns$i60$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: Mon, 8 Nov 2021 18:53:49 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="5aecade92803dad329ae52332fcb0c0e";
logging-data="18624"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/3w9RPATmFz1RjwRyz8Qy1FF/Ry5/eAS8="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.3.0
Cancel-Lock: sha1:77yIdynAw2+iNp0bGdXzbTMt+TY=
In-Reply-To: <38ba6059-8d52-4f64-9ce6-8d23b82203f6n@googlegroups.com>
 by: Bart - Mon, 8 Nov 2021 18:53 UTC

On 08/11/2021 17:56, Howerd Oakford 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.
>
> 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?

Only if using some scaled representation where the limits of int32:

-2147483648 to 2147483647

are mapped to -180.0 to 179.9...

At least, it would be OK with pretty much every processor on the planet,
and most languages that support a 32-bit signed int type.

Except that C makes it undefined.

> If not, is it OK to drop down to ARM assembler to avoid the "undefined behaviour" of C?

You could do that, but there are few problems (like grappling with gcc
ASM syntax, or having to write a separate ASM module, or not portability).

But you can stay in C, and just cast to uint32_t, do the sum, then cast
back to int32_t.

However, since you are using ARM and not some 8-bit controller, why not
just use float or double? You would need to wrap using software:

if (d > 180.0) d -:= 360.0; // etc (assuming degrees not radians)

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

How much RAM is in the machine? 1GM would store 128 million 64-bit readings.

Re: Signed integer overflow

<trsiogth78gm1e2cdlim4nlg6v85mljvns@4ax.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: schwa...@delq.com (Barry Schwarz)
Newsgroups: comp.lang.c
Subject: Re: Signed integer overflow
Date: Mon, 08 Nov 2021 11:16:14 -0800
Organization: A noiseless patient Spider
Lines: 36
Message-ID: <trsiogth78gm1e2cdlim4nlg6v85mljvns@4ax.com>
References: <38ba6059-8d52-4f64-9ce6-8d23b82203f6n@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="f06437b3161da074209e9bcfdd1926af";
logging-data="29651"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+V/ptD8JIlUdm2VnVsjfaUl5EHdo/YyDk="
Cancel-Lock: sha1:9j5YW4t/mqFdsj5UKU1a4sPRT/g=
X-Newsreader: Forte Agent 4.2/32.1118
 by: Barry Schwarz - Mon, 8 Nov 2021 19:16 UTC

On Mon, 8 Nov 2021 09:56:17 -0800 (PST), Howerd Oakford
<howerdo@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.
>
>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...

You will notice from the quoted material in this response that using
Goggle groups to post messages causes problems with the length of the
lines in your post. The recommendation is to use a real newsgroup
reader.

The answer to your first question depends on how you store fractional
degrees in an int32_t. Is 179.9999 actually stored as 0x7fffffff and
-180 stored as 0x80000000? If not, how does the hardware "know" that
adding .0001 to 179.9999 should cause a "carry" into the sign bit?

Regarding your second question, if you know the code the compiler will
generate and you know how the hardware will behave when it executes
those instructions, you can call this "implementation defined
behavior" for your environment. However, you would want to document
this VERY thoroughly because any change in the environment (such as
compiler version, compiler options, or hardware upgrade) would
necessitate reexamining this issue from scratch.

--
Remove del for email

Re: Signed integer overflow

<10b5cc02-a50f-4b20-b651-d7fabe4af9f3n@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:620a:2a10:: with SMTP id o16mr1190947qkp.357.1636399528274;
Mon, 08 Nov 2021 11:25:28 -0800 (PST)
X-Received: by 2002:a05:6214:9a1:: with SMTP id du1mr1487457qvb.45.1636399528098;
Mon, 08 Nov 2021 11:25:28 -0800 (PST)
Path: i2pn2.org!i2pn.org!aioe.org!news.dns-netz.com!news.freedyn.net!newsreader4.netcologne.de!news.netcologne.de!peer02.ams1!peer.ams1.xlned.com!news.xlned.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Mon, 8 Nov 2021 11:25:27 -0800 (PST)
In-Reply-To: <38ba6059-8d52-4f64-9ce6-8d23b82203f6n@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=2601:192:4800:8f40:90d6:ed57:86a0:374e;
posting-account=lT6HywoAAABy78apffHGZc7J4VlbclxH
NNTP-Posting-Host: 2601:192:4800:8f40:90d6:ed57:86a0:374e
References: <38ba6059-8d52-4f64-9ce6-8d23b82203f6n@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <10b5cc02-a50f-4b20-b651-d7fabe4af9f3n@googlegroups.com>
Subject: Re: Signed integer overflow
From: donalham...@gmail.com (Eric Sosman)
Injection-Date: Mon, 08 Nov 2021 19:25:28 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 1632
 by: Eric Sosman - Mon, 8 Nov 2021 19:25 UTC

On Monday, November 8, 2021 at 12:56:23 PM UTC-5, 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.

Use a uint32_t variable running from 0 degrees to 360-360/4294967296 degrees.
Wraparound does exactly what you want, and it's portable.

Re: Signed integer overflow

<smbtm4$225$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: Signed integer overflow
Date: Mon, 8 Nov 2021 20:26:59 +0100
Organization: A noiseless patient Spider
Lines: 78
Message-ID: <smbtm4$225$1@dont-email.me>
References: <38ba6059-8d52-4f64-9ce6-8d23b82203f6n@googlegroups.com>
<2leiJ.15504$SW5.13829@fx45.iad>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Date: Mon, 8 Nov 2021 19:27:01 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="34df53088790c71db89c3d94d2ab4515";
logging-data="2117"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19SgagDc6CYCpaG9bGKKS+88yqD7wf/E4Y="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
Cancel-Lock: sha1:/6EQXgif8tORQPO8heobwzij5pI=
In-Reply-To: <2leiJ.15504$SW5.13829@fx45.iad>
Content-Language: en-GB
 by: David Brown - Mon, 8 Nov 2021 19:26 UTC

On 08/11/2021 19:51, Richard Damon wrote:
> On 11/8/21 12:56 PM, Howerd Oakford 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.
>>
>> 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
>>
>>
>>
>
> Signed overflow is NOT defined by the C standard, though a given
> implementation may do so. (GCC has an option to force it to not optimize
> based on signed overflow not existing).

That is not an accurate way to describe it. gcc has an option to give
signed integers wrapping semantics. It is not a matter of what
optimisations it enables or disables, it is a matter of adding an extra
"rule" to C to say that signed integer arithmetic has wrapping two's
complement results.

The flag in question is "-fwrapv", but IMHO it is better to use a pragma
in the source code as that makes it independent of any compiler flags
you have chosen:

#pragma GCC optimize "-fwrapv"

> Without using some
> implementation specific option to make it add that promise, you run the
> risk of an agresive optimizer giving you some unexpected results. The
> biggest case would be if you ever did test to detect the wrap arround,
> that is a key thing that might get optimized away.

Indeed. Never rely on undefined behaviour - even if it passes your
tests today, some day in the future it's going to bite you (or the
maintainers of your code). If you use "-fwrapv" with gcc (or clang, or
icc), you are no longer using undefined behaviour - you've given the
behaviour a definition.

>
> The physical hardware will work perfectly fine.
>
> And alternative is to store the values as uint32_t, as unsigned
> arithmetic is defined to be defined as modulo arithmetic. You can then
> convert the uit32_t to int32_t to get -180 to 179.999 range.
>

Yes. That is usually the safe and portable way to get the behaviour.
(It's not /entirely/ portable, since there is implementation-defined
behaviour there, but it will work on any realistic C implementation.)

> I presume you are scaling the numbers so 179.999 degrees would be
> 0x7FFFFFFF and -180 would be 0x80000000
>
> The other option is just scale things a bit smaller and manually do the
> wrap around yourself. You do realize you are locating things to the
> centimeter if I am doing my math right. At that scale, you need a lot of
> corrections for the geodetic to keep things accurate.
>

Re: Signed integer overflow

<smbu4h$5je$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: Signed integer overflow
Date: Mon, 8 Nov 2021 20:34:41 +0100
Organization: A noiseless patient Spider
Lines: 79
Message-ID: <smbu4h$5je$1@dont-email.me>
References: <38ba6059-8d52-4f64-9ce6-8d23b82203f6n@googlegroups.com>
<smbrns$i60$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Date: Mon, 8 Nov 2021 19:34:41 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="34df53088790c71db89c3d94d2ab4515";
logging-data="5742"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18yd1t8C4SKy8BkMPqwZWZqSaSJDNv1ubg="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
Cancel-Lock: sha1:aKSJnc3wN+kApdOMvAfUXd+LwAY=
In-Reply-To: <smbrns$i60$1@dont-email.me>
Content-Language: en-GB
 by: David Brown - Mon, 8 Nov 2021 19:34 UTC

On 08/11/2021 19:53, Bart wrote:
> On 08/11/2021 17:56, Howerd Oakford 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.
>>
>> 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?
>
> Only if using some scaled representation where the limits of int32:
>
>    -2147483648 to 2147483647
>
> are mapped to -180.0 to 179.9...
>

Indeed.

> At least, it would be OK with pretty much every processor on the planet,
> and most languages that support a 32-bit signed int type.
>

For some undefined value of "most languages" - and irrelevant to a C
programmer asking C questions in a C newsgroup.

> Except that C makes it undefined.
>

Exactly. Some of MISRA's rules are downright stupid - this particular
one, banning reliance on any overflow behaviour of signed integers - is
a good one.

>
>> If not, is it OK to drop down to ARM assembler to avoid the "undefined
>> behaviour" of C?
>
> You could do that, but there are few problems (like grappling with gcc
> ASM syntax, or having to write a separate ASM module, or not portability).
>

gcc's asm syntax is powerful, but can be frightening when you are
unfamiliar with it. It is vastly better (and IMHO easier) than writing
separate assembly modules. But it is not portable, not MISRA friendly,
and - as you say - not necessary here.

> But you can stay in C, and just cast to uint32_t, do the sum, then cast
> back to int32_t.
>

Yes.

> However, since you are using ARM and not some 8-bit controller, why not
> just use float or double? You would need to wrap using software:
>
>    if (d > 180.0) d -:= 360.0;   // etc (assuming degrees not radians)
>

Indeed. Any 64-bit ARM target supports hardware floating point (most
small 32-bit ARM microcontrollers do not). Using floating point makes
the code clearer, and often more efficient, than working with scaled
integer arithmetic.

>
>> 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...
>
> How much RAM is in the machine? 1GM would store 128 million 64-bit
> readings.
>

It sounds like the OP is suffering from premature optimisation here!

Re: Signed integer overflow

<slrnsoj0ej.1rfm.grahn+nntp@frailea.sa.invalid>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: grahn+n...@snipabacken.se (Jorgen Grahn)
Newsgroups: comp.lang.c
Subject: Re: Signed integer overflow
Date: 8 Nov 2021 20:00:19 GMT
Lines: 14
Message-ID: <slrnsoj0ej.1rfm.grahn+nntp@frailea.sa.invalid>
References: <38ba6059-8d52-4f64-9ce6-8d23b82203f6n@googlegroups.com>
X-Trace: individual.net ijBjGcRFbb0yQxcnQLMmagcudqt85FD8sUIW3xjuUSNOAr14AJ
Cancel-Lock: sha1:L2TAHY0S00RMA2rfQX9mo/ZwYmg=
User-Agent: slrn/1.0.3 (OpenBSD)
 by: Jorgen Grahn - Mon, 8 Nov 2021 20:00 UTC

On Mon, 2021-11-08, Howerd Oakford 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...

You don't have to store anything: you can do the calculation in
64-bit, deal with overflow, and go back to 32-bit.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .

Re: Signed integer overflow

<smc1av$usg$1@dont-email.me>

  copy mid

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

  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: Mon, 8 Nov 2021 20:29:22 +0000
Organization: A noiseless patient Spider
Lines: 42
Message-ID: <smc1av$usg$1@dont-email.me>
References: <38ba6059-8d52-4f64-9ce6-8d23b82203f6n@googlegroups.com>
<smbrns$i60$1@dont-email.me> <smbu4h$5je$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Mon, 8 Nov 2021 20:29:19 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="5aecade92803dad329ae52332fcb0c0e";
logging-data="31632"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX184EN3ODDiMkbcQkgDx1ngsPwnS6JmXxaY="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.3.0
Cancel-Lock: sha1:ptF72FeOwfiIdjXw5AtHeM1UE1c=
In-Reply-To: <smbu4h$5je$1@dont-email.me>
 by: Bart - Mon, 8 Nov 2021 20:29 UTC

On 08/11/2021 19:34, David Brown wrote:
> On 08/11/2021 19:53, Bart wrote:
>> On 08/11/2021 17:56, Howerd Oakford 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.
>>>
>>> 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?
>>
>> Only if using some scaled representation where the limits of int32:
>>
>>    -2147483648 to 2147483647
>>
>> are mapped to -180.0 to 179.9...
>>
>
> Indeed.
>
>> At least, it would be OK with pretty much every processor on the planet,
>> and most languages that support a 32-bit signed int type.
>>
>
> For some undefined value of "most languages" - and irrelevant to a C
> programmer asking C questions in a C newsgroup.

I was emphasising out that integer overflow is tolerated with
well-defined behaviour nearly everywhere else, so there is nothing
really wrong with wanting to rely on it.

It just that some languages aren't keen on it. In C it is UB, others may
trap the overflow..

You can only be really confident in ASM, which is perverse.

Re: Signed integer overflow

<e716ee2e-e1ec-483e-a85d-ce1e3bb708d9n@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:ac8:1e95:: with SMTP id c21mr2644709qtm.412.1636404351395;
Mon, 08 Nov 2021 12:45:51 -0800 (PST)
X-Received: by 2002:ac8:5e12:: with SMTP id h18mr2661543qtx.168.1636404351142;
Mon, 08 Nov 2021 12:45:51 -0800 (PST)
Path: i2pn2.org!i2pn.org!news.swapon.de!news.mixmin.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: Mon, 8 Nov 2021 12:45:50 -0800 (PST)
In-Reply-To: <2leiJ.15504$SW5.13829@fx45.iad>
Injection-Info: google-groups.googlegroups.com; posting-host=2a01:c22:bd89:ce00:51c2:ed23:8a4:ceac;
posting-account=o1zNBwoAAABP2KiH5g6nPQp_sYWidVdt
NNTP-Posting-Host: 2a01:c22:bd89:ce00:51c2:ed23:8a4:ceac
References: <38ba6059-8d52-4f64-9ce6-8d23b82203f6n@googlegroups.com> <2leiJ.15504$SW5.13829@fx45.iad>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <e716ee2e-e1ec-483e-a85d-ce1e3bb708d9n@googlegroups.com>
Subject: Re: Signed integer overflow
From: howe...@gmail.com (Howerd Oakford)
Injection-Date: Mon, 08 Nov 2021 20:45:51 +0000
Content-Type: text/plain; charset="UTF-8"
 by: Howerd Oakford - Mon, 8 Nov 2021 20:45 UTC

On Monday, November 8, 2021 at 7:51:54 PM UTC+1, Richard Damon wrote:
> On 11/8/21 12:56 PM, Howerd Oakford 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.
> >
> > 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
> >
> >
> >
> Signed overflow is NOT defined by the C standard, though a given
> implementation may do so. (GCC has an option to force it to not optimize
> based on signed overflow not existing). Without using some
> implementation specific option to make it add that promise, you run the
> risk of an agresive optimizer giving you some unexpected results. The
> biggest case would be if you ever did test to detect the wrap arround,
> that is a key thing that might get optimized away.
>
> The physical hardware will work perfectly fine.
>
> And alternative is to store the values as uint32_t, as unsigned
> arithmetic is defined to be defined as modulo arithmetic. You can then
> convert the uit32_t to int32_t to get -180 to 179.999 range.
>
> I presume you are scaling the numbers so 179.999 degrees would be
> 0x7FFFFFFF and -180 would be 0x80000000
>
> The other option is just scale things a bit smaller and manually do the
> wrap around yourself. You do realize you are locating things to the
> centimeter if I am doing my math right. At that scale, you need a lot of
> corrections for the geodetic to keep things accurate.
Hi Richard,

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

So, how about two macros for use on signed latitude and longitudes :
#define plus_int32( a, b ) int32_t( (uint32_t)(a) + (uint32_t)(b) )
#define minus_int32( a, b ) int32_t( (uint32_t)(a) - (uint32_t)(b) )

Would this produce the same code as setting the "-fwrapv" flag?

Doesn't casting between signed and unsigned integers present a similar wraparound problem?
Isn't this also dependent on a one's or two's complement environment, and therefore has "undefined behaviour"?

Cheers,
Howerd

Re: Signed integer overflow

<7d185487-109c-4359-a44f-ae209f95285an@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:620a:1a8b:: with SMTP id bl11mr1488813qkb.411.1636404556753; Mon, 08 Nov 2021 12:49:16 -0800 (PST)
X-Received: by 2002:a05:620a:4051:: with SMTP id i17mr1662485qko.333.1636404556575; Mon, 08 Nov 2021 12:49:16 -0800 (PST)
Path: i2pn2.org!i2pn.org!usenet.goja.nl.eu.org!2.eu.feeder.erje.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed9.news.xs4all.nl!tr3.eu1.usenetexpress.com!feeder.usenetexpress.com!tr1.iad1.usenetexpress.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: Mon, 8 Nov 2021 12:49:16 -0800 (PST)
In-Reply-To: <10b5cc02-a50f-4b20-b651-d7fabe4af9f3n@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=2a01:c22:bd89:ce00:51c2:ed23:8a4:ceac; posting-account=o1zNBwoAAABP2KiH5g6nPQp_sYWidVdt
NNTP-Posting-Host: 2a01:c22:bd89:ce00:51c2:ed23:8a4:ceac
References: <38ba6059-8d52-4f64-9ce6-8d23b82203f6n@googlegroups.com> <10b5cc02-a50f-4b20-b651-d7fabe4af9f3n@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <7d185487-109c-4359-a44f-ae209f95285an@googlegroups.com>
Subject: Re: Signed integer overflow
From: howe...@gmail.com (Howerd Oakford)
Injection-Date: Mon, 08 Nov 2021 20:49:16 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 13
 by: Howerd Oakford - Mon, 8 Nov 2021 20:49 UTC

On Monday, November 8, 2021 at 8:25:35 PM UTC+1, Eric Sosman wrote:
> On Monday, November 8, 2021 at 12:56:23 PM UTC-5, 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.
> Use a uint32_t variable running from 0 degrees to 360-360/4294967296 degrees.
> Wraparound does exactly what you want, and it's portable.
Hi Eric,

So 1 degree West would be 359 degrees East - this is a great idea - thanks!

Cheers,
Howerd

Re: Signed integer overflow

<9sgiJ.11645$np6.6148@fx46.iad>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!4.us.feeder.erje.net!2.eu.feeder.erje.net!feeder.erje.net!newsreader4.netcologne.de!news.netcologne.de!peer01.ams1!peer.ams1.xlned.com!news.xlned.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx46.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>
<2leiJ.15504$SW5.13829@fx45.iad>
<e716ee2e-e1ec-483e-a85d-ce1e3bb708d9n@googlegroups.com>
From: Rich...@Damon-Family.org (Richard Damon)
In-Reply-To: <e716ee2e-e1ec-483e-a85d-ce1e3bb708d9n@googlegroups.com>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Lines: 73
Message-ID: <9sgiJ.11645$np6.6148@fx46.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: Mon, 8 Nov 2021 16:15:48 -0500
X-Received-Bytes: 4534
 by: Richard Damon - Mon, 8 Nov 2021 21:15 UTC

On 11/8/21 3:45 PM, Howerd Oakford wrote:
> On Monday, November 8, 2021 at 7:51:54 PM UTC+1, Richard Damon wrote:
>> On 11/8/21 12:56 PM, Howerd Oakford 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.
>>>
>>> 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
>>>
>>>
>>>
>> Signed overflow is NOT defined by the C standard, though a given
>> implementation may do so. (GCC has an option to force it to not optimize
>> based on signed overflow not existing). Without using some
>> implementation specific option to make it add that promise, you run the
>> risk of an agresive optimizer giving you some unexpected results. The
>> biggest case would be if you ever did test to detect the wrap arround,
>> that is a key thing that might get optimized away.
>>
>> The physical hardware will work perfectly fine.
>>
>> And alternative is to store the values as uint32_t, as unsigned
>> arithmetic is defined to be defined as modulo arithmetic. You can then
>> convert the uit32_t to int32_t to get -180 to 179.999 range.
>>
>> I presume you are scaling the numbers so 179.999 degrees would be
>> 0x7FFFFFFF and -180 would be 0x80000000
>>
>> The other option is just scale things a bit smaller and manually do the
>> wrap around yourself. You do realize you are locating things to the
>> centimeter if I am doing my math right. At that scale, you need a lot of
>> corrections for the geodetic to keep things accurate.
> Hi Richard,
>
>> I presume you are scaling the numbers so 179.999 degrees would be
>> 0x7FFFFFFF and -180 would be 0x80000000
> Exactly.
>
> So, how about two macros for use on signed latitude and longitudes :
> #define plus_int32( a, b ) int32_t( (uint32_t)(a) + (uint32_t)(b) )
> #define minus_int32( a, b ) int32_t( (uint32_t)(a) - (uint32_t)(b) )
>
> Would this produce the same code as setting the "-fwrapv" flag?
>
> Doesn't casting between signed and unsigned integers present a similar wraparound problem?
> Isn't this also dependent on a one's or two's complement environment, and therefore has "undefined behaviour"?
>
> Cheers,
> Howerd
>

Casting signed to unsigned is defined via the modulo rule.
Casting unsigned to signed isn't UB on 'overflow' but implementation
defined behavior which will almost certainly be doing what you want in
this case.

On an arbitrary machine, the unsigned to signed conversion might do
strange things to large nubmers going negative, but if you know that you
have a 2's complement machine with no padding bits in the signed number
representation, they will almost certainly define it the right way, as
that is what is certainly the cheapest.

Since you said you were ARM, we know it is this sort of sane 2's complement.

Re: Signed integer overflow

<smc5mp$ga$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: Signed integer overflow
Date: Mon, 8 Nov 2021 22:43:52 +0100
Organization: A noiseless patient Spider
Lines: 21
Message-ID: <smc5mp$ga$1@dont-email.me>
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=utf-8
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 8 Nov 2021 21:43:53 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="34df53088790c71db89c3d94d2ab4515";
logging-data="522"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18cwVvtneqFcqjQyZlaM4RwA690Pd+bMBs="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
Cancel-Lock: sha1:g571SVXebIRqtobAkgIq3BKHgbc=
In-Reply-To: <e716ee2e-e1ec-483e-a85d-ce1e3bb708d9n@googlegroups.com>
Content-Language: en-GB
 by: David Brown - Mon, 8 Nov 2021 21:43 UTC

On 08/11/2021 21:45, Howerd Oakford wrote:
> On Monday, November 8, 2021 at 7:51:54 PM UTC+1, Richard Damon wrote:

>
>> I presume you are scaling the numbers so 179.999 degrees would be
>> 0x7FFFFFFF and -180 would be 0x80000000
> Exactly.
>
> So, how about two macros for use on signed latitude and longitudes :
> #define plus_int32( a, b ) int32_t( (uint32_t)(a) + (uint32_t)(b) )
> #define minus_int32( a, b ) int32_t( (uint32_t)(a) - (uint32_t)(b) )
>
> Would this produce the same code as setting the "-fwrapv" flag?

It would give the same code, but it really is not a good idea. You are
better off thinking about the kind of data you have and how you want to
handle it. It's unlikely that scaled integers will be the clearest and
most practical way to hold such position data - the wrapping is of
negligible benefit. Aim for clarity of code - not for imagined object
code efficiency.

Re: Signed integer overflow

<87a6iez4sg.fsf@bsb.me.uk>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: ben.use...@bsb.me.uk (Ben Bacarisse)
Newsgroups: comp.lang.c
Subject: Re: Signed integer overflow
Date: Mon, 08 Nov 2021 22:00:31 +0000
Organization: A noiseless patient Spider
Lines: 41
Message-ID: <87a6iez4sg.fsf@bsb.me.uk>
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
Injection-Info: reader02.eternal-september.org; posting-host="ae245c3fc7f87ec92e88ec1c6c67efcd";
logging-data="8195"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1839CsK6BU5l9fAS6PQ59OpmZw97d9Lhj0="
Cancel-Lock: sha1:pIvgP1FS6QI9cQMLZ30vMIAoMPA=
sha1:VInAQC95ui24FSNR3PSEv7V++AY=
X-BSB-Auth: 1.e19b34ef84209ec5e07c.20211108220031GMT.87a6iez4sg.fsf@bsb.me.uk
 by: Ben Bacarisse - Mon, 8 Nov 2021 22:00 UTC

Howerd Oakford <howerdo@gmail.com> writes:

> On Monday, November 8, 2021 at 7:51:54 PM UTC+1, Richard Damon wrote:
>
>> I presume you are scaling the numbers so 179.999 degrees would be
>> 0x7FFFFFFF and -180 would be 0x80000000
>
> Exactly.
>
> So, how about two macros for use on signed latitude and longitudes :
> #define plus_int32( a, b ) int32_t( (uint32_t)(a) + (uint32_t)(b) )
> #define minus_int32( a, b ) int32_t( (uint32_t)(a) - (uint32_t)(b) )

There's a whiff of C++ there, but I think it's just a typo.

> Would this produce the same code as setting the "-fwrapv" flag?
>
> Doesn't casting between signed and unsigned integers present a similar
> wraparound problem?

Converting an out of range value to int32_t is implementation defined:

"the ... type is signed and the value cannot be represented in it;
either the result is implementation-defined or an
implementation-defined signal is raised."

You can do the conversion safely by first testing if the unsigned value
is greater than INT32_MAX and subtracting if it is.

But do you really need the value to be signed? If any kind of quantity
matches what C's unsigned ints do, it's angles. You need conversion on
input an output, but that's going t involve other factors as well.

> Isn't this also dependent on a one's or two's complement environment,
> and therefore has "undefined behaviour"?

All of the intN_t types are guaranteed to use two's complement for
negative values (and to have no padding bits).

--
Ben.

Re: Signed integer overflow

<874k8mz4p7.fsf@bsb.me.uk>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: ben.use...@bsb.me.uk (Ben Bacarisse)
Newsgroups: comp.lang.c
Subject: Re: Signed integer overflow
Date: Mon, 08 Nov 2021 22:02:28 +0000
Organization: A noiseless patient Spider
Lines: 18
Message-ID: <874k8mz4p7.fsf@bsb.me.uk>
References: <38ba6059-8d52-4f64-9ce6-8d23b82203f6n@googlegroups.com>
<10b5cc02-a50f-4b20-b651-d7fabe4af9f3n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="ae245c3fc7f87ec92e88ec1c6c67efcd";
logging-data="8195"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+FlEgykKVJ2/GHB/maHVGS4NHTbCHDEs0="
Cancel-Lock: sha1:sTnuZF7WtQ8LrnGtKMeHRKRkgYo=
sha1:zlSHiwUOY+E5Lg2IqwZManpAdJk=
X-BSB-Auth: 1.89faf9125a502b1bbc58.20211108220228GMT.874k8mz4p7.fsf@bsb.me.uk
 by: Ben Bacarisse - Mon, 8 Nov 2021 22:02 UTC

Eric Sosman <donalhambra@gmail.com> writes:

> On Monday, November 8, 2021 at 12:56:23 PM UTC-5, how...@gmail.com wrote:
>>
>> 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.
>
> Use a uint32_t variable running from 0 degrees to 360-360/4294967296 degrees.
> Wraparound does exactly what you want, and it's portable.

You beat me to it.

(If you are the same Eric Sosman, welcome back. If not, welcome.)

--
Ben.

Re: Signed integer overflow

<87mtmew91e.fsf@nosuchdomain.example.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: Signed integer overflow
Date: Mon, 08 Nov 2021 14:57:01 -0800
Organization: None to speak of
Lines: 13
Message-ID: <87mtmew91e.fsf@nosuchdomain.example.com>
References: <38ba6059-8d52-4f64-9ce6-8d23b82203f6n@googlegroups.com>
<2leiJ.15504$SW5.13829@fx45.iad>
<e716ee2e-e1ec-483e-a85d-ce1e3bb708d9n@googlegroups.com>
<87a6iez4sg.fsf@bsb.me.uk>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="8f220a377d54f8dc9b1bb1b71322e466";
logging-data="31218"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19lrJ1gHKpnEs1mlffSXPwg"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:eY1oVwlhbFyAOSrq0MV/w/KJ+JA=
sha1:MEFzDx8lhl05U2h9l8pGW21nrmE=
 by: Keith Thompson - Mon, 8 Nov 2021 22:57 UTC

Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
[...]
> All of the intN_t types are guaranteed to use two's complement for
> negative values (and to have no padding bits).

But, to be clear, the requirement of two's-complement representation
does not imply a requirement for two's-complement-style wraparound
behavior. (Ben, I'm sure you know this.)

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Philips
void Void(void) { Void(); } /* The recursive call of the void */

Re: Signed integer overflow

<smccls$if3$1@dont-email.me>

  copy mid

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

  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: Mon, 8 Nov 2021 23:42:55 +0000
Organization: A noiseless patient Spider
Lines: 33
Message-ID: <smccls$if3$1@dont-email.me>
References: <38ba6059-8d52-4f64-9ce6-8d23b82203f6n@googlegroups.com>
<10b5cc02-a50f-4b20-b651-d7fabe4af9f3n@googlegroups.com>
<7d185487-109c-4359-a44f-ae209f95285an@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Mon, 8 Nov 2021 23:42:52 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="ce83966d824a2449bbe6eca6ad3719b1";
logging-data="18915"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19NGEGGVuTqPj8k+Xsb0VT03kHSDJsVLU8="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.3.0
Cancel-Lock: sha1:/cs70p7rJuwkx/ouGdHfuVJrMQ0=
In-Reply-To: <7d185487-109c-4359-a44f-ae209f95285an@googlegroups.com>
 by: Bart - Mon, 8 Nov 2021 23:42 UTC

On 08/11/2021 20:49, Howerd Oakford wrote:
> On Monday, November 8, 2021 at 8:25:35 PM UTC+1, Eric Sosman wrote:
>> On Monday, November 8, 2021 at 12:56:23 PM UTC-5, 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.
>> Use a uint32_t variable running from 0 degrees to 360-360/4294967296 degrees.
>> Wraparound does exactly what you want, and it's portable.
> Hi Eric,
>
> So 1 degree West would be 359 degrees East - this is a great idea - thanks!

Just a little confusing to everyone used to using 0-180 degrees West
(Google maps doesn't like 181E-359E for a start.)

But, it's still not clear to me why you need to do this at all, or how
you will work with it in practice.

You mentioned incrementing, but if you want to increment by 1° say, it
means adding, what, 11930464 or 11930465?

If you wanted to add a offset X which can be positive or negative, how
is that going to work? Instead of -30°, do you add +330° instead?

(Personally, if I couldn't use floats, and had to use a 32-bit int, I
might use int32 with range of +/- 180 million, degrees*1000000, with
wraparound handled in software. [-180'000'000 .. +179'999'999]

To increment by 1°, add 1 million. For an offset of -30°, use -30
million. Normalise the results of any arithmetic to keep in the expected
range. But I don't know how precise you need this to be. This only gives
10cm resolution.)

Re: Signed integer overflow

<LyjiJ.37037$ya3.3432@fx38.iad>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!news.uzoreto.com!newsfeed.xs4all.nl!newsfeed9.news.xs4all.nl!nntp.speedium.network!feeder01!81.171.65.13.MISMATCH!peer01.ams4!peer.am4.highwinds-media.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx38.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>
<10b5cc02-a50f-4b20-b651-d7fabe4af9f3n@googlegroups.com>
<7d185487-109c-4359-a44f-ae209f95285an@googlegroups.com>
<smccls$if3$1@dont-email.me>
From: Rich...@Damon-Family.org (Richard Damon)
In-Reply-To: <smccls$if3$1@dont-email.me>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Lines: 50
Message-ID: <LyjiJ.37037$ya3.3432@fx38.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: Mon, 8 Nov 2021 19:47:38 -0500
X-Received-Bytes: 3368
 by: Richard Damon - Tue, 9 Nov 2021 00:47 UTC

On 11/8/21 6:42 PM, Bart wrote:
> On 08/11/2021 20:49, Howerd Oakford wrote:
>> On Monday, November 8, 2021 at 8:25:35 PM UTC+1, Eric Sosman wrote:
>>> On Monday, November 8, 2021 at 12:56:23 PM UTC-5, 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.
>>> Use a uint32_t variable running from 0 degrees to 360-360/4294967296
>>> degrees.
>>> Wraparound does exactly what you want, and it's portable.
>> Hi Eric,
>>
>> So 1 degree West would be 359 degrees East - this is a great idea -
>> thanks!
>
> Just a little confusing to everyone used to using 0-180 degrees West
> (Google maps doesn't like 181E-359E for a start.)
>
> But, it's still not clear to me why you need to do this at all, or how
> you will work with it in practice.
>
> You mentioned incrementing, but if you want to increment by 1° say, it
> means adding, what, 11930464 or 11930465?
>
> If you wanted to add a offset X which can be positive or negative, how
> is that going to work? Instead of -30°, do you add +330° instead?
>
> (Personally, if I couldn't use floats, and had to use a 32-bit int, I
> might use int32 with range of +/- 180 million, degrees*1000000, with
> wraparound handled in software. [-180'000'000 .. +179'999'999]
>
> To increment by 1°, add 1 million. For an offset of -30°, use -30
> million. Normalise the results of any arithmetic to keep in the expected
> range. But I don't know how precise you need this to be. This only gives
> 10cm resolution.)

Point to note, if you use 32 bit floats, instead of 32 bit integers, you
can store the range from -180 to +180 with 16+ fractional bits, which
gives better than 2 meter accuracy if I am doing my math right. (Angles
out side of -127 to +127 will be stored with 1 implied + 7 interger bits
+ 16 fractional bits of a degree, which at the equator is about
40,000,000m / 360 / 65536 ~ 1.7 meters resolution.)

Again, at that resolution, you are needing to do local corrections to
the reference spheroid for those numbers to be meaningful.

Re: Signed integer overflow

<87k0hixgsi.fsf@bsb.me.uk>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: ben.use...@bsb.me.uk (Ben Bacarisse)
Newsgroups: comp.lang.c
Subject: Re: Signed integer overflow
Date: Tue, 09 Nov 2021 01:24:13 +0000
Organization: A noiseless patient Spider
Lines: 18
Message-ID: <87k0hixgsi.fsf@bsb.me.uk>
References: <38ba6059-8d52-4f64-9ce6-8d23b82203f6n@googlegroups.com>
<2leiJ.15504$SW5.13829@fx45.iad>
<e716ee2e-e1ec-483e-a85d-ce1e3bb708d9n@googlegroups.com>
<87a6iez4sg.fsf@bsb.me.uk> <87mtmew91e.fsf@nosuchdomain.example.com>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="85c53abacd32481a9eba26655465b427";
logging-data="20761"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18OFv8z6uONyXp1Aeex2aUvpfwg0oyxc38="
Cancel-Lock: sha1:iX/sS4C8AOITrJDPfa+YNzqm3QE=
sha1:1e8+37Y9EfQ9ommYOKbgvTYX7s0=
X-BSB-Auth: 1.f26f10f613e237e7e334.20211109012413GMT.87k0hixgsi.fsf@bsb.me.uk
 by: Ben Bacarisse - Tue, 9 Nov 2021 01:24 UTC

Keith Thompson <Keith.S.Thompson+u@gmail.com> writes:

> Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
> [...]
>> All of the intN_t types are guaranteed to use two's complement for
>> negative values (and to have no padding bits).
>
> But, to be clear, the requirement of two's-complement representation
> does not imply a requirement for two's-complement-style wraparound
> behavior. (Ben, I'm sure you know this.)

Yes, thanks. The context could have given that impression. The
significance is that intN_t and uintN_t have the same number of values
and that intN_t has one move negative value that positive so one can
always get the test and subtract conversion to work.

--
Ben.

Re: Signed integer overflow

<90a0bfb9-6af3-4ca6-8380-7cfe21a9dc49n@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:620a:2a10:: with SMTP id o16mr4204497qkp.357.1636445642025;
Tue, 09 Nov 2021 00:14:02 -0800 (PST)
X-Received: by 2002:ac8:58cf:: with SMTP id u15mr6298937qta.334.1636445641846;
Tue, 09 Nov 2021 00:14:01 -0800 (PST)
Path: i2pn2.org!i2pn.org!aioe.org!news.uzoreto.com!2.eu.feeder.erje.net!feeder.erje.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: Tue, 9 Nov 2021 00:14:01 -0800 (PST)
In-Reply-To: <smccls$if3$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=195.78.249.52; posting-account=o1zNBwoAAABP2KiH5g6nPQp_sYWidVdt
NNTP-Posting-Host: 195.78.249.52
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>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <90a0bfb9-6af3-4ca6-8380-7cfe21a9dc49n@googlegroups.com>
Subject: Re: Signed integer overflow
From: howe...@gmail.com (Howerd Oakford)
Injection-Date: Tue, 09 Nov 2021 08:14:02 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
 by: Howerd Oakford - Tue, 9 Nov 2021 08:14 UTC

On Tuesday, November 9, 2021 at 12:43:03 AM UTC+1, Bart wrote:
> On 08/11/2021 20:49, Howerd Oakford wrote:
> > On Monday, November 8, 2021 at 8:25:35 PM UTC+1, Eric Sosman wrote:
> >> On Monday, November 8, 2021 at 12:56:23 PM UTC-5, 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.
> >> Use a uint32_t variable running from 0 degrees to 360-360/4294967296 degrees.
> >> Wraparound does exactly what you want, and it's portable.
> > Hi Eric,
> >
> > So 1 degree West would be 359 degrees East - this is a great idea - thanks!
> Just a little confusing to everyone used to using 0-180 degrees West
> (Google maps doesn't like 181E-359E for a start.)
>
> But, it's still not clear to me why you need to do this at all, or how
> you will work with it in practice.
>
> You mentioned incrementing, but if you want to increment by 1° say, it
> means adding, what, 11930464 or 11930465?
>
> If you wanted to add a offset X which can be positive or negative, how
> is that going to work? Instead of -30°, do you add +330° instead?
>
> (Personally, if I couldn't use floats, and had to use a 32-bit int, I
> might use int32 with range of +/- 180 million, degrees*1000000, with
> wraparound handled in software. [-180'000'000 .. +179'999'999]
>
> To increment by 1°, add 1 million. For an offset of -30°, use -30
> million. Normalise the results of any arithmetic to keep in the expected
> range. But I don't know how precise you need this to be. This only gives
> 10cm resolution.)

Hi Bart,

> But, it's still not clear to me why you need to do this at all, or how
> you will work with it in practice.
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 converted it to an int32_t.

The hardware and ARM16 ASM are OK with wrapping this value, but C (correctly) complains because this depends on a two's complement environment.

Using a float does not give enough resoultion, using a double does, but either type requires two run-time conversions.

Using a smaller range requires extra run-time code to do the modulo calculation.

The use case for this is finding the distance between two points given their latitude and longitude, so it is useful if the distance calculation wraps so that moving 1 step West of -180 degrees gives you +179.9999.. East.

My prefered solution is to cast to uint_32_t and then back to int_32_t - thank you, Eric, Ben and David and all who suggested this.

With "-fwrapv" set the cast may not be necessary - this would be a "mental cast" - just think of the value as 0 to 359.999.. degrees instead of -180 to 179.999... degrees.
Since we know this is a two's complement system we know that this works.

Thanks again y'all,

Cheers,
Howerd

Re: Signed integer overflow

<87ee7py50b.fsf@bsb.me.uk>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: ben.use...@bsb.me.uk (Ben Bacarisse)
Newsgroups: comp.lang.c
Subject: Re: Signed integer overflow
Date: Tue, 09 Nov 2021 10:53:24 +0000
Organization: A noiseless patient Spider
Lines: 21
Message-ID: <87ee7py50b.fsf@bsb.me.uk>
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
Injection-Info: reader02.eternal-september.org; posting-host="85c53abacd32481a9eba26655465b427";
logging-data="13771"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19DFXPnm+HOLbCFdRfy2BnO0UxQ7fkPgA4="
Cancel-Lock: sha1:Od3g14cii63M6XpYwsGbBO2rM2A=
sha1:1X9cEJUM027GldBwhTsyqBWyLV4=
X-BSB-Auth: 1.6d36b84e0a6d24fa18b3.20211109105324GMT.87ee7py50b.fsf@bsb.me.uk
 by: Ben Bacarisse - Tue, 9 Nov 2021 10:53 UTC

Howerd Oakford <howerdo@gmail.com> writes:

> My prefered solution is to cast to uint_32_t and then back to int_32_t
> - thank you, Eric, Ben and David and all who suggested this.

Hmm... That was not was what I was suggesting! But it may be a
confusion over terms. A cast is a C operator, but the word is often
used to mean any conversion, even one involving calculation.

You can safely cast (or just implicitly convert) an int32_t to uint32_t
when processing the input. All calculations on the unsigned values will
just warp (on all C implementations on all hardware) and you can then
safely convert (using a simple test and subtraction) back to signed if
that helps with presenting the information.

However, I doubt you even need this. Whatever you need to do to present
the results can be done from an unsigned 0-360 number as easily as a
signed one.

--
Ben.

Re: Signed integer overflow

<smdk20$dtr$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: Signed integer overflow
Date: Tue, 9 Nov 2021 11:54:56 +0100
Organization: A noiseless patient Spider
Lines: 129
Message-ID: <smdk20$dtr$1@dont-email.me>
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=utf-8
Content-Transfer-Encoding: 8bit
Injection-Date: Tue, 9 Nov 2021 10:54:56 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="9f6ac3f674544c1fc0b596b3c9a47d26";
logging-data="14267"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+omaBX+2nSd7FMjiukHdpbNRH5ZTZHSm4="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
Cancel-Lock: sha1:ph8hyYpRTHI5futy3ueypARDHH0=
In-Reply-To: <90a0bfb9-6af3-4ca6-8380-7cfe21a9dc49n@googlegroups.com>
Content-Language: en-GB
 by: David Brown - Tue, 9 Nov 2021 10:54 UTC

On 09/11/2021 09:14, Howerd Oakford wrote:
> On Tuesday, November 9, 2021 at 12:43:03 AM UTC+1, Bart wrote:
>> On 08/11/2021 20:49, Howerd Oakford wrote:
>>> On Monday, November 8, 2021 at 8:25:35 PM UTC+1, Eric Sosman
>>> wrote:
>>>> On Monday, November 8, 2021 at 12:56:23 PM UTC-5,
>>>> 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.
>>>> Use a uint32_t variable running from 0 degrees to
>>>> 360-360/4294967296 degrees. Wraparound does exactly what you
>>>> want, and it's portable.
>>> Hi Eric,
>>>
>>> So 1 degree West would be 359 degrees East - this is a great idea
>>> - thanks!
>> Just a little confusing to everyone used to using 0-180 degrees
>> West (Google maps doesn't like 181E-359E for a start.)
>>
>> But, it's still not clear to me why you need to do this at all, or
>> how you will work with it in practice.
>>
>> You mentioned incrementing, but if you want to increment by 1° say,
>> it means adding, what, 11930464 or 11930465?
>>
>> If you wanted to add a offset X which can be positive or negative,
>> how is that going to work? Instead of -30°, do you add +330°
>> instead?
>>
>> (Personally, if I couldn't use floats, and had to use a 32-bit int,
>> I might use int32 with range of +/- 180 million, degrees*1000000,
>> with wraparound handled in software. [-180'000'000 .. +179'999'999]
>>
>>
>> To increment by 1°, add 1 million. For an offset of -30°, use -30
>> million. Normalise the results of any arithmetic to keep in the
>> expected range. But I don't know how precise you need this to be.
>> This only gives 10cm resolution.)
>
> Hi Bart,
>
>> But, it's still not clear to me why you need to do this at all, or
>> how you will work with it in practice.
> 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 converted it to an int32_t.
>

What is the scaling? Without knowing that, all your points about
wrapping, formats, etc., are /useless/.

> The hardware and ARM16 ASM are OK with wrapping this value, but C
> (correctly) complains because this depends on a two's complement
> environment.

Forget the hardware. Forget assembly. Forget everything you think you
know about efficiency here, because you are almost certainly wrong
unless you have done extensive measurements to show that you need to use
some kind of hardware wrapping to get fast enough results.

Forget the whole concept of wrapping signed integer arithmetic. C does
not give you that - regardless of whether you have two's complement
signed integers (the representation of the integers is independent from
the treatment of arithmetic overflows).

There /are/ ways to get safe, guaranteed wrapping behaviour for
arithmetic involving signed integers. These are almost certainly
counter-productive to your needs because you've got obsessed with those
instead of looking at what makes sense for your code and what types,
formats, scalings and operations you actually /need/ and what makes the
source code clearer and more maintainable.

>
> Using a float does not give enough resoultion, using a double does,
> but either type requires two run-time conversions.
>

A single-precision float will give you a resolution of about 2.5 meters.
What are you doing that needs greater precision than that?

A typical 64-bit ARM processor (I believe that is what you are using)
will do conversion from an integer to a double in a nanosecond or so.
What are you doing where that nanosecond is too long?

Conversion from a string of digits to an integer might take several
hundred clock cycles. Conversion to a double will take several hundred
plus 2 or 3 clock cycles. Why do you think it is remotely relevant?

> Using a smaller range requires extra run-time code to do the modulo
> calculation.
>
> The use case for this is finding the distance between two points
> given their latitude and longitude, so it is useful if the distance
> calculation wraps so that moving 1 step West of -180 degrees gives
> you +179.9999.. East.
>

It is not the /slightest/ bit useful for such calculations. Have you
looked at the maths for doing this distance calculation? Making a
cut-off, whether it be at -180 degrees or 360 degrees, is
counter-productive. Your distance calculations will all be done in
floating point unless you are working on an 8-bit AVR or you are insane,
meaning a scaled integer format is just pointless extra work before you
can do the mathematics. And those calculations will be so extensive
that any modulo calculations are lost in the noise.

> My prefered solution is to cast to uint_32_t and then back to
> int_32_t - thank you, Eric, Ben and David and all who suggested
> this.
>

That is a good way to get wrapping signed integer arithmetic. But from
what you have written here, it is worse than useless for your needs
because it is a massive distraction from doing something useful.

Basically, you are bikesheding. Don't do that.

> With "-fwrapv" set the cast may not be necessary - this would be a
> "mental cast" - just think of the value as 0 to 359.999.. degrees
> instead of -180 to 179.999... degrees. Since we know this is a two's
> complement system we know that this works.
>

Re: Signed integer overflow

<sme1j6$nc7$1@dont-email.me>

  copy mid

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

  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: Tue, 9 Nov 2021 14:46:02 +0000
Organization: A noiseless patient Spider
Lines: 47
Message-ID: <sme1j6$nc7$1@dont-email.me>
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=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 9 Nov 2021 14:45:58 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="ce83966d824a2449bbe6eca6ad3719b1";
logging-data="23943"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19cqbPU39+l5jDHvf+65feU5bYDcM5j92A="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.3.0
Cancel-Lock: sha1:xBCx9i42CRwlvQjoJetFsUoSlek=
In-Reply-To: <90a0bfb9-6af3-4ca6-8380-7cfe21a9dc49n@googlegroups.com>
 by: Bart - Tue, 9 Nov 2021 14:46 UTC

On 09/11/2021 08:14, Howerd Oakford wrote:
> On Tuesday, November 9, 2021 at 12:43:03 AM UTC+1, Bart wrote:

>> But, it's still not clear to me why you need to do this at all, or how
>> you will work with it in practice.

> Because we receive the data as a string in the form "123456789" or "-123456789" , and the Schema for

What is the actual format: decimal radians, decimal degrees,
degrees:minutes:decimal seconds?

the data format refers to this as an int32_t.
> So naturally, I converted it to an int32_t.
>
> The hardware and ARM16 ASM are OK with wrapping this value, but C (correctly) complains because this depends on a two's complement environment.
>
> Using a float does not give enough resoultion, using a double does, but either type requires two run-time conversions.

Whether your latitude/longitude readings are radians or degrees, the
sort of calculations you describe scream floating point.

If float32 is too inprecise, use float64.

I was reluctant to use float64 35 years ago, because I needed software
emulation and it was slower. But for at least 30 years now hardware
float64 has been available to everyone.

I was obliged to switch eventually because a customer of my graphics app
was using it to represent geographical mapping data, and float32 was too
imprecise for absolute data points when coordinates were a long way from
zero.

I can't believe that decades later, this is still a consideration even
on a board computer. Any ARM device will be a supercomputer compared
with what I've used!

> Using a smaller range requires extra run-time code to do the modulo calculation.
>
> The use case for this is finding the distance between two points given their latitude and longitude, so it is useful if the distance calculation wraps so that moving 1 step West of -180 degrees gives you +179.9999.. East.

You seem to be hung-up on this aspect. My aforementioned app also worked
with angles, which were normalised, in software, to:

0 <= angle < 2 pi

in my case.

Re: Signed integer overflow

<8afeebeb-053b-4c85-821b-19437e0ba5acn@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:622a:1716:: with SMTP id h22mr8985241qtk.224.1636470219646;
Tue, 09 Nov 2021 07:03:39 -0800 (PST)
X-Received: by 2002:a05:6214:238e:: with SMTP id fw14mr7859894qvb.47.1636470219481;
Tue, 09 Nov 2021 07:03:39 -0800 (PST)
Path: i2pn2.org!i2pn.org!paganini.bofh.team!news.dns-netz.com!news.freedyn.net!newsreader4.netcologne.de!news.netcologne.de!peer02.ams1!peer.ams1.xlned.com!news.xlned.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Tue, 9 Nov 2021 07:03:39 -0800 (PST)
In-Reply-To: <38ba6059-8d52-4f64-9ce6-8d23b82203f6n@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=84.50.190.130; posting-account=pysjKgkAAACLegAdYDFznkqjgx_7vlUK
NNTP-Posting-Host: 84.50.190.130
References: <38ba6059-8d52-4f64-9ce6-8d23b82203f6n@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <8afeebeb-053b-4c85-821b-19437e0ba5acn@googlegroups.com>
Subject: Re: Signed integer overflow
From: oot...@hot.ee (Öö Tiib)
Injection-Date: Tue, 09 Nov 2021 15:03:39 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Received-Bytes: 2517
 by: Öö Tiib - Tue, 9 Nov 2021 15:03 UTC

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.

Re: Signed integer overflow

<7c19b204-be96-4f1e-aa1c-1c96d1a34733n@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:6214:20ab:: with SMTP id 11mr15173648qvd.31.1636550095705;
Wed, 10 Nov 2021 05:14:55 -0800 (PST)
X-Received: by 2002:a05:620a:4051:: with SMTP id i17mr12738200qko.333.1636550095106;
Wed, 10 Nov 2021 05:14:55 -0800 (PST)
Path: i2pn2.org!i2pn.org!aioe.org!feeder1.feed.usenet.farm!feed.usenet.farm!newsfeed.xs4all.nl!newsfeed9.news.xs4all.nl!border2.nntp.ams1.giganews.com!nntp.giganews.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: Wed, 10 Nov 2021 05:14:54 -0800 (PST)
In-Reply-To: <8afeebeb-053b-4c85-821b-19437e0ba5acn@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=2a01:c22:b84c:b300:5175:b20a:a754:2704;
posting-account=o1zNBwoAAABP2KiH5g6nPQp_sYWidVdt
NNTP-Posting-Host: 2a01:c22:b84c:b300:5175:b20a:a754:2704
References: <38ba6059-8d52-4f64-9ce6-8d23b82203f6n@googlegroups.com> <8afeebeb-053b-4c85-821b-19437e0ba5acn@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <7c19b204-be96-4f1e-aa1c-1c96d1a34733n@googlegroups.com>
Subject: Re: Signed integer overflow
From: howe...@gmail.com (Howerd Oakford)
Injection-Date: Wed, 10 Nov 2021 13:14:55 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Lines: 38
 by: Howerd Oakford - Wed, 10 Nov 2021 13:14 UTC

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

Pages:12
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor