0

Particularly i have a large unsigned number and a large signed negative number.

Why can't I divide a large number by a negative number C++

When I try to divide the two, I get the result zero simply because something somewhere is converting my signed number to unsigned (meaning that it has to be positive). >.>;

Edit.

Just to be absolutely clear. I know that it is resolved to zero and I'm happy with that. I'm also happy with the previous reason that it is resolved to zero because a number is converted away from being a signed number to an unsigned number.

However that doesn't answer why the number is converted from a signed number to an unsigned number in the first place. There doesn't seem to be any logical reason for doing so.

  • Isn't this the same question? Did your other question not receive a good enough answer? – Blue Ice Feb 16 '14 at 05:15
  • 1
    It's not the same question. My previous question was about why a number was coming out at zero. It does not and none of the answers answer the question of why they are converted, simply that they are converted. – user3315127 Feb 16 '14 at 05:16
  • 2
    Fine: They are converted because the standard demands it. – user1520427 Feb 16 '14 at 05:18
  • 1
    It's converted because it's being used with an `unsigned` version of the same type in a division. It's simply a part of usual arithmetic conversions: *Otherwise, if the operand that has unsigned integer type has rank greater than or equal to the rank of the type of the other operand, the operand with signed integer type shall be converted to the type of the operand with unsigned integer type.* – chris Feb 16 '14 at 05:18
  • @chris aka manual says so. Doesn't give a reason for it, but that's the way it is. Hmmmm. Thanks – user3315127 Feb 16 '14 at 05:22
  • @user3315127 It should make sense that to perform arithmetic, the operands must be (exactly) the same type. Since your input operands are of different types, you have to convert one or the other. If you converted the ULL to a LL, you risk losing values in `[2^63,2^64)`. If you convert the LL, you risk messing up the sign. It's unfortunate, but no architecture I've seen has machine operations to divide a ULL by an LL directly. – crockeea Feb 16 '14 at 05:26

1 Answers1

2

The operands of the arithmetic operators need to have the same types. That makes the language easier to define and easier to implement, in part because there are fewer cases to handle, and in part because hardware doesn't generally support mixed-type operations, either. Formally, C++ is defined in terms of a virtual machine, but the capabilities expected of that machine are influenced by those of real-world hardware. Operations dictated by the standard but not supported by hardware (such as your hypothetical signed-unsigned division) would need to be implemented by compiler-writers in library code, and that would affect performance.

Either the signed operand needs to be converted to unsigned, or vice versa. The standard committee picked the former.

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467