Jump to content

chars and percent - C#

lubblig
Go to solution Solved by 79wjd,

Ehm, ok... ;)

 

So, 97%96 means that there is a reminder of 1?

And 97%122 means that there is an overflow and returns 97?

 

Is that right?

 

If yes, then why does 97%122 return 97? Why not something easier to understand (or something...)?

Because modulo arithmetic is very useful the way it is (and it's like that because of the way it works). 

It returning 97 is actually a good thing. 

 

Say I wanted to iterate through an array infinitely with entries being overwritten when I've exceeded the size of the array. If 97%122 returned anything other than 97, then I would have to computer 97 from whatever the return value was if I wanted to write whatever data I had to element 97. 

When I do:

double val = 0;val = 'z' % 'a';Console.Write(val.ToString());

The console returns 25

 

But when I do:

double val = 0;val = 'a' % 'z';Console.Write(val.ToString());

It returns 97.

 

 

So what I've concluded so far is that 'a' is 97 in the ascii table and 'z' is 122.

 

In the first case it did (I guess?) 122-97 that equals 25.

But in the second case where I did 97-122 it returns 97 but I don't know why.

 

I'm thinking it might be some weird thing that it cannot equal a negative number (since 97-122 would be -25) and therefore returns the first value.

 

 

So the real question I guess is, have I understood this correctly and why does it not return negatives? I basically want an explanation to what's happening because I cannot fully understand it.

 

Thanks!

Spoiler

System:

i5 3570k @ 4.4 GHz, MSI Z77A-G43, Dominator Platinum 1600MHz 16GB (2x8GB), EVGA GTX 980ti 6GB, CM HAF XM, Samsung 850 Pro 256GB + Some WD Red HDD, Corsair RM850 80+ Gold, Asus Xonar Essence STX, Windows 10 Pro 64bit

PCPP:

http://pcpartpicker.com/p/znZqcf

 

Link to comment
Share on other sites

Link to post
Share on other sites

% is modulo, which is used in order to determine overflow/remainders. 

 

I'm terrible at explaining this, so I'll just leave an example: 

97%122 = 97

97%97=0

97%96=1

97%98 = 97

 

https://en.wikipedia.org/wiki/Modular_arithmetic

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
Share on other sites

Link to post
Share on other sites

% is modulo, which is used in order to determine overflow/remainders. 

 

I'm terrible at explaining this, so I'll just leave an example: 

97%122 = 97

97%97=0

97%96=1

97%98 = 97

Ehm, ok... ;)

 

So, 97%96 means that there is a reminder of 1?

And 97%122 means that there is an overflow and returns 97?

 

Is that right?

 

If yes, then why does 97%122 return 97? Why not something easier to understand (or something...)?

Spoiler

System:

i5 3570k @ 4.4 GHz, MSI Z77A-G43, Dominator Platinum 1600MHz 16GB (2x8GB), EVGA GTX 980ti 6GB, CM HAF XM, Samsung 850 Pro 256GB + Some WD Red HDD, Corsair RM850 80+ Gold, Asus Xonar Essence STX, Windows 10 Pro 64bit

PCPP:

http://pcpartpicker.com/p/znZqcf

 

Link to comment
Share on other sites

Link to post
Share on other sites

% in computer science is modulo most of the time.

 

97%122 returns 97 because 97 = 122 * 0 + 97. (the reminder theorem : If a/b = c and a remainder , r , then a = b*c + r)

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

Ehm, ok... ;)

 

So, 97%96 means that there is a reminder of 1?

And 97%122 means that there is an overflow and returns 97?

 

Is that right?

 

If yes, then why does 97%122 return 97? Why not something easier to understand (or something...)?

Because modulo arithmetic is very useful the way it is (and it's like that because of the way it works). 

It returning 97 is actually a good thing. 

 

Say I wanted to iterate through an array infinitely with entries being overwritten when I've exceeded the size of the array. If 97%122 returned anything other than 97, then I would have to computer 97 from whatever the return value was if I wanted to write whatever data I had to element 97. 

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
Share on other sites

Link to post
Share on other sites

Because modulo arithmetic is very useful the way it is (and it's like that because of the way it works). 

It returning 97 is actually a good thing. 

 

Say I wanted to iterate through an array infinitely with entries being overwritten when I've exceeded the size of the array. If 97%122 returned anything other than 97, then I would have to computer 97 from whatever the return value was if I wanted to write whatever data I had to element 97. 

Oh ok. I'm not 100 percent sure what a modulo is and G translate doesn't help me getting it right in my own language either.

 

But I think I sort of understand how it works now. I guess I have to read about what exactly modulo's are to fully understand it.

 

Thanks for the help! :)

Spoiler

System:

i5 3570k @ 4.4 GHz, MSI Z77A-G43, Dominator Platinum 1600MHz 16GB (2x8GB), EVGA GTX 980ti 6GB, CM HAF XM, Samsung 850 Pro 256GB + Some WD Red HDD, Corsair RM850 80+ Gold, Asus Xonar Essence STX, Windows 10 Pro 64bit

PCPP:

http://pcpartpicker.com/p/znZqcf

 

Link to comment
Share on other sites

Link to post
Share on other sites

Note, if you want to use percents , just use rational numbers (float).

Eg.

50% = 0.5

25.784% = 0.25784

187,9% = 1.879

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

Depends on the language, % could also mean remainder.

Isn't it the same thing?

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

They're different for negative numbers.

In C, -4 % 5 = -4

In Python -4 % 5 = 1

Ah...

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

Note, if you want to use percents , just use rational numbers (float).

Eg.

50% = 0.5

25.784% = 0.25784

187,9% = 1.879

Yeah, I know. This was like an extra part of an assignment that I had in a programming course in school. We were basically only supposed to test it and see what happens. But since I didn't understand the result I thought I'd ask here. But thanks anyway.

Spoiler

System:

i5 3570k @ 4.4 GHz, MSI Z77A-G43, Dominator Platinum 1600MHz 16GB (2x8GB), EVGA GTX 980ti 6GB, CM HAF XM, Samsung 850 Pro 256GB + Some WD Red HDD, Corsair RM850 80+ Gold, Asus Xonar Essence STX, Windows 10 Pro 64bit

PCPP:

http://pcpartpicker.com/p/znZqcf

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×