Where to find the code that defines standard Functions?

This is a discussion on Where to find the code that defines standard Functions? within the C Programming forums, part of the General Programming Boards category; I am interested in editing the sqrt function from math.h so that it does not allow negative numbers to be ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    13

    Where to find the code that defines standard Functions?

    I am interested in editing the sqrt function from math.h so that it does not allow negative numbers to be passed to it. I am also interested in how these math functions work in C.

    Is there somewhere in the compiler files that has the actual function definition and the code that does all of the computation of the square roots?

    Is this actually written in C or in another language?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    Well you can get the source code for the GNU libc implementation
    http://www.gnu.org/software/libc/

    If you have some $$$ compiler, then getting the source code usually involves more $$$.

    > Is this actually written in C or in another language?
    Mostly C, with minor bits of ASM seems usual.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    13
    THanks

    I am just using Dev C++ because it is easy and free

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,021
    Quote Originally Posted by hammer1234
    I am interested in editing the sqrt function from math.h so that it does not allow negative numbers to be passed to it.
    Don't ever change the standard librairy.

    Also, sqrt() already does not allow negitive numbers to be passed to it. It return 0 if you try, and prints an error to stderr.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    I am interested in editing the sqrt function from math.h so that it does not allow negative numbers to be passed to it.
    If that's all you're trying to do, use this:
    Code:
    #define sqrt(x) (x < 0 ? sqrt(-x) : sqrt(x))
    Then just call sqrt() as usual.

    Also, sqrt() already does not allow negitive numbers to be passed to it. It return 0 if you try, and prints an error to stderr.
    That's what the OP is trying to fix!
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    13
    Quote Originally Posted by King Mir
    Don't ever change the standard librairy.

    Also, sqrt() already does not allow negitive numbers to be passed to it. It return 0 if you try, and prints an error to stderr.
    Using dev c++ sqrt definately doesn't return 0 when negative values are passed. I notived in the gnu lib that i was told to download above that the sqrt function already had checks for negative values.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > Using dev c++ sqrt definately doesn't return 0 when negative values are passed.
    Rewriting standard functions to do what you want will only confuse you and other readers later on.
    If you want your own mysqrt(), then write that as a short function
    Code:
    double mysqrt ( double x ) {
      if ( x < 0.0 ) return 0.0;
      else return sqrt(x);
    }
    Simple, easy. Nobody gets confused and nobody has to faff about with customised libc implementations.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    13
    Thanks God of C. I was really more interested in just seeing how it all worked.

    Thanks for your help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. exchange functions demo code
    By kryptkat in forum C++ Programming
    Replies: 0
    Last Post: 04-05-2009, 02:06 PM
  2. Help would be much appreciated.
    By jitterbug in forum C++ Programming
    Replies: 6
    Last Post: 04-01-2009, 08:02 PM
  3. calling functions & assembly code
    By Micko in forum C++ Programming
    Replies: 1
    Last Post: 02-25-2004, 02:27 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. where can I find source code for a binary tree?
    By binary_man in forum C++ Programming
    Replies: 5
    Last Post: 01-10-2003, 08:53 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21