Very basic maths | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Very basic maths

It may seem like a silly question but with Modulus does the number round up and down or just up? If the returned integer value is 4.56, it'll be 5 right? If it's 4.35 is it 4? Is it the opposite with division or the same?

26th Apr 2017, 11:25 AM
Nick H
5 Answers
+ 7
In c++; mod (%) is only for integers. Use fmod(x, y); for floats/doubles. You will need to include math.h
26th Apr 2017, 3:17 PM
Rrestoring faith
Rrestoring faith - avatar
+ 6
Modulus doesn't round up neither down... Modulus return the reminder of the integer divsion, wich is necessarly an integer: 5 / 2 = 2*2 + 1 so, 5 modulus 2 = 1.. because in 5 go 2 times 2 which is 4 so 5 modulus 2 = 5 - 4 ^^
26th Apr 2017, 11:34 AM
visph
visph - avatar
+ 4
@Nick H wrote << In the example from the course 25%7 = 4 why is that, if the answer to divide it would be a decimal? Is there no division in Modulus? >> Modulus doesn't directly make division... From your example, 25 modulus 7 is calculated by asking "how much times can 7 be in 25 and substract it to 25"... So, 25/7 = 3.something: the integer division of 25/7 = 3, modulus = rest of integer division = 25 - ( 7*3 ) = 25 - 21 = 4 ^^
26th Apr 2017, 4:13 PM
visph
visph - avatar
0
yea like visph says it doesnt round because modulus is only used on whole number integers. cant be used on decimal numbers even if you tried. compiler wont let you
26th Apr 2017, 12:04 PM
Edward
0
Ok, that makes a little bit more sense. I'm trying to make sense of how exactly Modulus works. In the example from the course 25%7 = 4 why is that, if the answer to divide it would be a decimal? Is there no division in Modulus?
26th Apr 2017, 12:41 PM
Nick H