Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: C arithmetic question. |
| Date | 2012-01-24 12:39 -0500 |
| Organization | A noiseless patient Spider |
| Message-ID | <4F1EECD3.6050809@verizon.net> (permalink) |
| References | (3 earlier) <1bhazss041.fsf@pfeifferfamily.net> <rubrum-72E0E9.23142118012012@news.albasani.net> <4471b54c-34d0-407b-9a89-845a8b3f0380@n7g2000pbd.googlegroups.com> <4F1EE746.9040609@verizon.net> <c43860a7-d8f2-4eda-838a-40beb45de4c1@3g2000pbd.googlegroups.com> |
On 01/24/2012 12:26 PM, Chad wrote: > On Jan 24, 9:15 am, James Kuyper <jameskuy...@verizon.net> wrote: ... >> What's causing this C code to correctly produce a value other than the >> one Michael was expecting is the fact that 'a' has the type 'long', >> while 'b' has the type 'unsigned long', with the result that 'a' is >> converted to the 'unsigned long' before evaluating the modulus. The >> conversion produces the value ULONG_MAX-57, which is positive, so that >> a%b produces a value of 21. Michael's assertion to the contrary >> notwithstanding, a - a%b is in fact divisible by b (because the same >> conversion occurs to both occurrences of 'a'). If Michael had written >> >> a %= (long)b; >> >> b would have been converted to long, without change of value, and he >> would have gotten a result of -21, which violates the requirement you >> mention, but satisfies the one he mentions. Whether that's the value he >> was expecting is unclear; he might have been expecting 16.cd- Hide quoted text - >> > > Maybe I'm being a bit daft, but your explanation sort of mirrors how > you would go about dividing something like -11 by 3 when the remainder > can't be negative. I'm not sure exactly what you're saying. The C standard requires a) (-11)/3 == -3 b) ((-11)/3)*3 + (-11)%3 == -11 which means that (-11)%3 == -2. It also requires that, on a system where ULONG_MAX == 4294967295, that a) (-11UL)/3 == 1431655761 b) ((-11UL)/3)*3 + (-11UL)%3 == -11UL which means that (-11UL)%3 == 2. That's because the expression -11UL has a value of 4294967285. Michael didn't realize that he was writing the equivalent of such an expression, because he wasn't paying attention to the usual arithmetic conversions.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
C arithmetic question. Michael Press <rubrum@pacbell.net> - 2012-01-18 10:32 -0800
Re: C arithmetic question. James Kuyper <jameskuyper@verizon.net> - 2012-01-18 14:03 -0500
Re: C arithmetic question. TheGunslinger <mikiesrunsbaal.sec@sbcglobal.net> - 2012-01-18 19:30 -0800
Re: C arithmetic question. James Kuyper <jameskuyper@verizon.net> - 2012-01-19 08:10 -0500
Re: C arithmetic question. Markus Wichmann <nullplan@gmx.net> - 2012-01-18 21:14 +0100
Re: C arithmetic question. "BartC" <bc@freeuk.com> - 2012-01-19 10:44 +0000
Re: C arithmetic question. Seebs <usenet-nospam@seebs.net> - 2012-01-21 21:32 +0000
Re: C arithmetic question. Peter Nilsson <airia@acay.com.au> - 2012-01-18 13:51 -0800
Re: C arithmetic question. Michael Press <rubrum@pacbell.net> - 2012-01-18 15:51 -0800
Re: C arithmetic question. James Kuyper <jameskuyper@verizon.net> - 2012-01-18 20:50 -0500
Re: C arithmetic question. Michael Press <rubrum@pacbell.net> - 2012-01-20 12:41 -0800
Re: C arithmetic question. Keith Thompson <kst-u@mib.org> - 2012-01-20 14:11 -0800
Re: C arithmetic question. gazelle@shell.xmission.com (Kenny McCormack) - 2012-01-20 22:43 +0000
Re: C arithmetic question. James Kuyper <jameskuyper@verizon.net> - 2012-01-20 17:19 -0500
Re: C arithmetic question. James Kuyper <jameskuyper@verizon.net> - 2012-01-20 17:32 -0500
And jacob navia <jacob@spamsink.net> - 2012-01-20 23:52 +0100
Re: And Markus Wichmann <nullplan@gmx.net> - 2012-01-21 19:23 +0100
Re: And Keith Thompson <kst-u@mib.org> - 2012-01-21 13:20 -0800
Re: And jacob navia <jacob@spamsink.net> - 2012-01-21 23:02 +0100
Re: And Markus Wichmann <nullplan@gmx.net> - 2012-01-22 15:24 +0100
Re: And Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-01-22 15:40 +0000
Re: And blp@cs.stanford.edu (Ben Pfaff) - 2012-01-21 11:02 -0800
Re: C arithmetic question. Rui Maciel <rui.maciel@gmail.com> - 2012-01-24 18:53 +0000
Re: C arithmetic question. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-01-19 02:09 +0000
Re: C arithmetic question. Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2012-01-18 23:00 -0700
Re: C arithmetic question. Michael Press <rubrum@pacbell.net> - 2012-01-18 23:14 -0800
Re: C arithmetic question. Ike Naar <ike@sverige.freeshell.org> - 2012-01-19 08:41 +0000
Re: C arithmetic question. Michael Press <rubrum@pacbell.net> - 2012-01-20 12:43 -0800
Re: C arithmetic question. Kaz Kylheku <kaz@kylheku.com> - 2012-01-20 22:17 +0000
Re: C arithmetic question. Michael Press <rubrum@pacbell.net> - 2012-01-21 00:12 -0800
Re: C arithmetic question. Seebs <usenet-nospam@seebs.net> - 2012-01-21 21:32 +0000
Re: C arithmetic question. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-01-21 23:21 +0000
Re: C arithmetic question. Robert Wessel <robertwessel2@yahoo.com> - 2012-01-22 00:48 -0600
Re: C arithmetic question. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-01-22 11:45 +0000
Re: C arithmetic question. Robert Wessel <robertwessel2@yahoo.com> - 2012-01-22 21:46 -0600
Re: C arithmetic question. Kaz Kylheku <kaz@kylheku.com> - 2012-01-23 04:17 +0000
Re: C arithmetic question. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-01-23 11:19 +0000
Re: C arithmetic question. Tim Rentsch <txr@alumni.caltech.edu> - 2012-01-26 10:41 -0800
Re: C arithmetic question. Seebs <usenet-nospam@seebs.net> - 2012-01-23 07:58 +0000
Re: C arithmetic question. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-01-23 11:06 +0000
Re: C arithmetic question. James Kuyper <jameskuyper@verizon.net> - 2012-01-23 07:03 -0500
Re: C arithmetic question. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-01-23 17:27 +0000
Re: C arithmetic question. Keith Thompson <kst-u@mib.org> - 2012-01-19 02:38 -0800
Re: C arithmetic question. Dr Nick <3-nospam@temporary-address.org.uk> - 2012-01-20 07:30 +0000
Re: C arithmetic question. Nomen Nescio <nobody@dizum.com> - 2012-01-19 12:28 +0100
Re: C arithmetic question. Michael Press <rubrum@pacbell.net> - 2012-01-20 12:44 -0800
Re: C arithmetic question. 88888 Dihedral <dihedral88888@googlemail.com> - 2012-01-20 13:14 -0800
Re: C arithmetic question. James Kuyper <jameskuyper@verizon.net> - 2012-01-19 08:45 -0500
Re: C arithmetic question. Chad <cdalten@gmail.com> - 2012-01-24 08:14 -0800
Re: C arithmetic question. James Kuyper <jameskuyper@verizon.net> - 2012-01-24 12:15 -0500
Re: C arithmetic question. Chad <cdalten@gmail.com> - 2012-01-24 09:26 -0800
Re: C arithmetic question. James Kuyper <jameskuyper@verizon.net> - 2012-01-24 12:39 -0500
Re: C arithmetic question. Chad <cdalten@gmail.com> - 2012-01-24 10:03 -0800
Re: C arithmetic question. James Kuyper <jameskuyper@verizon.net> - 2012-01-24 13:23 -0500
Re: C arithmetic question. Chad <cdalten@gmail.com> - 2012-01-24 10:40 -0800
Re: C arithmetic question. James Kuyper <jameskuyper@verizon.net> - 2012-01-24 14:35 -0500
Re: C arithmetic question. Chad <cdalten@gmail.com> - 2012-01-24 12:04 -0800
Re: C arithmetic question. Chad <cdalten@gmail.com> - 2012-01-24 12:02 -0800
Re: C arithmetic question. Kleuske <kleuske@nowhere.net> - 2012-01-24 20:26 +0000
Re: C arithmetic question. Chad <cdalten@gmail.com> - 2012-01-24 13:45 -0800
Re: C arithmetic question. James Kuyper <jameskuyper@verizon.net> - 2012-01-24 15:40 -0500
Re: C arithmetic question. Chad <cdalten@gmail.com> - 2012-01-24 12:09 -0800
Re: C arithmetic question. Rui Maciel <rui.maciel@gmail.com> - 2012-01-24 18:48 +0000
Re: C arithmetic question. "osmium" <r124c4u102@comcast.net> - 2012-01-19 08:12 -0600
Re: C arithmetic question. Geoff <geoff@invalid.invalid> - 2012-01-20 15:33 -0800
Re: C arithmetic question. Kleuske <kleuske@nowhere.net> - 2012-01-27 08:17 +0000
Re: C arithmetic question. Geoff <geoff@invalid.invalid> - 2012-01-27 13:37 -0800
csiph-web