|
|
Modulus Operator won't work in CGI
|
|
|
|
Dedicated MacNNer
Join Date: Apr 2002
Location: Boston, MA
Status:
Offline
|
|
I have a CGI script that uses the modulus operator (which gets the remainder of a division problem if I remember correctly) to divide to different numbers. But every time I execute the script it will not work correctly. For example: I put in $how_many_deleted=94%16; This returns a value of 14!? Where did this number come from? Am I wrong about what modulus is supposed to do, or is my sciprt just acting screwy.
|
Revenge is a meal best served cold.
|
|
|
|
|
|
|
|
Fresh-Faced Recruit
Join Date: Jan 2001
Location: San Francisco, CA
Status:
Offline
|
|
Vash,
Actually, you are correct that the modulus operator (%) returns the remainder of a division. Also, your program is returning the correct answer... 14.
94/16 = 5 with a remainder of 14. So, 94%16 = 14. What answer were you expecting?
-Scot
(
Last edited by sfederman; Oct 20, 2002 at 11:46 PM.
)
|
|
|
|
|
|
|
|
|
Dedicated MacNNer
Join Date: Apr 2002
Location: Boston, MA
Status:
Offline
|
|
94/16 gives me 5.875, I do not know how you got 14. I can not see any rounding involved to get 14 either. So the modulous should give 875 should it not?
|
Revenge is a meal best served cold.
|
|
|
|
|
|
|
|
Fresh-Faced Recruit
Join Date: Jan 2001
Location: San Francisco, CA
Status:
Offline
|
|
Ah... Now I see the problem. You're confusing the fractional portion of the quotient with the remainder.
The quotient of a division is the result, so 94/16 = 5.875. However, the remainder of a division is not .875, but instead, it is the amount left over from the division. So, the remainder of 94/16 is 14. 16 goes into 94 5 times, for a value of 80. The remainder, or the amount left over is 94-80, or 14. This is what the modulus operator returns. If you take this remainder and divide it by 16, you get .875, which is the fractional component of the quotient.
Got it?
|
|
|
|
|
|
|
|
|
Fresh-Faced Recruit
Join Date: Oct 2002
Location: St. Louis, MO
Status:
Offline
|
|
sfederman is right. Modulus is an integer operation, which returns the remainder of a the first integer divided by the second. Remember when you did long division in elementary school, and you always ended up with a remainder on the side, and they said "We'll teach you more about that in the next grade." Thats the modulus.
Who knew I was learning perl in 3rd grade?
Also, just to make sure we are all on the same page, its a good idea to specify the language you are talking about, CGI isn't a programming language, its just an interface. CGI apps can be written in perl, C, python, C++, and more. However, the modulus operator is pretty consistent, so its not a big deal this time, but a more complex script would probably need us to know what language you are talking about.
peace,
sam
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Forum Rules
|
|
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
|
|
|
|
|
|