Skip to main content
68 votes

Why do so many programming languages not have a "built-in" way to do simple math functions?

factorial isn't useful Imperative programming languages don't have factorial built-in because, on its own, it isn't useful. ...
Keiji's user avatar
  • 607
39 votes

Why does MATLAB have left division/solve?

Non-commutativity of multiplication a/b calculates $a \cdot b^{-1}$, whereas b\a calculates $b^{-1} \cdot a$. Since matrix ...
xigoi's user avatar
  • 1,007
38 votes
Accepted

Why do so many programming languages not have a "built-in" way to do simple math functions?

The bottom line is you can't have everything. The "elusive eight" statistical functions referenced in another answer might be absolutely essential to statisticians, but as a web application ...
IMSoP's user avatar
  • 2,428
33 votes

Why do so many programming languages not have a "built-in" way to do simple math functions?

Ultimately it comes down to convention and opinion: languages can't have a builtin for everything, and generally developers prefer to have smaller standard libraries with "conventional" ...
tarzh's user avatar
  • 3,924
25 votes

Why do so many programming languages not have a "built-in" way to do simple math functions?

There are three places where a language designer can choose to place some functionality: language core (e.g. operator) standard library leave it for third-party libraries Putting something into the ...
Ralf Kleberhoff's user avatar
25 votes

What obstacles prevented C and C++ from standardizing π?

There is no deep reason. The answer is social: The C standard does not specify any constant for π. We can only speculate on why, but ultimately, the set of things the C standard does and does not ...
Alexis King's user avatar
  • 8,721
21 votes
Accepted

Is there any particular reason to only include 3 out of the 6 trigonometry functions?

This does just roll the barrel slightly lower on the hill (or whatever the saying is), but: The IEEE 754 floating point standard includes the following trig-related operations as "recommended&...
RubenVerg's user avatar
  • 1,632
19 votes

What syntax could be used to implement both an exponentiation operator and XOR?

Demote xor Unless you're in C or a similarly low-level language, you're not going to be doing a lot of bit-level manipulation. So the bitwise operations really don't need to be using up those juicy ...
Silvio Mayolo's user avatar
16 votes

What syntax could be used to implement both an exponentiation operator and XOR?

I think most of the problem comes from functions being in the form op(a, b), which is verbose and doesn't read well when expressions get complicated. So why not use ...
BoppreH's user avatar
  • 1,441
16 votes

What syntax could be used to implement both an exponentiation operator and XOR?

Double star Many languages have a ** operator for exponentiation. This is reasonably intuitive, as exponentiation is just the hyperoperation after multiplication. <...
CPlus's user avatar
  • 8,321
12 votes

Is there any particular reason to only include 3 out of the 6 trigonometry functions?

