I am working on a platform, which doesn't have the math library, but I need to use the logf function (natural log with floating point input). I tried to search the code for logf but in vain. Can somebody provide or give a link for logf function code.
If you don't have math library, you can go to libc to look for the code ->
http://sourceware.org/git/?p=glibc.git;a=blob;f=math/w_logf.c
And see how logf calls __ieee754_logf ->
http://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/ieee754/flt-32/e_logf.c
I hope it helps you.
I've seen a few references to logf() functions which just use casting around log(), such as:
float logf(float _X)
{
return ((float)log((double)_X));
}
http://www.raspberryginger.com/jbailey/minix/html/lib_2math_2log_8c-source.html is an implementation of log() (though I have no personal experience with the function there, I merely found it while googling).
Related
I understand fun and fix in ML as :
fix calculates least fix point or simply can allow use of same name inside the call and fun allows to explicitly control scope of the variable also fun is used for non-recursive versions.
I might be wrong. Can anyone specify the difference between fun and fix in ML.
That paper introduces a mini-language, called ML₀, which presents two constructs, lam and fix. The first is for non-recursive function expressions, while the second is a fixpoint combinator that allows defining recursive functions.
fun is used later on as part of a different language that's meant as a surface language for use by the programmer. fun should be desugared to lam and fix calls, as shown in Figure 5 of the paper.
I'm very new to coding in C, and I'm working on calculating the CDF for a chi-square distribution given a value and the degrees of freedom. Is there a function or a library that does this or will I need to write this code myself?
Any help would be appreciated
If I am right you can use the GNU Scientific Library. Look for the function gsl_ran_chisq, which returns a random value from a Chi-square distribution provided a number of degrees of freedom.
You can check the C functions related to chi-square and many other distributions on this link. Simply install the library. Link to the lib's home page can also be found here.
I hope I have helped.
Answers in C, Python, C++ or Javascript would be very much appreciated.
I've read a few books, done all the examples. Now I'd like to write a simple program.
But, I already ran into the following roadblock:
My intention is to take an equation from the user and save it in a variable,
For example:
-3*X+4 or pow(2,(sin(cos(x))/5)) > [In valid C Math syntax]
And then calculate the given expression for a certain X-Value.
Something like this:
printf("%g", UserFunction(3.2)) // Input 3.2 for X in User's Function and Print Result
Any ideas? For the life of me, I can't figure this out. Adding to my frustration, the solution is likely a very simply one. Thank you in advance.
There isn't a simple way to do this in C but I think muParser may be useful to you, it is written in C++ but has C binding. ExprTk is also an option but looks like it is C++ only, on the plus side it looks much easier to get interesting results with.
Another option may be the Expression Evaluation which is part of Libav. It is in C and the eval.h header has some good descriptions of the interface.
In compiled languages like C, C++, or Java there is no easy way to do this--you basically have to rewrite a whole compiler (or use an external library with an interpreter). This is only trivial in "scripting" languages like Python and Javascript, which have a function (often called "eval()") that evaluates expressions at runtime. This function is often dangerous, because it can also do things like call functions with side effects.
Ffmpeg/libav has a nice simple function evaluator you could use.
I recently looked into Lua and it seems really nice. The only annoying thing is its lack of (standard) libraries. But with the JIT compiler comes along a nice FFI C interface.
Coming from a java background, i tried to avoid C as much as possible, so my question: has anyone some experience with LuaJIT, especially its FFI interface, and how difficult is it to set up a library for someone with little to no knowledge in C?
Seemed really simple to me, and Mike Pall has some nice tutorials on it here, the lua mailing list also includes some good examples, so check out the archives as well
how difficult is it to set up a library for someone with little to no
knowledge in C?
Really easy. First, you need to declare the functions that you'd like to use. Then, load the target library and assign it to a Lua variable. Use that variable to call the foreign functions.
Here's an example on using function powf from C's math library.
local ffi = require("ffi")
-- Whatever you need to use, have to be declared first
ffi.cdef([[
double powf(double x, double y);
]])
-- Name of library to load, i.e: -lm (math)
local math = ffi.load("m")
-- Call powf
local n, m = 2.5, 3.5
print(math.powf(n, m))
I read that the pow(double, double) function is defined in "math.h" but I can't find its declaration.
Does anybody know where this function declared? And where is it implemented in C?
Reference:
http://publications.gbdirect.co.uk/c_book/chapter9/maths_functions.html
Quite often, an include file such as <math.h> will include other header files that actually declare the functions you would expect to see in <math.h>. The idea is that the program gets what it expects when it includes <math.h>, even if the actual function definitions are in some other header file.
Finding the implementation of a standard library function such as pow() is quite another matter. You will have to dig up the source code to your C standard runtime library and find the implementation in there.
Where it's defined depends on your environment. The code is inside a compiled C standard library somewhere.
Its "definition" is in the source code for your c standard library distribution. One such distribution is eglibc. This is browsable online, or in a source distribution:
w_pow.c
math_private.h
Short answer: In the C standard library source code.
The actual implementation of pow may vary from compiler to compiler. Generally, math.h (or a vendor-specific file included by math.h) provides the prototype for pow (i.e., its declaration), but the implementation is buried in some library file such as libm.a. Depending on your compiler, the actual source code for pow or any other library function may not be available.
declared: in the include directory of your system/SDK (e.g.: /usr/include;/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS3.2.sdk/usr/include/architecture/arm/math.h)
defined (implemented):
as library (compiled, binary code): in the library directory of your system/SDK (e.g.: /usr/lib (in case of the math library it's libm.dylib)
as source (program code): this is the interesting part. I work on a Mac OS X 10.6.x right now. The sources for the functions declared in math.h (e.g.: extern double pow ( double, double ); ) are not shipped with the installation (at least I couldn't find it). You are likely to find those sources in your system/SDK's C library. In my case the math library (libm) is a separate project, some of its sources are provided by Apple: http://www.opensource.apple.com/tarballs/Libm/Libm-315.tar.gz
The extern keyword in the function declaration of pow means, that it's defined somewhere else. Math functions are low-level high-performance implementations mostly done in assembly code (*.s). The assembly routines (taking the arguments/giving the parameters via registers/stack) are linked with the rest of the C library. The linking/exporting of the function/routine names is platform specific and doesn't really matter if ones goal is not dive into assembly coding.
I hope this helped,
Raphael
If you are seeking how the calculation is implemented, you can find it here:
http://fossies.org/dox/gcc-4.5.3/e__pow_8c_source.html
The name of the function is __ieee754_pow
which is called by pow function.
I’s really defined in math.h. Have you tried including math.h and simply using pow? What do you mean by “can't find it”?
Here's a C implementation for fdlibm: http://www.netlib.org/fdlibm/e_pow.c
For what it's worth, when v8 dropped its cos/sine tables, it pulled from fdlibm's implementation to do so: https://code.google.com/p/v8/source/detail?r=22918
From the change commit comments: "Implement trigonometric functions using a fdlibm port."
Mozilla on the other hand calls the cstdlib math functions, which will have variable performance by build and system (ex: may or may not invoke the chip-level implementations of transcendental functions). While C# bytecode seems to make explicit references to chip-level functions when it can. However, "pow" is not one of those, iirc (doesn't seem to have an chip-level function) and is implemented elsewhere.
See also: https://bugzilla.mozilla.org/show_bug.cgi?id=967709
For a cos/sine discussion in the Mozilla community, comparison of Mozilla's implementation vs old v8 implementation.
See also: How is Math.Pow() implemented in .NET Framework?
Intrinsic functions are chip-level, actually implemented on the processor. (We don't necessarily need lookup tables any more.)
Its here and also here.
Also go on wikipedia
You will find pow there.