Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #105294
| Path | csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail |
|---|---|
| From | Keith Thompson <kst-u@mib.org> |
| Newsgroups | comp.lang.c |
| Subject | Re: "usual arithmetic conversions": please explain something... |
| Date | Wed, 01 Mar 2017 10:15:30 -0800 |
| Organization | None to speak of |
| Lines | 79 |
| Message-ID | <ln4lzcdfst.fsf@kst-u.example.com> (permalink) |
| References | <448topoppn.fsf@be-well.ilk.org> <o951vq$ch6$2@dont-email.me> <ln1suhersb.fsf@kst-u.example.com> <o96d9b$p64$1@dont-email.me> <lnshmxc6bz.fsf@kst-u.example.com> <o96tr9$u0t$1@dont-email.me> <ln8toodhad.fsf@kst-u.example.com> <o9723r$ema$1@dont-email.me> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=us-ascii |
| Injection-Info | mx02.eternal-september.org; posting-host="40f2111bd31821bfebd41309549fe646"; logging-data="21586"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18Z5U3khQtYTgYKu3Uh15YK" |
| User-Agent | Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) |
| Cancel-Lock | sha1:UE6d64SEEHwdBGsPjpsMVEDsPso= sha1:J8H9aXcq7b7obOeMecU85Ed5URU= |
| Xref | csiph.com comp.lang.c:105294 |
Show key headers only | View raw
BartC <bc@freeuk.com> writes:
> On 01/03/2017 17:43, Keith Thompson wrote:
>> BartC <bc@freeuk.com> writes:
>
>>> I showed how to convert the result of a signed/signed,
>>> unsigned/unsigned, signed/unsigned or unsigned/signed to a float, which
>>> is a better idea.
>>
>> I showed how to print the actual values without converting them.
>
> Where did you do that?
printf("%lu\n", a * b);
(I originally wrote "%llu\n", which was a mistake, one that my compiler
would have warned me about if I had compiled that line of code.)
> I found it difficult to establish whether a*b was done in signed or
> unsigned mode, without examining the result.
The rules are in N1570 6.3.1.8, "Usual arithmetic conversions":
Otherwise, if the operand that has unsigned integer type has rank
greater or equal to the rank of the type of the other operand, then
the operand with signed integer type is converted to the type of the
operand with unsigned integer type.
The rules are complicated, but you need to understand them if you're
going to reason about arithmetic on operands of different types.
I suppose I can see the point of converting to a type like long double
whose range covers the expected range of values, such as long double --
but that's necessary only if you're unwilling to figure out what the
actual type is.
Here's a program that uses both your method and mine:
#include <stdio.h>
int main (void) {
unsigned long a = 4000;
signed long b = -10;
int c;
c = a * b;
long double ld = a * b;
printf("a * b = %lu\n", a * b);
printf("ld = %Lf\n", ld);
printf("c = %d\n", c);
}
and its output on my system (in 64-bit mode);
a * b = 18446744073709511616
ld = 18446744073709511616.000000
c = -40000
> And examining the result usually means stipulating whether it should be
> shown as signed or unsigned!
The standard already stipulates that the result is unsigned long.
[...]
> Again, how do you know that? By sweating over the minutia in the
> Standard? That's no way to do things if you're just learning; suppose
> you get it wrong?
If you get it wrong, you get it wrong. It happens. Some compilers will
warn you if you print a value with the wrong format for its type. (gcc,
at least by default, doesn't warn about using "%ld" with an unsigned
long argument.)
[...]
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
"usual arithmetic conversions": please explain something... Lowell Gilbert <lgusenet@be-well.ilk.org> - 2017-02-28 18:33 -0500
Re: "usual arithmetic conversions": please explain something... BartC <bc@freeuk.com> - 2017-02-28 23:45 +0000
Re: "usual arithmetic conversions": please explain something... Keith Thompson <kst-u@mib.org> - 2017-02-28 16:59 -0800
Re: "usual arithmetic conversions": please explain something... Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-03-01 02:20 +0000
Re: "usual arithmetic conversions": please explain something... Keith Thompson <kst-u@mib.org> - 2017-02-28 18:51 -0800
Re: "usual arithmetic conversions": please explain something... BartC <bc@freeuk.com> - 2017-03-01 12:04 +0000
Re: "usual arithmetic conversions": please explain something... Keith Thompson <kst-u@mib.org> - 2017-03-01 08:25 -0800
Re: "usual arithmetic conversions": please explain something... BartC <bc@freeuk.com> - 2017-03-01 16:47 +0000
Re: "usual arithmetic conversions": please explain something... Keith Thompson <kst-u@mib.org> - 2017-03-01 09:43 -0800
Re: "usual arithmetic conversions": please explain something... BartC <bc@freeuk.com> - 2017-03-01 18:00 +0000
Re: "usual arithmetic conversions": please explain something... Keith Thompson <kst-u@mib.org> - 2017-03-01 10:15 -0800
Re: "usual arithmetic conversions": please explain something... Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-03-01 19:08 +0000
Re: "usual arithmetic conversions": please explain something... supercat@casperkitty.com - 2017-03-01 09:01 -0800
Re: "usual arithmetic conversions": please explain something... jameskuyper@verizon.net - 2017-02-28 15:49 -0800
Re: "usual arithmetic conversions": please explain something... Keith Thompson <kst-u@mib.org> - 2017-02-28 16:03 -0800
Re: "usual arithmetic conversions": please explain something... Nick Bowler <nbowler@draconx.ca> - 2017-03-01 21:53 +0000
Re: "usual arithmetic conversions": please explain something... Keith Thompson <kst-u@mib.org> - 2017-03-01 14:18 -0800
Re: "usual arithmetic conversions": please explain something... Lowell Gilbert <lgusenet@be-well.ilk.org> - 2017-03-01 08:29 -0500
Re: "usual arithmetic conversions": please explain something... Lowell Gilbert <lgusenet@be-well.ilk.org> - 2017-03-01 09:11 -0500
Re: "usual arithmetic conversions": please explain something... Keith Thompson <kst-u@mib.org> - 2017-03-01 08:59 -0800
Re: "usual arithmetic conversions": please explain something... scott@slp53.sl.home (Scott Lurndal) - 2017-03-01 14:00 +0000
Re: "usual arithmetic conversions": please explain something... "James R. Kuyper" <jameskuyper@verizon.net> - 2017-03-01 10:58 -0500
csiph-web