One possible reason is that it is quite difficult to implement these functions with high accuracy. For + - * / the IEEE standard requires that the error is at most 1/2 of the least significant bit (0....
gnasher729's user avatar
11 votes

Is there any particular reason to only include 3 out of the 6 trigonometry functions?

Older processors had instructions to compute the sine, cosine and tangent of floating-point numbers, but not the secant, cosecant or cotangent. So the reason could just be that especially for older ...
kaya3's user avatar
  • 19.7k
10 votes

Is there any particular reason to only include 3 out of the 6 trigonometry functions?

A very huge system library with functions for trivial, easy to derive features is more difficult to remember. The code using them may easily be less readable. Imagine you see: ...
Audrius Meškauskas's user avatar
10 votes

Is there any particular reason to only include 3 out of the 6 trigonometry functions?

Frequency of use The main three are vastly more commonly used than the other three.
Adám's user avatar
  • 3,156
10 votes
Accepted

What obstacles prevented C and C++ from standardizing π?

Both languages have an upper bound to their precision of their floating point types (float double ...
kaya3's user avatar
  • 19.7k
10 votes

Why does MATLAB have left division/solve?

Solving linear systems is better than inverting The main reason is the one in xigoi's answer, noncommutativity, but let me add that a separate operator is handy to have for matrix operations: ...
Federico Poloni's user avatar
9 votes

Why do so many programming languages not have a "built-in" way to do simple math functions?

most programming languages cannot do without calling a library (square roots, logarithms, trigonometric functions, factorials, etc.). The bolded part (emphasis in original) is really the key here. ...
kaya3's user avatar
  • 19.7k
9 votes

Why do so many programming languages not have a "built-in" way to do simple math functions?

It's almost entirely down to history of programming languages. For example, the Elusive Eight are generally not included in languages because they were not included in C, and one can argue that this ...
Corbin's user avatar
  • 851
8 votes

What obstacles prevented C and C++ from standardizing π?

I can't find anything in the Rationale that specifically addresses mathematical constants, or the lack thereof, so I can only speculate. Other popular programming languages at the time didn't have a ...
dan04's user avatar
  • 1,959
8 votes

What programming languages contain a built-in imaginary data type?

Wolfram The programming language used by Mathematica not only supports pure imaginary numbers, but offers rigorous treatment of infinite complex values. ...
Karl Knechtel's user avatar
7 votes

What syntax could be used to implement both an exponentiation operator and XOR?

Unicode See for examples: https://en.wikipedia.org/wiki/List_of_logic_symbols https://en.wikipedia.org/wiki/Mathematical_operators_and_symbols_in_Unicode Powers Superscript for powers 23 Downsides: ...
Bruce Adams's user avatar
  • 2,843
7 votes
Accepted

What syntax could be used to implement both an exponentiation operator and XOR?

Lua solves this by using x ~ y for XOR. When applied to floats which do not have an integer representation, it throws an error. This is reasonably intuitive, since ...
Luatic's user avatar
  • 330
7 votes

What programming languages contain a built-in imaginary data type?

Results are below: Languages Snap! and Ruby both pass the original test case, but Ruby does not pass a simpler one suggested by user23013. Maple passes the original test case and user23013's other ...
Adam Hyland's user avatar
5 votes
Accepted

Can you represent a language with a group with a small/simple generator set?

There is a notion studied in computability theory and category theory. A partial combinatory algebra (PCA) is a set $A$ equipped with a binary partial operation $A \times A \rightharpoonup A$, written ...
Trebor's user avatar
  • 166
5 votes
Accepted

Correctness of mixed signed/unsigned arithmetic

So is there a correct way to implement this? Any pitfalls and footguns I should be considering? How do other languages do it? Your proposed struct value is utterly bizarre and no language I know of ...
Moonchild's user avatar
  • 879
5 votes

Correctness of mixed signed/unsigned arithmetic

This code causes the C compiler to emit warnings like: [...] Which makes me worried about the correctness of this implementation. Let's examine this with an example: ...
André L F S Bacci's user avatar
5 votes
Accepted

Why is truncated or non-Euclidean division/modulo the norm?

The answer is largely given at the top of the answer by Jules to the question you linked: In short, div-mod operations come in pairs satisfying the equation ...
IMSoP's user avatar
  • 2,428
4 votes

Why do so many programming languages not have a "built-in" way to do simple math functions?

Disclaimer: @Ralf Kleberhoff's answer provides the key distinction made here. TL;DR: Cost and Convenience. There are 3 possible places for functionality to be defined: Language, ie built-in. Standard ...
Matthieu M.'s user avatar
  • 3,038
4 votes

Why do so many programming languages not have a "built-in" way to do simple math functions?

Processors have several units, the most important for this discussion being the ALU, and its coprocessor, the FPU. The ALU is the part of the processor that does math. It has at most four math ...
phyrfox's user avatar
  • 221
4 votes

What syntax could be used to implement both an exponentiation operator and XOR?

** is fairly common. Speaking personally, I strongly dislike "identifiers that parse like operators" of which the most notorious is Pascal's inline mod. Unicode is best avoided at least in a ...
Mark Morgan Lloyd's user avatar

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