12/2/2023 0 Comments Integers negative plus negativeWhen you want to get the difference between numbers and make sure that the modular arithmetic will not be applied, then you should consider using abs() function defined in stdlib.h: int c = five - seven // c = -2īe very careful, especially while writing conditions, because: if (abs(five - seven) < seven) // = if (2 < 7) When you want to make sure that the result is signed, then stored it into signed variable or cast it to signed. Now look at the following operations storing the result as an unsigned int: unsigned int five = 5, seven = 7 If the result type is unsigned, then modular arithmetic takes place. The same principle is applied while working with unsigned types. To understand this modular arithmetic, just have a look at these clocks:ĩ + 4 = 1 ( 13 mod 12), so to the other direction it is: 1 - 4 = 9 ( -3 mod 12). When you work with unsigned types, modular arithmetic (also known as "wrap around" behavior) is taking place. Which or what interpretation is right? Is it defined at all? I suppose one could take that quote to mean that when the right operand is larger the operation is adjusted to be meaningful in the context of modulo truncated numbers.Ġx0000 - 0x0001 = 0x 1 0000 - 0x0001 = 0xFFFFĪs opposed to using the implementation dependent signed semantics:Ġx0000 - 0x0001 = (unsigned)(0 + -1) = (0xFFFF but also 0xFFFE or 0x8001) Value that can be represented by the resulting type. Type is reduced modulo the number that is one greater than the largest Result that cannot be represented by the resulting unsigned integer This is the only vaguely relevant quote from the C standard I could find.Ī computation involving unsigned operands can never overflow, because a So that code like this would be incorrect even if it happens to work on most architectures. You need to ignore the plus sign and recognize that the second negative means you are subtracting that number.I have come across code from someone who appears to believe there is a problem subtracting an unsigned integer from another integer of the same type when the result would be negative. This reads “negative three plus negative 2”. When you are adding a negative number to a negative number, it becomes subtraction, where you start from a negative point on the numbers line and move left.įor example, -3 + (-2). Rule 4: Adding negative numbers to negative numbers- treat the problem like subtraction (counting backwards). How does that look on the numbers line?Īnd then you add the negative number, which means you are moving to the left – in the negative direction. When you are adding a negative number to a positive number you are effectively subtracting the second number from the first.įor example, take 4 + (-2). Rule 3: Adding negative numbers to positive numbers-count backwards, as if you were subtracting. You’re starting with the negative number -6.Īnd you’re adding three to that number, which means you are moving three spots to the right.Ĭlass="green-text">The answer is -3. The best way to think about this problem is to use a number line that extends to the negative numbers. This would reading “negative six plus three”. Pay close attention to where the negative signs are placed in the problem.įor example: -6 + 3. Rule 2: Adding positive numbers to negative numbers-count forward the amount you’re adding. You can calculate these problems the way you always have: 3 + 2 = 5. Rule 1: Adding positive numbers to positive numbers-it’s just normal addition.įor example: this is what you have learned all along. However, there are some simple rules to follow and we introduce them here. When we add a negative number to a positive number, or two negative numbers, that can sometimes seem tricky. Adding positive numbers, such as 2 + 2, is easy.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |