How Much Math Do You Need to Know to Know to Learn C+

Hither are some posts I've seen on the r/learnprogramming subreddit forum:

  • How much math do you need to get a good developer?
  • Should I brush up on math?
  • This may exist the dumbest question I have ever posted online. How much math does ane really need to be a skillful programmer?

Math and programming accept a somewhat misunderstood relationship. Many people think that you take to exist expert at math or made good grades in math grade earlier you can even begin to learn programming. But how much math does a person need to know in order to program?

Not that much really. This article will go into detail about the kinds of math you should know for programming. You probably know it already.

For full general programming, you lot should know the following:

  • Addition, subtraction, division, and multiplication – And really, the calculator will be doing the calculation, subtracting, dividing, and multiplying for yous anyway. You just have to know when yous need to do these operations.
  • Mod – The modern operation is the "remainder" and its sign is usually the % percentage sign. And then 23 divided by 7 is 3 with a remainder of 2. But 23 mod vii is 2.
  • The even/odd mod test trick – If you lot want to know if a number is odd or even, mod it by two. If the result is 0, the number is even. If the event is ane, the number is odd. 23 mod 2 is 1, so you know 23 is odd. 24 mod 2 is 0, so y'all know 24 is fifty-fifty. If x modern 2 is 0, you lot know that whatever number is stored in the variable x is fifty-fifty.
  • To become a pct of a number, multiply that number by the percent number with the decimal point in front of it. So to go 54% of 279, multiple 0.54 * 279. This is why ane.0 often means 100% and 0.0 ways 0%.
  • Know what negative numbers are. A negative number times a negative number is a positive. A negative times a positive is negative. That'southward well-nigh information technology.
  • Know what a Cartesian coordinate arrangement is. In programming, the (0, 0) origin is the top left corner of the screen or window, and the Y axis increases going down.
  • Know the Pythagorean theorem, and that information technology can be used to find the distance between two points on a Cartesian coordinate organization. The Pythagorean theorem is a^2 + b^2 = c^2. What this usually means in programming is the distance between coordinate (x1, y1) and (x2, y2) will just exist sqrt( (x1 – x2)^2 + (y1 – y2)^2 ).
  • Know what decimal, binary, and hexadecimal numbering systems are. Decimal numbers are the numbers we're used to that have ten digits: 0 to 9. Information technology's normally thought that humans develop this system considering we have ten fingers and counted on our fingers.

Computers work with binary data, which is a number system with merely two digits: 0 and 1. This is considering we build computers out of electronics components where information technology's cheaper to make them only recognize two unlike states (one country to correspond 0 and the other to represent 1).

The numbers are still the verbal same, but they are written out differently because there are a different number of digits in each system. Because hex has 6 more than digits than the 0-nine numerals can provide, we utilise the messages A through F for the digits to a higher place ix. The easiest fashion to show these number systems is with an odometer. The following three odometers always show the aforementioned number, only they are written out differently in different number systems:


See the Odometer Number Systems page in a new window.

You don't even take to know the math of converting a number from one number system to some other. Every programming language has functions that can do this for y'all.

(On a side note, hexadecimal is used because one hexadecimal digit can stand for exactly iv binary digits. And so since 3 in hex represents 0011 in binary and A in hex represents 1010. This has the prissy effect that the hex number 3A (which is 58 in decimal) is written in binary equally 00111010. Hex is used in programming because information technology is a shorthand for binary. Nobody likes writing out all those ones and zeros.)

And that's well-nigh information technology. Other than the number system stuff, you probably already knew all the math you needed to know to do programming. Despite the popular formulation, math isn't really used that much in programming. You lot would demand to know math in order to write programs that practise, say, earthquake simulators. But that'due south more nearly needing to know math for earthquakes rather than needing to know math for programming an earthquake simulator.

Advanced Mathematics in Some Areas of Programming

At that place's a few areas of programming where some additional math knowledge might be needed (merely for 95% of the software y'all'll write, you don't need to know information technology.)

3D games and 3D graphics – 3D stuff will usually involve knowing trigonometry and linear algebra (that is, math dealing with matrices). Of course, there are many 3D graphics libraries that implement all this math programming for y'all, and then you lot don't need to know the math.

2D physics (like Angry Birds) and 3D physics (like many popular 3D games use) – To do programming that involves physics, you'll need to larn some physics equations and formulas (specifically mechanics, which is the blazon of physics with springs, gravity, and balls rolling downward inclined planes.) Withal, there are several physics engines and software libraries that implement this stuff for you, so you really don't need to know the physics equations to brand a game similar Angry Birds.

Cryptography – And actually, by cryptography, I just mean RSA. In which case, yous'd have to acquire some math almost how prime numbers piece of work and doing the Greatest Common Divisor (which is a expressionless simple algorithm, although plenty of programming languages accept gcd() role that does this for you.) Other encryption ciphers are by and large moving data around in specific steps. For case, this Flash animation shows the steps in the AES "Rijndael" cipher. All the steps are basically substituting numbers for other numbers, shifting rows of numbers over, mixing upwardly columns of numbers, and doing basic improver with numbers.

