Thread: Why there is no STL for math in C++?

  1. #1
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246

    Question Why there is no STL for math in C++?

    Why there is no STL for math in C++? And we should use <cmath>.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    I'm not sure I understand...

    And we do use <cmath>
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    Useless Apprentice ryan_germain's Avatar
    Join Date
    Jun 2004
    Posts
    76
    Quote Originally Posted by siavoshkc
    Why there is no STL for math in C++? And we should use <cmath>.
    do you mean you want pre-built templates in the stl to do some math?
    There is not the slightest indication that [nuclear energy] will ever be obtainable. It would mean that the atom would have to be shattered at will.

    -Albert Einstein, 1932

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The reason the STL does not include math functions is probably that it wasn't viewed as particularly important. Support of mathematical functions was a bit of an after-thought in C, and this way of thinking carried across to C++ (although changes to the type system improved some things, such as not requiring float to be promoted to double in a lot of circumstances where C does that promotion).

    IMHO, this has always been a weakness of both C and C++, and one of the reasons why I still use Fortran (which is a language designed for such things) for numerical work.

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Ah, I got the question now. I was reading "And we should use <cmath>" as a suggestion, not as an affirmative. Sorry about that.

    Math is not an objective of the programming language. C was not meant to be an answer to math problems. It is a general-purpose programming language. The standard library paradigm is what provides the "specialization" to solve specific problems (and of course the fact programmers can "extend" the language with their own libraries). This is not a weakness per-se of the language. It's just the way it was implemented. There's no keywords to the programming language past those provided strictly to guarantee the general-purpose theme of the language. In other words, it is left to the programmers to define the needed tools for their specific problems.

    In fact the C standard library started out as a non-standard mesh of libraries defined by the C community at large to answer a few more common problems; IO, time, math, type definitions, string manipulation,...

    C++ built on C, as you know, mainly to provide OOP and generic programming (to name the most influential). And yet the same generalistic purpose was kept. Libraries provide complex math. And there is really not much that can't be done in C++ by either using an already existing library (from the powerful like SciMath or MathLab to the more mainstream like Boost math libraries) or by coding our own with the existing C++ language keywords.
    Last edited by Mario F.; 07-31-2006 at 07:23 AM.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #6
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Take a look at this beautiful list:
    Code:
    <algorithm><bitset><cassert><cctype><cerrno> <cfloat><ciso646>
    <climits> <clocale><cmath><complex> <csetjmp><csignal><cstdarg>
     <cstddef><cstdio><cstdlib><cstring><ctime><cwchar><cwctype>
     <deque><exception><fstream><functional> <hash_map><hash_set> 
    <iomanip><ios><iosfwd><iostream><iso646.h><istream><iterator>
     <limits><list><locale><map><memory><new><numeric><ostream>
     <queue><set><sstream><stack><stdexcept><streambuf>
     <string><strstream><utility><valarray><vector>
    Let me ask my question this way. Why we should use C headers by a wrapper, instead of some headers with same (or even better) functionality written in C++ language?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  7. #7
    Registered User Osaou's Avatar
    Join Date
    Nov 2004
    Location
    Stockholm, Sweden
    Posts
    69
    siavoshkc> Why we should use C headers by a wrapper, instead of some headers with same (or even better) functionality written in C++ language?

    I honestly don't understand you're asking here.


    MD> I asked Stroustrup about writing an operating system in std::c++, and he told me that there were some out there, but he also later basically admitted that c++ is not necessarity the best language for that job.

    Ofcourse it isn't, since the std:: libraries are created to simplify low-level stuff... you need low level access for low level stuff (obviously), such as writing an OS.
    What does this have to do with anything anyway?

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by siavoshkc
    Let me ask my question this way. Why we should use C headers by a wrapper, instead of some headers with same (or even better) functionality written in C++ language?
    No, you're asking it the wrong way. The right way is: Why should the C++ standard require the implementors to reimplement something that works perfectly fine the way it is, i.e. the C way?
    C++ provides std::complex, something that is missing from C (or was until C99). It provides std::valarray, which is a data structure optimized for heavy number crunching, something that is also missing from C. But all those simple mathematic functions that have nothing object-oriented about them, it simply took them from C, because they were available. What's wrong with that?

    Most other C headers taken into C++ are there just because someone might want to migrate C code to C++ and have the C functions in the std namespace.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by MDofRockyView
    I asked Stroustrup about writing an operating system in std::c++, and he told me that there were some out there, but he also later basically admitted that c++ is not necessarity the best language for that job.
    I'd really like to see that admission. I don't think I've ever read Stroustrup admit that C++ wasn't the best tool for anything.

    Quote Originally Posted by Osaou
    Ofcourse it isn't, since the std:: libraries are created to simplify low-level stuff... you need low level access for low level stuff (obviously), such as writing an OS.
    Not really. Obviously iostream libraries are pretty much out, but there's no reason you couldn't use C++ (including std headers) to write an OS. Although you'd probably have to implement the C run time first....

    as for STL math, there are the types CornedBee mentioned, and if you'd read what MDofRockyView wrote earlier, you'd know that boost has several math classes including an entire math sub library and the boost graph library.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  10. #10
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Boost seems to be a good package. Its libraries are trustable, aren't they?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  11. #11
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Absolutely. Some of them have even been admited for the next standard, hopefully soon to come.
    Last edited by Mario F.; 08-01-2006 at 07:05 AM.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Formatting Using STL
    By ChadJohnson in forum C++ Programming
    Replies: 4
    Last Post: 11-18-2004, 05:52 PM
  2. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  3. STL or no STL
    By codec in forum C++ Programming
    Replies: 7
    Last Post: 04-12-2004, 02:36 PM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM