Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: What is truncation for (singed) integer in C ? |
| Date | 2015-09-19 16:46 -0400 |
| Organization | Self |
| Message-ID | <55FDC9AE.5010705@verizon.net> (permalink) |
| References | <6f66f235-1b11-4b0c-b2d8-c49ac3eac78e@googlegroups.com> |
On 09/19/2015 03:25 PM, fl wrote: > Hi, > > 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, You have a LOT of confusion here. The relevant clause in the C standard says: "When a finite value of real floating type is converted to an integer type other than _Bool, the fractional part is discarded (i.e., the value is truncated toward zero)." (6.3.1.4p1) The truncation it's talking about here involves only floating point values, and your example doesn't involve any floating point values. What it's saying is that if, for instance, a floating point value of 3.21 is converted to an integer type, the result is 3, obtained by truncating the "21" at the end. Similarly, value of -5.67 is converted to an integer type by truncating (removing) the "67", giving a value of -5. In both cases, the truncated value is closer to 0, which is why it's described as being truncated towards zero. > 82H < 83H. For these negative numbers, it is not rounded towards zero. What are "these negative numbers"? I don't see any negative numbers in your message. The only data type you mentioned is unsigned char, which is incapable of representing a negative number. The form 83H has no special meaning in C. It would get parsed as a pp-number during translation phase 4 (6.4.8p1), and then rejected during translation phase 7 due to the fact that it doesn't match the form of any of the five permitted types of tokens. I would guess that what you mean by 83H is equivalent to the C hexadecimal constant 0x83. A key point to remember about C is that decimal constants, octal constants, and hexadecimal constants never have a negative value. -3 is NOT a decimal constant. It is the unary negation operator applied to the decimal constant 3. 0x83 therefore has the decimal value 131. My best guess is that you're talking about an 8-bit signed char which is represented by the same bit pattern that would represent a 0x83 as an unsigned char; such a char object would represent a negative value: -125 if using 2's complement (by far the most common choice), -3 if using sign-magnitude, and -124 if using 1's complement (assuming I did the conversions correctly, which is not guaranteed). C allows for all three possibilities. C also allows CHAR_BIT > 8; CHAR_BIT==16 is not uncommon in C compilers targeting DSPs. > Is the above link not talking the same thing with my understanding? I'm not sure what you're understanding is, but your words make it pretty clear that your understanding is incorrect, and that the above link is referring to something quite different. I'm not sure what your comments "one bit LSB truncation" are intended to mean. There appears to be some resemblance between what you're talking about and division by 2, so I'll use such a division in my example below. There is a "toward zero" rule that applies to integers. It applies specifically to integer division. "When integers are divided, the result of the / operator is the algebraic quotient with any fractional part discarded." (6.5.5p6). Thus, 8/3 is algebraically 2 2/3. Discarding the 2/3 leaves the C value of 8/3 == 2. Algebraically, -125/2 is -62 1/2; discarding the 1/2 leaves the C value of (-125)/2 == -62. In both cases, the result of integer division is closer to 0 than the algebraic quotient would be, which is why footnote 105 says that this rule 'is often called "truncation toward zero"'. Note that the C rule is entirely in terms of the meaning of the value being represented, it is not in terms of the bit pattern being used to represent it. A signed char variable containing a value of -125 will, when divided by 2, give a result of -62, regardless of whether it's represented in 2's complement, 1's complement, or sign-magnitude, and regardless of whether char has 8 bits, 9 bits, 16 bits, or any other permitted size.
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