This is kind of a semi rant so if you don’t like to read those kind of things feel free to skip this post.

How much math does a programmer need to know to do his job? These days with all the frameworks that exists you don’t need to know how to do a square root, a power function or a quick sort these will likely already be built in. What perplexes me is that someone would come out with a function to flip a sign. So if the number is 5 make it -5 and if it is -5 make it 5. Isn’t this simple math? Multiply by -1 and you are done!!

Take a look at this function that someone created on StackOverflow

Math opposite sign function?

Does such function exist? I created my own but would like to use an official one:

C | |

1 2 3 4 5 6 7 8 9 10 11 12 | private function opposite(number:Number):Number { if (number < 0) { number = Math.abs(number); } else { number = -(number); } return number; } |

private function opposite(number:Number):Number { if (number < 0) { number = Math.abs(number); } else { number = -(number); } return number; }

If we visit the Java forums it doesn’t get any better, take a look at this thread: How do you Invert signed numbers?

Hi there.

A simple question: How can I invert the sign of an integer? ie) convert -12 to +12? No doubt there is a really simple way to do this, but I’m stumped, I cannot find anything and I’ve been working on this program all day.

I’ll be very grateful to anyone who can let me know if Java provides a way to do this, or point me in the right direction if it takes some tweaking of binary numbers using the Math package or something.

Cheers!

Here is one ‘solution’

int x = numberToInvertSign; boolean pos = x > 0; for(int i = 0; i < 2*Math.abs(x); i++){ if(pos){ numberToInvertSign--; } else{ numberToInvertSign++; } }

You have to love this ‘gem’ (of course this is someone messing with the original poster)

Java | |

1 2 3 4 5 6 7 | switch (i) { case 1: return -1; case 2: return -2; case 3: return -3; // ... etc, you get the proper pattern } |

switch (i) { case 1: return -1; case 2: return -2; case 3: return -3; // ... etc, you get the proper pattern }

Looking at the php documentation for the ABS function we find a bunch of comments and replies to create a sign function

Isn’t this just basic math that was taught to us in grammar school? Of course it doesn’t stop here, how many people these day can convert numbers between hexadecimal (base-16), binary (base-2) and decimal (base-10) without using a tool?

Of course the future is that people will rely more and more on frameworks and wizards, this could be bad if you need to fix something in your code. Take a look at jQuery, people just love this because JavaScript is hard and they don’t have to deal with it…the tool shields them from that. Another example is ORM….people hate writing queries and just want to create some classes and then some tool will spit out all the CRUD code for them. Sometimes these tools produce some bad SQL and it takes a effort to figure out why your calls to the DB are suddenly slow

Enough ranting for the day….

What do you think is it inexcusable that a programmer does not know that you can flip a sign by doing the following

return -number

or

return number * -1

## 48 Comments

I think this is a problem. We are not emphasizing fundamentals enough in education. It’s one reason that I think that every programmer/CS person should still start with Pascal or C and dig through how things work. Learn to sort things, work with pointers, etc.

A basic understanding of math is a must. You don’t have to be know how to calculate Pearson’s Correlation Coefficient (unless you’re actually doing statistical work), but a knowledge of simple math is essential. I always thought those questions on various forums were fake, but after meeting some people who pass for programmers, I’m not so sure.

I’m with Steve. I started out programming on the 400 in cobol and RPG (control language and all that fun). I ahted it but you know, it gave me what I needed to do my job really well to this day.

Great post Denis!

Nice article! I agree programmers need to have ‘common math sense’. Like Steve Jones pointed out, it helps if the first programming language is Pascal or C.

I think this goes beyond “basic math”. If a programmer cannot (on his own) figure out how to reverse the sign of a number, what else can this programmer “not do”. That’s not to say that this programmer cannot learn enough to effectively do his job, but what an uphill climb!

Some people tend to be more suited for a programming position than others. Most programming tasks (if broken down in to bite size pieces) usually have a simple and elegant solution for them. An experienced programmer will recognize that solution… an inexperienced programmer will hack their way to a slow-loopy solution.

quick question for everyone that has jumped on the “people should be smarter” bandwagons…

do you know how old-schol calulators work?

what may seem simple to someone that has pencil and paper at hand, put yourself in the shoes of

someone that has to program a calculator and be able to calculate just about anything.

not everyone thinks the same way. we should be thankful that others see things differently,

that is what allows for advancements to take place.

Mathematics is universal, and is the basis for how computers work. If you don’t know basic math, you have no business programming. I’ll be (once again) very unpopular for saying this, but the solution from a hiring perspective is easy: only hire people with a degree in CS, math, or an engineering discipline.

Yes, some people think differently. And those people had better not get anywhere near my data. They can work in marketing, HR, or any number of other positions that don’t require the kind of thought processes that good programmers need.

Great post! I guess I’m not all that surprised to know that we have a few, uh, sub-sub-average coders out there. As much as I’d like to say, “hey, everybody skips a groove now and then”, I expect that some of these people actually don’t get that whole *-1 thing…

If you don’t understand basic algebra, you have no business coding. Maybe you don’t need advanced math, but it certainly helps. If you can’t figure out that to change the sign on a number you have to mulitply by -1, then you need to get into a new profession as the other problems you will encounter in programming are much harder than this one. If you can’t solve the simple stuff, there is no hope at all that you will be any good at doing the complex tasks.

If you don’t thoroughly understand Boolean algebra then you have no business querying a database as you have no basis to judge if your query gives the correct answer to the question.

SQLRockstar: “Thinking differently” in a way that creates advancements is wonderful. But when “thinking differently” is just a euphemism for “mathtard”, then that person shouldn’t be programming. Sorry.

Adam Machanic: I don’t have any degrees at all, let alone in CS, math, or engineering. And I think I’m very skilled in my profession (and I love math and excel at it). I realize that a degree can be a good, easy filter, but it’s not perfect. You would miss out if you excluded me as a candidate just because of an arbitrary rule like that.

In fact, I bet that people can manage to get a degree in one of those disciplines and be a LOT worse at math than I am…

Anyone with a job should know “basic math” period. There’s nothing special about being a programmer which should require math over any other job. The term “programmer” is too wide to make any sweeping generalizations. I might go out on a limb, however, and suggest that a programmer should have a basic understand of computers ;).

Anyways, if you wish to write scientific simulation software, you should probably be VERY good at different types of math. If you wish to write applications to manipulate data in databases, then I think you would be well prepared with a basic competency of math/algebra/basic geometry.

Wow, I’ve seen some bad stuff, but probably never this bad. Not knowing -1 * x changes the sign of x is pretty unforgivable. It’s not like you have to do any bit operations or knowing if your representation of negative numbers uses two’s complement.

And it’s pretty universally practical in terms of usage in a LOT of different areas of computing – how often do you need to negate something in accounting software in SQL or ecommerce shopping carts in PHP or reporting formulas or GUI layout?

Someone who doesn’t know this would not get through the first page of Knuth…

Firstly, Denis, I enjoyed the post very much, Thanks :).

I do, however, find myself disagreeing with Adam that tossing every resume without an engineering, CS, or math degree is a good way to protect ourselves from these types of situations. You will lose a lot of good developers with this method and I have worked on enough legacy code-bases and data systems to know that a degree does not guarantee logical design, implementation, or maintainability. Having read a number of your posts in the past I know you have a tendency to throw around inflammatory comments, but I’m not sure if your trying to honestly spark a debate, provide us with an estimation of your character, or simply relieve a little midday tedium.

While I know this conversation and topic has been discussed to death all over, I invite everyone to join us in the forums to discuss it further. We all have unique (and mostly valid) opinions and experiences, might as well share

@Adam Machanic: You’d be amazed at the number of people I’ve come across *professionally* with CS degrees (and MS and MBAs) who can’t employ simple logic to solve the most basic problems. I don’t know how those people even got into college to start.

Math and water are the real basis of life.

Hi, I’m the one who asked the question.

I was having a bit of a brain failure. That’s the best excuse I can come up with. I do believe I’ve a good understanding of math though -

for whatever it’s worth, I took differential equations last semester and received an A (from a decent university).

Sorry to have made you weep for humanity, or programmers.

posting that comment with the level of restraint shown was impressive. Good form and good luck in the future.

[sarcasm]maybe we should have a BrainBench simple math certification…after all they have a HTML 4.0 one they could add the math one [/sarcasm]

Thanks for commenting, “The one who asked the question” – everyone has a “brain failure” from time to time and often these questions end up on the internets for everyone to see.

While I do think that people in general should know basic math, its’ probably unfair to assume someone doesn’t have this understanding based on one forum post (if that was all it took, I probably would have been banned from ever touching a computer again years ago).

That being said, we all see “repeat offenders” that clearly don’t get it – I don’t mean to put words in Denis’ mouth but I think that these folks were the target of the rant.

I’m glad that you seem not to have taken it personally (you just happened to be a convenient example )

I agree. Although i’m a noob to the programming world, so what do i know?

Btw, i can convert between decimal, binary, and hexidecimal

To be fair, a good number of those “answers” were designed to perform the desired function in the most ridiculous way possible. I know, because I wrote a similar answer long ago that was a multi-threaded solution using pointers and a database to find the opposite value of X.

Such threads become a source of one-up-man-ship where each developer attempts to cram more unnecessary functionality, bloat, and insanity into a solution that could be written in about 10 characters.

That said, ALWAYS google before posting. That way, only Google will know when you have brain farts

-Rick

Nice catch, Denis!

Let’s not forget about set theory and its place in RDBMS & schema design, SQL joins, etc. Granted anyone can throw together a table in design view, but without this knowledge & basic understanding of Boolean logic & math I think you are at a serious disadvantage. Especially if you plan on becoming a DBA or Software Engineer, not to mention a programmer. This was taught in High School (and, of course, college) when I attended. That was quite some time ago. Have the educational requirements for graduation changed that much in 30 years?

Maths == Logical/Reasoning Skills.

Rest is bullshit!!!

I like the first Java implementation haha. It took me a few min to understand it. Just for fun, I have a variation of it, without the loop, but with the same intention as the original

//

numberToInvertSign = numberToInvertSign – 2 * numberToInvertSign

//

Same dumb implementation but cleaner!

Computers/Coding without complex math alghoritms is nothing, is look like hanky-panky.

Computers are linked with math.

Actually most schools in the US *do* emphasis fundamentals and “basic math” in a 3R sense but it’s not simply fundamentals that are required. Rote fundamentals exactly what standardized tests are optimized for and in fact it’s the only thing that standardizes tests can detect. When you only tool is a hammer, every problem looks like a nail and you only product (student who are) nail hammerers! Most competent teacher know this well but they usually micromanaged by unions, politicians, bureaucrats and too often parents who are complete morons.

You have to have interest and context to make any fundamental skill stick and every be usable. A real case of “you can lead a horse to water but not make him drink”. Learning the “3 R’s” does no good without having interest and context because these are essential to building the foundation of “critical thinking”.

The problem is that it’s not possible to simply use a cookbook recipe teaching or learning style to get “critical thinking”. You also can’t really test for it short of throwing somebody into a problem that forces them to use and develop practical skills.

This is what “apprenticeship” used to and are supposed to “systematize” despite not really being systematizable in a normal bureaucratic/technocratic wet-dream sense.

I teach computer science in a state university and happened to give an exam today, asking students to give a javadoc comment for this method:

public Rectangle makeRectangle(int x, int y, int width, int height)

{

int newX = x – width / 2;

int newY = y – height / 2;

Rectangle rect = new Rectangle(newX, newY, width, height); // plain old java.awt.Rectangle

return rect;

}

ONE student out of 30 figured out that the rectangle’s CENTER is (x,y). Everyone else either claimed that (x,y) is the top left corner, or just said something wishy-washy about it being the “starting position” or some such thing. A heap of shame on the secondary education system that produces a herd of innumerates!

I love the switch case thing. Wonder if the OP actually implemented it

BTW, I completely forgot about recent question in ASP.NET forums

http://forums.asp.net/t/1476612.aspx

Programmers must understand logic. I started programming z80 assembly games when I was 13, and I really hated math at that time. Nowadays some kids are able to program when they are 8 or 9 years old.

I agree that most jobs require at least a basic math (sophisticated games requires a lot of math), but I strongly disagree that math is essential to become a good programmer.

Although many people think that math is the only way to teach logic, I believe that it’s not the natural way for learning it. And it’s interesting to see that some 20+ years old people can’t understand it and a 9 years old child can.

Ha ha ha, this post made my day. Thank you.

I never tought that such programmers could exist. I found this post very funny. In my country no abiturient will have a chance to go study programming in university if he won’t pass a state math exam. And at the universtity we had 10 diffrent math courses. But we had a very small amount of programming cources that is a my regions problem. We have a very few skilled developers and a very few of them are willing to teach.

In my opinion math is one of the main requirements in game, 3d or heavy software for buisness development. But if a programmer is willing to develop cheap websites, then he does not need knowledge of math.

I can’t agree with your opinion about ORM. ORM is a great way to save time. When writing buisness logic I found that it takes 3x more time to create it and 3x more logic bugs are more likely will be created using basic queries. But one should generate a perfect sql skill and then use Object Relational Mapping to make a perfect use of it.

Everyone should understand basic math! @Steve Jones suggests students learning C or Pascal, I will take it farther, teach the students Assembly! There both Math and Logic is required. When I was in college, Assembly was dropped from the degree(It was too hard for students and “no one” writes assembly anymore). I convinced the instructors to let me take the course as an advanced self study course. I felt that if I could not write Assembler code, I had no business with a CS degree.

Now having said all that, Math and Logic are two different creatures. I would rather maintain code written by a poet, philosopher or a engineer than a Mathematician. As quick as I see throw-away functions or Lambda’s, I start refactoring.

It doesn’t seem to me that it’s merely a math problem. Certainly not in the case of the StackOverflow poster. He seems to have known the math of how to reverse a sign. What’s interesting is, he didn’t seem to connect that piece of knowledge with his problem. That’s not uncommon. It’s kind of a “not seeing the trees for the forest” thing. People get lost in the complexity of what they’re doing and fail to perceive simple, known, solutions to their problems. A programming problem must require a programming solution. Well, maybe not. In this case, it simply required a math solution. It’s an interesting phenomenon, and not uncommon in history, though I can’t remember an example right now.

The *-1 example is a good one, and I certainly understand the rant. But the other examples? ORM has nothing to do with math, nor does using a JavaScript library like jQuery.

JavaScript is “hard” because it’s a huge headache to deal with all of the browser incompatibilities and bugs. Whenever I used to flip from solving problems in Java to (vanilla) JavaScript it was like a slap in the face — I’d write correct code, and it would fail for obscure and stupid reasons that had nothing to do with my problem-solving ability. Now I’m very glad to paper over those huge flaws with mootools, jquery, etc..

ORM is related — bridging over the generally pointless differences between many implementations of SQL and generally removing the need for a lot of boilerplate code — though I admit that for any non-trivial application, I use tools that take standard SQL (which I can optimize, tweak, etc.) and load the results into objects.

But back on topic — the real problem is rote-learning. Students are “taught” low and even high-level math by memorizing formulas and techniques, with NO clue as to what they’re actually doing. They learn that a problem in form X gets solved with technique Y but the numbers themselves are still meaningless to them. If they make a calculation error and get a result an order of magnitude off… they won’t notice. They also will be totally unable to identify a real-world problem they would solve using their math.

And because they never get an actual acquaintance with numbers in the real world, their mental muscle for problem solving is incredibly weak, and they make horrible programmers. (Likewise with those taught programming via rote-learning…). Faced with an actual problem, they just throw their memorized techniques at it randomly until one seems to work.

Interestingly (particularly for anyone saying you should only hire those with CS or math degrees), I was already majoring in music more than a decade ago when I started writing code to solve actual problems, and it was continually surprising to me that when I did take compsci classes, there were plenty of students with far more CS background than me… who still had no clue how to logically work through a problem. They were pretty useless at debugging. They graduated, though.

I’m not a fan of interviews that include quizzes, mini code assignments or brain teasers, but I tend to think it can include a “talk through how you’d approach this problem” kind of question. There’d be no right answer… it’s just a great way to see what logical steps someone would take to break down/isolate and resolve a problem.

Only basic maths is required for most situations. Far more important is the ability to think clearly, so agreement with the point about philosophers writing good code. Perhaps even more importantly to focus on what it is required to be done, than an amazing technical solution with todays latest hot new technique, applied out of context.

Thinking clearly applies not only in writing the code, but in approaching resolution of problems. Too many programmers leap into debugging with no forethought, when a cursory thought process and code review could have spotted the bug or guided to a more appropriate start point.

So perhaps programmers need to be able to read as well?

If a person doesn’t even know how to do very basic math things like this, which they should’ve learned in school when they were 12 or so, then that person is absolutely not suited to be a programmer. Such a person should stop programming immediately and pursue a non-technical career that doesn’t require them to deal with numbers.

In fact, I find it astonishing that people who don’t even know very basic math have ever become programmers. If you don’t even have the smallest bit of talent in math or technical things, then how and why did you become a programmer?

The youngster “programmer” I heard was 7 years old. A 10 years old girl from Pakistan was the youngest certified Microsoft expert a few years ago. 14/15 years teenagers are compiling their own kernel right now, all around the world.

So, do we need a degree to become a programmer ? Not in my world.

Do we need to know basic math as programmers? I think so, at least for professional programmers.

Math is a business requirement, not a requirement to be a (good) programmer.

We’ll need basic math to get a job. We’ll need advanced math to build sophisticated games, but it’s a business requirement, it won’t make us better programmers.

Some contractors told me: this density is lbs per gallon but in order to do this calculation we need gallons per pound. We have searched the entire internet and no function to convert between the two exists. We have concluded it to be impossible.

We are no loger with that company.

for anybody, who dares to do programming without a basic knowledge of mathematics i have a message: http://piv.pivpiv.dk/

“nother example is ORM….people hate writing queries” – generalization stinks. for example i love writing queries and hate ORM :]

The scary thing is they didn’t explain why they wanted to reverse the sign. Problems don’t exist in isolation. Maybe they only wanted to do it for negative numbers in which case |ABS| would be appropriate. Its not safe to multiply by -1 in this case.

As for this effort from Jason, it will only work for making positive numbers negative. Its not even an anti-ABS because at least ABS makes no change to positive numbers. Credit for thinking about it a little.

//

numberToInvertSign = numberToInvertSign – 2 * numberToInvertSign

//

The problem with the question is not that they are stupid or don’t know maths, its that people are too quick to post questions for which a quick googling on a maths website would provide the answer.

Sorry. I am one of those people that you all are lambasting. I hate math. Have since the day I was born. I failed it in almost every grade I took it. However I have a great creative mind and somehow I seem to figure things out even if it takes me a couple of hours longer to do things than my programmer buddy next to me. I don’t think you have to know how to do math – you just have to have the right resources to show you how to do the math you need to do. Whether that is a textbook, googling it, copying it from code you’ve used before or whatever. As long as you can get the job done, who cares?

I learned programming before math. I managed to out program all my little friends at the time, including the math geeks who were in high math classes and a year ahead. Not only that, but by learning programming I was able to better understand math. For someone who learns math, functions/procedures are more like f(x) but for someone who learned programming first f(x) is like a function.

Also in my professional career I don’t use advanced math. I don’t use more than algebra. I know for engineering/video game development/systems simulation more advanced math is involved. But in a normal business basic algebra is fine. Usually plugging in numbers for financial equations. Even advanced algorithms are not required with most of the libraries pre-written. Basically business logic is mostly a series of decisions. The most advanced math I use is the occasional topological sort because it is useful for figuring out the order to do things given a bunch of dependencies (ie the order to perform a sequence of calculations that a user specifies).

And on logic, if you need a course on that then you have problems. and = traditional definition of and, both items or it is false. or = traditional definition of or (a, b, or both). XOR is a little new but still easy enough to master with a knowledge of and/or. Basically the truth tables for these operations are intuitive. After that, without anything else most SQL queries are easy and intuitive too. Add some set theory such as union and intersection and you are all set. I learned SQL long before I ever had a discrete mathematics course, it was easy. And I wrote some queries that are still used at my first internship even today…and I had that job before my freshman year of college.

Anyway you people need to stop with the holier than thou get off my lawn stuff, it makes me sick. Also on the guy with the degree in music, I had heard that from more than one person that music majors often make great programmers. And to the guy who says everyone needs a degree in science/engineering here is your counterexample. Also there are a lot of non-degree programmers who are absolutely great. I know one guy who dropped out of college due to failing out who could outprogram the entire class. His programs were the stuff of legends, but unfortunately his exam scores not…

And on negating, I would think it is more efficient to do 0 – number. Generally addition/subtraction have a cost of log(32) while multiplication has a cost of log(32)^2 when using 32 bit ints. Now if you are using higher bits it will take more time. If you are using arbitrarily long numbers, this makes a difference….What if they are 10000 bits or something. your Pi computation may slow to a crawl…..

But I think it depends what you want to do. Graphics programmers will be doing all sorts of advanced math, linear algebra, interpolation, etc… so will someone making a nuclear simulation. But the guy at a normal business probably just basic algebra. Even accounting software will mostly be plugging numbers into standard equations. Although it does help to have some idea of the asymtotic complexity of your library. ie is that list you are using o(n) per operation or lg(n)? Then if you run into performance problems you have some ideas for improvement. I did take someone’s o(n^3) algorithm and turn it into O(nlogn) for a big performance improvement. If it wasn’t taking so long I probably wouldn’t have bothered with it….

This is not so much about math as human nature. Most problems have many answers but rarely do we arrive at the simplest solution.

Apply simple refactoring techniques to these code samples and you will arrive at your suggested final solution.

Dear Mr. Dude,

You said:

> As for this effort from Jason, it will only work for making positive numbers negative.

> Its not even an anti-ABS because at least ABS makes no change to positive numbers.

> Credit for thinking about it a little.

>

> > numberToInvertSign = numberToInvertSign – 2 * numberToInvertSign

Are you sure you want to stand by your claim that this only works for positive numbers? Right now you’re providing a shining example of someone who isn’t too strong at that math stuff. Look:

f(x) = x – 2 * x

f(x) = 1x – 2x

f(x) = (1 – 2)x

f(x) = -1x

f(x) = -x

His function works just fine for positive or negative numbers, dude. Er, Dude.

The other day I felt really stupid because I had to look on mathsiffun.com to remember basic trig and triangle solving.

Having read this I don’t feel so bad!

The problem is compounded because Basic Sciences are always ignored in Engineering classes.

I know only basic maths.

I had learning difficulties at school so I am mostly self taught all the way (and quite proud of it).

However I do agree, as a developer, that programming is important, at least how to create formulas, algorithms to workout solutions because doing that stuff is no different to programming (maths is just a purer form).

Which is why I am taking a course at a University to get a decent education in Mathematics to benefit my new development career path.

My kids are smarter than me

And I wanted to chime the poster above called Chris – I hear you my friend.

I have no formal education yet I managed to keep up with many a developers in the IT field but I found I wasn’t chained down and have always been able to think freely outside of the box.

Design has always been my passion, my creative flair and artistic side has really added value to my development roles.

Logic is important, as long as you use common sense you can develop a solution.

There is a distinct divide though… You have “software engineers” on one side, and then modern developers on the other side.

I sit kind of in the middle as I am a tinkerer at heart and mechanically minded.

Personally I am finding software engineers live in the realms of computer science and precise solutions like PLC controllers and custom research and design while the majority of programmers and developers are business solution orientated – focusing on integrating technologies together to create a solution as opposed to actually engineering one.

And yes, I separated programmers and developers, they are two different things in my book.