1%2 - Modulus Question - C++ Forum

Có thể bạn quan tâm

cplusplus.com
  • TUTORIALS
  • REFERENCE
  • ARTICLES
  • FORUM

C++

  • Tutorials
  • Reference
  • Articles
  • Forum

Forum

  • Beginners
  • Windows Programming
  • UNIX/Linux Programming
  • General C++ Programming
  • Lounge
  • Jobs
  • Forum
  • General C++ Programming
  • 1%2 - Modulus Question

1%2 - Modulus Question

tappedout (3) I am getting differing opinions on the answer to 1%2. Some say you should round and the answer is 1 and some say the answer is 0 because modulus only deals with integer values in C++. I used a sample program to test this in the compiler and it seems to be rounding the answer because it says the value is 1. Anyone out there have insight and can help us settle this? . Disch (13742) There is no rounding to be done. % gives you the remainder of integer division. 1%2 is 1 because 1 divided by 2 is 0 remainder 1. % can only be used with ?nonnegative? integer types. Pretty much it works like so: if: A / B = d A % B = r then: (d * B) + r = A melkiy (131) a%b behaves like follows (i consider a and b to be greater than 0) : Compose a into a = k*b + rest, where k - the largets integer that can appear here. The rest is the answer (it will be strictly less than b and greater or equal 0). Last edited on Topic archived. No new replies allowed. Home page | Privacy policy© cplusplus.com, 2000-2025 - All rights reserved - v3.3.3Spotted an error? contact us

Từ khóa » C++ 1 2