Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: What is truncation for (singed) integer in C ? |
| Date | 2015-09-19 13:45 -0700 |
| Organization | None to speak of |
| Message-ID | <lna8sidtol.fsf@kst-u.example.com> (permalink) |
| References | <6f66f235-1b11-4b0c-b2d8-c49ac3eac78e@googlegroups.com> |
fl <rxjwg98@gmail.com> writes:
> I see the following on wikipedia and doubt about negative integer when
> truncating.
>
> https://en.wikipedia.org/wiki/Type_conversion
> ///////
> Data can be lost when floating-point representations are converted to
> integer representations as the fractional components of the
> floating-point values will be truncated (rounded towards zero)
> .......
>
> I agree for positive integer, truncation in C is towards 0. But for
> negative integer, it looks like away from '0'. For example, for a
> unsigned char 83H, one bit (LSB) truncation result will be 41H, whose
> non-truncation value (8-bit) is equal to 82H. The real value of 82H
> plus '1' is 83H. That is, 82H < 83H. For these negative numbers, it is
> not rounded towards zero.
>
> Is the above link not talking the same thing with my understanding?
The link is talking about conversion of *floating-point* values to
integers.
For example, (int)3.7 = 3, and (int)-3.7 == -3.
I'm not entirely clear on what your example is supposed to be. For
starters, C expresses a hexadecimal value as 0x83, not 83H. You appear
to be talking about truncating the result of dividing 0x83 by 2 (you
didn't mention the division). 0x83 / 2 == 0x41; for unsigned values,
integer division truncates. There are other rules for negative values
-- but 0x83 is positive, so those rules don't apply.
--
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
What is truncation for (singed) integer in C ? fl <rxjwg98@gmail.com> - 2015-09-19 12:25 -0700
Re: What is truncation for (singed) integer in C ? fir <profesor.fir@gmail.com> - 2015-09-19 12:55 -0700
Re: What is truncation for (singed) integer in C ? fir <profesor.fir@gmail.com> - 2015-09-19 13:20 -0700
Re: What is truncation for (singed) integer in C ? Geoff <geoff@invalid.invalid> - 2015-09-19 13:05 -0700
Re: What is truncation for (singed) integer in C ? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-09-19 21:28 +0100
Re: What is truncation for (singed) integer in C ? Tim Prince <tprince@computer.org> - 2015-09-19 16:38 -0400
Re: What is truncation for (singed) integer in C ? James Kuyper <jameskuyper@verizon.net> - 2015-09-19 16:52 -0400
Re: What is truncation for (singed) integer in C ? Keith Thompson <kst-u@mib.org> - 2015-09-19 13:45 -0700
Re: What is truncation for (singed) integer in C ? James Kuyper <jameskuyper@verizon.net> - 2015-09-19 16:46 -0400
Re: What is truncation for (singed) integer in C ? Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-09-19 16:47 -0400
Re: What is truncation for (singed) integer in C ? Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2015-09-19 22:55 -0600
Re: What is truncation for (singed) integer in C ? James Kuyper <jameskuyper@verizon.net> - 2015-09-20 10:54 -0400
Re: What is truncation for (singed) integer in C ? Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2015-09-20 10:11 -0600
csiph-web