Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Modulus Operator won't work in CGI

Modulus Operator won't work in CGI
Thread Tools
Vash
Dedicated MacNNer
Join Date: Apr 2002
Location: Boston, MA
Status: Offline
Reply With Quote
Oct 20, 2002, 08:19 PM
 
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.
     
sfederman
Fresh-Faced Recruit
Join Date: Jan 2001
Location: San Francisco, CA
Status: Offline
Reply With Quote
Oct 20, 2002, 11:30 PM
 
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. )
     
Vash  (op)
Dedicated MacNNer
Join Date: Apr 2002
Location: Boston, MA
Status: Offline
Reply With Quote
Oct 21, 2002, 04:46 PM
 
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.
     
sfederman
Fresh-Faced Recruit
Join Date: Jan 2001
Location: San Francisco, CA
Status: Offline
Reply With Quote
Oct 21, 2002, 05:41 PM
 
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?
     
phidauex
Fresh-Faced Recruit
Join Date: Oct 2002
Location: St. Louis, MO
Status: Offline
Reply With Quote
Oct 26, 2002, 09:12 PM
 
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 Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 01:48 AM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,