And that'southward just if y'all want to write your own encryption ciphers (which you shouldn't practice, because there are already plenty of good ones and without expertise your zip will probably suck and be hands cracked.) If you merely desire to write a program that encrypts data, there are software libraries that implement encryption and decryption functions already.

And then even for the above situations, yous don't need to know the math to brand programs with 3D graphics, physics, or encryption. Simply learn to utilise the libraries.

What You lot Do Need to Learn to Do Programming

What you do need to learn is how to model data and devise algorithms. This basically means, how to have some real-world adding or some data processing, and write out lawmaking that makes the computer do it. For case, in the game Dungeons and Dragons the characters and monsters have several dissimilar statistics for combat:

  • HP, or hitting points, is the amount of damage a person can accept before dying. More HP means you can take more damage before dying.
  • Air conditioning, or armor form, is a mensurate of the chance your armor has of blocking an attack. The lower the AC, the more protective the armor is.
  • THAC0 (pronounced "thay-co"), or "To Hit Armor Class 0", is a measure of how skillful the person is at making a successful hitting on an opponent. The lower the THAC0, the more accurate the person's attack is.
  • The damage of the weapon is written out as something like 1d6+2. This means the harm is the amount from rolling 1 vi-sided die, and and so adding ii to it. A damage stat of 2d4 would be rolling 2 four-sided dice and calculation them together. (Dungeons and Dragons uses 4, 6, 8, 10, 12, and 20-sided dice.)

To encounter if an attacker hits a defender, the attacker rolls a twenty-sided die. If this number is equal to or greater than the attacker'due south THAC0 minus the defender's AC, then the hit is successful and the defender takes damage. Otherwise, the defender has either dodged or blocked the attack and takes no damage.

Let's take two Dungeon and Dragons characters, Alice and Bob, with the following stats:

  • Alice: HP 14, Air-conditioning 5, THAC0 eighteen, DAMAGE 1d6
  • Bob: HP 12, AC 7, THAC0 16, DAMAGE 2d4

So Alice has ii more than hit points than Bob and improve armor (recollect, lower Air-conditioning is meliorate). But Bob is more than probable to brand a successful hitting (remember, lower THAC0 is amend) and does more than damage. We can tell Bob'south damage is better because 2d4 will consequence in 2 to viii points of damage, while Alice's 1d6 volition result in ane to vi points of damage. (If you knew statistics math, you could summate that Bob's expected value of damage is 5, which is larger than Alice's expected value of damage is 3.v.)

So would you lot bet on Alice or Bob to win in a fight? It's hard to tell, they seem pretty evenly matched. Even if y'all knew a lot of statistics, doing all these calculations would be a pain. But you lot don't demand to know statistics in club to write a program that simulates Dungeons and Dragons gainsay (that is, models this process) and so run several hundred or m simulated fights and encounter who wins on average.

Here'southward such a program written in Python: (Download source)

            import            random            ,            copy            NUM_FIGHTS            =            ane            VERBOSE            =            True            # Lower thac0 and lower air-conditioning values are better. Higher harm & hp values are better.            aliceTemplate            =            {            'name'            :            'Alice'            ,            'hp'            :            14            ,            'ac'            :            5            ,            'thac0'            :            18            ,            'dmgnum'            :            1            ,            'dmgsize'            :            6            ,            'dmgmod'            :            0            }            bobTemplate            =            {            'name'            :            'Bob'            ,            'hp'            :            12            ,            'ac'            :            7            ,            'thac0'            :            xvi            ,            'dmgnum'            :            ii            ,            'dmgsize'            :            4            ,            'dmgmod'            :            0            }            def            display            (            s            ):            if            VERBOSE            :            print            (            s            )            def            attack            (            attacker            ,            defender            ):            if            random            .            randint            (            1            ,            20            )            >=            assailant            [            'thac0'            ]            -            defender            [            'ac'            ]:            damage            =            0            for            i            in            range            (            attacker            [            'dmgnum'            ]):            damage            +=            random            .            randint            (            1            ,            attacker            [            'dmgsize'            ])            impairment            +=            assailant            [            'dmgmod'            ]            display            (            '            %southward                          (            %southward                          hp) hits                        %s                          (            %southward                          hp) for                        %s                          points of damage.                        %s                          is reduced to                        %s                          hp.'            %            (            attacker            [            'name'            ],            attacker            [            'hp'            ],            defender            [            'name'            ],            defender            [            'hp'            ],            damage            ,            defender            [            'name'            ],            defender            [            'hp'            ]            -            damage            ))            defender            [            'hp'            ]            -=            harm            else            :            display            (            '            %southward                          misses                        %s            .'            %            (            attacker            [            'proper noun'            ],            defender            [            'proper noun'            ]))            aliceWins            =            0            bobWins            =            0            for            i            in            range            (            NUM_FIGHTS            ):            display            (            '======================'            )            display            (            'Start of combat #            %south            '            %            (            i            +            1            ))            alice            =            re-create            .            deepcopy            (            aliceTemplate            )            bob            =            copy            .            deepcopy            (            bobTemplate            )            while            True            :            set on            (            alice            ,            bob            )            if            bob            [            'hp'            ]            <=            0            :            pause            assault            (            bob            ,            alice            )            if            alice            [            'hp'            ]            <=            0            :            break            if            alice            [            'hp'            ]            <=            0            :            display            (            'Alice has died.'            )            bobWins            +=            ane            if            bob            [            'hp'            ]            <=            0            :            brandish            (            'Bob has died.'            )            aliceWins            +=            1            print            ()            print            (            'Alice won                        %south                          (            %due south%%            ) fights. Bob won                        %south                          (            %s%%            ) fights.'            %            (            aliceWins            ,            round            (            aliceWins            /            NUM_FIGHTS            *            100            ,            2            ),            bobWins            ,            round            (            bobWins            /            NUM_FIGHTS            *            100            ,            2            )))          

When you run this program, information technology produces output like this:

======================  Beginning of gainsay #1  Alice misses Bob.  Bob (12 hp) hits Alice (14 hp) for 6 points of damage. Alice is reduced to eight hp.  Alice misses Bob.  Bob misses Alice.  Alice misses Bob.  Bob misses Alice.  Alice misses Bob.  Bob misses Alice.  Alice (8 hp) hits Bob (12 hp) for 5 points of damage. Bob is reduced to vii hp.  Bob misses Alice.  Alice misses Bob.  Bob misses Alice.  Alice misses Bob.  Bob (7 hp) hits Alice (8 hp) for 2 points of damage. Alice is reduced to half-dozen hp.  Alice (six hp) hits Bob (7 hp) for 6 points of damage. Bob is reduced to 1 hp.  Bob misses Alice.  Alice (6 hp) hits Bob (1 hp) for 1 points of damage. Bob is reduced to 0 hp.  Bob has died.    Alice won ane (100.0%) fights. Bob won 0 (0.0%) fights.

But maybe Alice just got lucky in this one fight. Let'southward reprogram this plan to turn off the verbose output (displaying text on the screen takes a lot more time than running the simulation) and upwardly the number of fights to 30,000 (this is just changing the NUM_FIGHTS variable to 30000 and the VERBOSE variable to Fake):

Alice won 12909 (43.03%) fights. Bob won 17091 (56.97%) fights.

So nosotros tin run across that with the given stats, Bob is at a slight advantage. The calculator merely ran thirty,000 faux fights. If we were to play thirty,000 fights of Dungeons and Dragons with pencil, paper, and physical dice, it would accept months to calculate this. But my laptop had the results in less than eight seconds.

But what if we increased Alice'southward hit points from 14 to 20. Who would win so?

Alice won 19438 (64.79%) fights. Bob won 10562 (35.21%) fights.

We see that those 6 extra hit points turns the tables and gives Alice the reward. How well-nigh if her hit points were only increased to 16 instead of 20?

Alice won 15176 (fifty.59%) fights. Bob won 14824 (49.41%) fights.

We encounter that just tweaking the stats past 2 hitting points is just plenty to even out the advantages that Bob gets from his higher level of damage.

And when you wait at this program, the only math information technology uses is addition, subtraction, and multiplication and division to find a percentage. Fifty-fifty if we fabricated the simulation more sophisticated to business relationship for the effects of magic spells, healing potions, multiple attackers, and switching to dissimilar weapons in mid-combat, nosotros wouldn't need to know more than math or have made skillful math grades to do the programming for information technology.

Certain, go ahead and learn more math. Information technology tin only help y'all get a better developer. But how much math do you need to know to program? Very little, really.

UPDATE: I guess I'd add bones algebra to the required knowledge, only merely insofar as that if 10 * iii = 12 knowing why X is 4.

(Hither'southward a listing of other discussions on Reddit about this topic.)

  • Math in programming - I'm concerned that I should give up trying (89 comments)
  • Is there hope for having a career in programming if I'm terrible at math? (50 comments)
  • What areas of math are the nigh important to information science/programming? (26 comments)
  • Why is Discrete Math so important? (xx comments)
  • How much math exercise you need to become a good programmer? (20 comments)
  • Is it possible to be "almost ok" at math and still be a adept desirable programmer? (13 comments)
  • Should I brush up on math? (ten comments)
  • This may be the dumbest question I have ever posted online. How much math does one actually need to be a good programmer? (xi comments)
  • I'thou trying to larn c++ but why do tutorials always have math bug? (7 comments)
  • Worth while improving math skills? (8 comments)
  • What math courses should I take if I really want to delve into 3D? (15 comments)
  • What is a amend supplement to a BS in Informatics? Math or Physics? (11 comments)
  • The importance of Math for Programming? (half-dozen comments)

davisandis1945.blogspot.com

Source: https://inventwithpython.com/blog/2012/03/18/how-much-math-do-i-need-to-know-to-program-not-that-much-actually/

0 Response to "How Much Math Do You Need to Know to Know to Learn C+"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel