# Tag Info

137

First off: I am a mathematician - a professional one (in that I get paid for doing maths). I am not a programmer. I do do some programming, but very definitely of the Cargo Cult variety (see first comment to https://tex.stackexchange.com/q/451/86 and my response) and nothing of the sort that would normally bring me to this site (indeed, I registered here ...

84

2D / 1D - mapping is pretty simple. Given x and y, and 2D array sizes width (for x-direction) and height (for y-direction), you can calculate the according index i in 1D space (zero-based) by i = x + width*y; and the reverse operation is x = i % width; // % is the "modulo operator", the remainder of i / width; y = i / width; // where "/" is an ...

74

The CPU has built in detection. Most instruction set architectures specify that the CPU will trap to an exception handler for integer divide by zero (I don't think it cares if the dividend is zero). It is possible that the check for a zero divisor happens in parallel in hardware along with the attempt to do the division, however, the detection of the ...

74

Two common cases to consider: Integer arithmetic Obviously if you are using integer arithmetic (which truncates) you will get a different result. Here's a small example in C#: public static void TestIntegerArithmetic() { int newValue = 101; int oldValue = 10; int SOME_CONSTANT = 10; if(newValue / oldValue > SOME_CONSTANT) { ...

69

You need to keep in mind that in FPU arithmetics, 0 doesn't necessarily has to mean exactly zero, but also value too small to be represented using given datatype, e.g. a = -1 / 1000000000000000000.0 a is too small to be represented correctly by float (32 bit), so it is "rounded" to -0. Now, let's say our computation continues: b = 1 / a Because a is ...

65

Suppose we're designing a new language and we want Sqrt to be an instance method. So we look at the double class and begin designing. It obviously has no inputs (other than the instance) and returns a double. We write and test the code. Perfection. But taking the square root of an integer is valid, too, and we don't want to force everyone to convert to ...

64

Assuming that your square might be rotated against whatever coordinates system you have in place, you can't rely on there being any repetition of X and Y values in your four points. What you can do is calculate the distances between each of the four points. If you find the following to be true, you have a square: There are two points, say A and C which ...

60

If you want a "math-like" language, Haskell is your best friend (for your best friend). You can easily make new functions without hassle. It is the best language recommendation I can give you for you friend. Here are some links: Try Haskell - An online Haskell compiler and tutorial. Learn You A Haskell For Great Good! - This is how I learned Haskell. How to ...

59

The usual reason for writing numbers, in code, in other than base 10, is because you're bit-twiddling. To pick an example in C (because if C is good for anything, it's good for bit-twiddling), say some low-level format encodes a 2-bit and a 6-bit number in a byte: xx yyyyyy: main() { unsigned char codevalue = 0x94; // 10 010100 printf("x=%d, y=%d\n"...

58

Although there were older precursors, the influential French mathematician Rene Descartes is usually credited for introducing superscripted exponents (ab) into mathematical writing, in his work Geometrie which was published in 1637. This is the notation still universally used in mathematics today. Fortran is the oldest programming language widely used for ...

50

The conceptually simplest solution would be to create a list where each element occurs as many times as its weight, so fruits = [apple, apple, apple, apple, orange, orange, lemon] Then use whatever functions you have at your disposal to pick a random element from that list (e.g. generate a random index within the proper range). This is of course not very ...

47

According to MDN Math.min accepts only numbers, and if one of the arguments is not a number, it'll return NaN. That's not what it says (bold emphasis mine): If at least one of arguments cannot be converted to a number, the result is NaN. Type Conversion: Math.min uses ToNumber to convert its arguments. ToNumber uses ToPrimitive to convert Objects (and ...

45

The main reason I use different bases is when I care about bits. It's much easier to read int mask=0xFF; byte bottom_byte = value & mask; than int mask=255; byte bottom_byte = value & mask; Or image something more complex int mask=0xFF00FF00; int top_bytes_by_word = value & mask; compared to int mask=4278255360; //can you say magic ...

45

Your argumentation against floating point numbers is very fragile, probably because of naivety. (No offense here, I find your question is actually very interesting, I hope my answer will also be.) A classic argument is that floats provide a greater range, but high precision integers can meet this challenge now. For example: with modern 64-bit ...

45

There isn't any, and it's pretty arbitrary. The only useful distinction is between first class, and all others. Every case that's in the "other" bracket has its own distinct set of rules in each case and lumping them all together just isn't very helpful. "First class" means "You don't have to look up the rules", essentially, and "other" is "You have to ...

35

To really understand how arithmetic works inside a computer you need to have programmed in assembly language. Preferably one with a small word size and without multiplication and division instructions. Something like the 6502. On the 6502, virtually all arithmetic is done in a register called the Accumulator. (A register is a special memory location inside ...

34

It depends on the language, on the compiler, on whether you are using integers or floating point numbers, and so on. For floating point number, most implementations use the IEEE 754 standard, where division by 0 is well defined. 0 / 0 gives a well defined result of NaN (not-a-number), and x / 0 for x ≠ 0 gives either +Infinity or -Infinity, depending on ...

32

The right thing to do in such circumstances is to implement the algorithm, formula or whatever with exactly the same variable names as in the primary real-world source (as far as the programming language allows this), and have a succinct comment above it saying something like "Levenshtein distance computation as described in [Knuth1968]", where the citation ...

30

Here's an algorithm (in C#) that can select random weighted element from any sequence, only iterating through it once: public static T Random<T>(this IEnumerable<T> enumerable, Func<T, int> weightFunc) { int totalWeight = 0; // this stores sum of weights of all elements before current T selected = default(T); // currently selected ...

27

They're not that closely related. For programming, it is important to know about mathematics- especially those branches pertaining to, for example, algorithm performance, but the simple fact is that there is no branch of mathematics that will tell you that Singletons are a horrifically bad idea, for example, or when to favour inheritance over composition, or ...

27

As a Computer Scientist looking to get a Master's degree with focus on "Algorithms, Complexity and Computability Theory and Programming Languages" I would say Discrete Mathematics is very important. Discrete math will help you with the "Algorithms, Complexity and Computability Theory" part of the focus more than programming language. The understanding of ...

26

To simplify things by defining a concrete implementation, I will assume (as other answers do) that we're talking about IEEE 754 64-bit floating point. Each floating point number has three parts: a sign, an exponent, and a mantissa. (Technical details about hidden bits are irrelevant to this discussion). Reciprocation doesn't affect the sign 1 / (2**e * m) ...

26

Mathematical operations are often very performance-sensitive. Therefore, we will want to use static methods that can be fully resolved (and optimizied, or inlined) at compile time. Some languages do not offer any mechanism to specify statically dispatched methods. Furthermore, the object model of many languages has considerable memory overhead that is ...

25

You are looking for an arbitrary precision arithmetic (also called "multiple precision" or "big num") library for the language you are working with. For instance, if you are working with C you can use the GNU Bignum Library -> http://gmplib.org/ If you want to understand how it works you can also write your own big num library and use it. The simplest way ...

25

Because switching to integers doesn't solve anything. The problem with floats isn't that they have inaccuracies, it's that half the people using them don't pay any attention to what's going on. Those same people aren't going to pay proper attention to the units they are using when they use an integer, and a different set of screw ups will happen. Repeat ...

25

I like your question as it potentially covers many ideas. On the whole, I suspect the answer is it depends, probably on the types involved and the possible range of values in your specific case. My initial instinct is to reflect on the style, ie. your new version is less clear to the reader of your code. I imagine I would have to think for a second or two (...

24

In the end, basic arithmetic operations are done in hardware. More specifically, in the CPU (or actually, a subpart thereof) In other words, it's electronic circuits. Set the appropriate bits as input and you will get the appropriate bits as output. It is a combination of basic logic gates. http://en.wikipedia.org/wiki/Adder_%28electronics%29 http://en....

24

The demands you have put actually put Fortran at the top of the list, for problems like this: a) number crunching b) paralellable c) it was and still is the de facto language taught outside of cs studies (to engineers who aren't professional programmers). d) has an incredible(!) industry backing, number-of-industry-grade-compilers-wise, with none of the ...

Only top voted, non community-wiki answers of a minimum length are eligible