Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #70939

Re: What is truncation for (singed) integer in C ?

From Tim Prince <tprince@computer.org>
Newsgroups comp.lang.c
Subject Re: What is truncation for (singed) integer in C ?
Date 2015-09-19 16:38 -0400
Message-ID <d65vdmFsqvaU1@mid.individual.net> (permalink)
References <6f66f235-1b11-4b0c-b2d8-c49ac3eac78e@googlegroups.com> <87twqq9msk.fsf@bsb.me.uk>

Show all headers | View raw


On 9/19/2015 4:28 PM, Ben Bacarisse wrote:
> 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,
> 
> Something is already wrong here.  You say "but for negative integer" and
> the then give an example of "unsigned char 83H".  unsigned char 83H is
> not negative -- it's unsigned.
> 
>> 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?
> 
> I think you are talking here about integer division by two.  The article
> you cite is about converting floating point values to integers so there
> is no direct connection.  The rules about integer division have changed
> between old C (ANSI C or ISO C from 1990) and modern C (C99 and later)
> but in modern C, the result is indeed truncated (rounded towards zero).
> 
> To use your example, signed char c = -125; will give c the bit-pattern
> 83 in hex.  Note that I can't say c == 0x83 because that is not correct
> in C -- do you know why?  Now c/2 is -125/2 which, truncated towards
> zero is -62.  That will "look like" C2 in hex, but, again, c/2 == 0xC2
> is not correct in C.
> 
My references imply that the definition with truncation towards 0 came
in with C89.  Before that, i/2 might be considered interchangeable with
i>>1 regardless of whether 1's or 2's complement was in use, even though
they give different results.
I would assume that casting from a floating point type to unsigned char
would involve an intermediate int representation, but OP seemed to lose
track of what was the question.

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


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