last day (37 days later) » 

19:29
12
Q: Why do so many programming languages not have a "built-in" way to do simple math functions?

Nike DattaniNote 1: My question is not about the factorial function. It's about "simple math functions" that high-school level pocket calculators can do, but most programming languages cannot do without calling a library (square roots, logarithms, trigonometric functions, factorials, etc.). Note 2: Comments...

The question is blurring the line between what is provided at a language level and what is provided in a language's standard library. This is a question about what should be in a language's standard library. A function like factorial is fundamentally different from a language construct like if-then-else, pattern-matching, objects, types, or similar which can't be implemented in a library (in most languages). So: this asks why factorial and other math functions are not in standard libraries.
One line is what do the processors have. Sin and cos are often provided as native instructions because calculating them is common enough that is in certain tasks it provides a notable speedup. If the processor has it, but the language doesn't then you can't use the instruction easily, whereas if the language has it, and the processor doesn't, thats easy to handle.
"such functions are not used much (that might be true, but they're common enough to be on most high-school calculators since at least the 1990s)." General-purpose programming languages and calculators serve different purposes. Most programmers don't need factorial, spline, and gamma very often (if ever), so they don't show up outside of languages like MATLAB. Most languages do, however, have math libraries implementing these functions. There's no need to bake that stuff into the language
Most languages do have them -- though they often just provide gamma rather than factorial (since gamma is real extension of factorial). Generally these will be gathered together into a math library, which maybe enabled/disabled with a build-time flag.
It's not going to be the same answer for all the functions you miss, or all the languages you miss them in.
19:29
@user Your mentioning of spline and gamma indicate a fundamental misunderstanding of the question: The title uses the term "simple math", and the body of the question talks about factorials of integers and square roots, which are on a lot of everyday calculators. I don't think anyone needs to be told that most programmers don't use the spline function. To address the rest of your comment, you seem to be trying to provide an "answer" in the comment, which is that most programmers don't need factorials or sqrt very often, but that's also true of exponents, which most languages do have.
@ChrisDodd For exponentiation in Python, you can just do something like 5**3, but for factorial you would need to import numpy or math. I disagree that most languages do provide a built-in gamma function "rather than factorial": Very few languages provide a "built-in" gamma function, and those that do will probably also have a factorial function (even if it's just a synonym for the gamma function when the argument is a positive integer). By "built-in" function, I'm talking about something that doesn't require enabling a library. A comment in V.1 of the question mentioned libraries.
@NikeDattani: Most langauges can't do anything much without including at least one library. You need the I/O library to do I/O and you need the math library to do basic math stuff. In python, you just need the math library to get gamma
@ChrisDodd but most languages can multiply and exponentiate without importing a library, so my question is why we can't at least do the basic functions that are implemented in high-school level pocket calculators!
There are a few languages (forth, java) that don't have gamma in the math library that has things like sin/cos/exp/log/pow, but they're the exception rather than the rule.
Or maybe your questions is really "why don't langauges provide an overload for postfix-! meaning factorial"?
Regarding "not putting a flag in the ground", perhaps it's less so about how to write a factorial than about how to do things you can do with factorials? Without sufficiently insightful optimization, familiar factorial-based expressions for certain combinatorial calculations can lead to poor performance, so if you don't want to put your binomial coefficients in the core library you might want to keep factorials out too.
@NikeDattani In python, factorial can be imported from the math module, which is part of the standard library. It's not an external library, it's built-in with the language. If you write from math import factorial; print(factorial) in python it will literally tell you that it's built-in: <built-in function factorial>
19:29
@ChrisDodd My question is about the basic math operations that appear on typical calculators that are used by high-school students. Factorial is just one example. I also don't need the syntax to be ! since factorial() would be good enough, just like a lot of languages have sin() and cos(x) as "built-in" functions that are part of the language syntax.
@Stef A few comments earlier, I mentioned the ability to get factorial from the math library, but the question is why it's necessary to even run that import command, because you do not need to do that for exponentiation when you do 5**3 and you do not need to do that for multiplication when you do 5*3. I agree that factorials and trig functions are less "elementary" than +,-,x,÷, and I agree that a lot of programs don't need them, but it seems we'd be losing next to nothing by allowing cos to work without importing math. Fortran has cos but not factorial, why?
@NikeDattani Python has both math.cos and cmath.cos in its standard library; if there was a cos already in the main namespace then it'd be confusing to have a second one. And then in external libraries there are other cos functions, most importantly numpy.cos and sympy.cos. We'd lose on all that by having a cos already in the main namespace. Instead, you can choose which one you want depending on your needs.
@NikeDattani Also, * and ** are operators, and python can only have a very limited number of those. There was a ton of discussion when it was suggested to add an operator for matrix multiplication to python; and matrix multiplication is much more ubiquitous than factorial.
@NikeDattani Also UnrelatedString made a very good argument above that if binomial coefficients are not in the main namespace, then factorial shouldn't be either, because factorial(n) / (factorial(n-k) * factorial(k)) is a terrible way to calculate choose(n, k).
My opinion, I'm not sure if there's any Prior Art reflecting this, is that any function / action that is not either natural to the computer, or a feature of the language, should be relegated to a library. Standard operators will almost always be included. Often, I/O operations such as print or input. However, an action like sin which requires computation and may be implemented in language (or out of language), should be relegated to a library such as math.sin. These still may be built in libraries, but libraries nevertheless.
I can probably count on my two hands how often I needed my program to calculate a factorial in my programming career. Calculators have this function because their main job is to calculate, the same cannot be said for Java or C++.
@xLeitix what about exponents?
Factorial example is used in introductory courses precisely because there is no factorial function in the standard library. This gives a student an opportunity to implement something that is not there. If it were in the standard library, the instructors would have to use a different function to demonstrate recursion. That could be for instance Fibonacci numbers.
19:29
Which model of calculator are you looking at that has a dedicated button for factorial? Neither the TI-30X IIS nor TI 84 Plus, two of the most popular in the US, have a dedicated button, both have the ability to calculate factorial but it's buried in a menu.
@stannius I have a ti84+ and I assure you it has a factorial button.
There are languages that have these functions "built in" Those languages run on calculators. Most languages "built in" functions are built into the hardware.
@NikeDattani exponents are more frequent than factorials, and in my experience they are normally provided using a pow() function or similar, but even that I couldn't tell you the last time I I've needed it in my fifteen years of programming.
@PCLuddite I've needed exponents more frequently, but almost all of them could be written as a bit shift operation. 🤓
@blackjack true, but I suppose something could be said about readability since exponents are usually more intuitive than bit shifts.
19:29
@PCLuddite Yes, I usually write them as exponents for that reason unless I know the compiler isn't smart enough to compile it to a bit shift.
@NikeDattani Multiplication and (simple) exponentiation are used relatively frequently almost independent of the type of programming work being done. Floor, ceiling, and truncate functions come up enough they could stand to be part of the global namespace for more languages. But beyond that, a lot of stuff gets rather domain specific. Trig functions, for example, are useful for the things you would use trig for IRL, but a vast majority of programming that doesn’t involve modeling or simulations never needs to do that stuff. Same for most other stuff usually found in math libraries.
Python does have math.factorial, but it was added in 2008, fairly late in the development of the language.
19:49
@user46971 I guess you're missing the point, because the fundamental nature of the question is asking why these functions are not included without calling a library.

  last day (37 days later) »