What bignum libraries work with D? - d

I'm in need of a bignum library for representing large integers. What options do I have with the D programming language? Are there, for instance, GMP bindings?
Update:
I'm trying to use the inbuilt BigInt as described below but it appears it's not available with the GDC.
import std.bigint;
import std.stdio;
void main()
{
BigInt n = "123";
writefln(n);
}
When I try to compile this code with gdc main.d I'm told it can't find bigint.d. Does gdc only implement some of the library or am I doing something wrong?

If what you're looking for is a big integer type, then there's BigInt in the standard library. On the other hand, if you're specifically looking to use GMP, then all you have to do is have extern(C) declarations for the appropriate types and functions in GMP that you need, and you can call them directly from D. Check out out this page for more details on how to use C code in D.

Paul Anderson is working on a BigFloat abstraction for the standard library.

Related

Complex Digamma Function

Do you know a c++ library where the Digamma Function (http://en.wikipedia.org/wiki/Digamma_function) is implemented with complex arguments?
I did not find a library, but google found this for me:
GammaFunctions.c++
However I did not check the validity of this code.
Try one of these:
GSL - GNU Scientific Library contains gsl_sf_complex_psi_e.
http://www.ensta-paristech.fr/~lunevill/doc_melina++_v039/src/special_functions/GammaFunctions.c++.html
http://www.mathworks.com/matlabcentral/fileexchange/978-special-functions-math-library/content/psi.m - You'll need to convert this to C++.
http://www.netlib.no/netlib/slatec/fnlib/cpsi.f - You'll need to convert this to C++.

CoffeeScript-like language for C/C++

I've been looking for a language that provides the same functionality that Coffeescript has, but for C/C++. I mean a language that converts the code into C, but readable, just like CoffeeScript converts to Javascript, readable and all.
I think this is possible, and even desirable (I grudgingly deal with C++ when writing Node.js native modules), but more challenging than with a higher-level language like JavaScript.
What you're asking for is a language that would provide syntactic sugar without sacrificing performance or flexibility. Some syntactic sugars (say, syntactic whitespace or Ruby-style def/end blocks instead of curly braces) would be trivial to add. But adding anything more advanced, you'd run into two major hurdles: static typing, and garbage collection.
For instance, let's say that you wanted to add implicit returns. It seems like a small feature, but think about it: In order for the feature to be useful, you'd have to—at the very least—throw a compile-time error when the value of the last expression doesn't match the function's return type. That means that your compiler needs to inspect a line like
a->b
and figure out what type it is. That's possible in principle, but it's a heck of a lot more work than the CoffeeScript compiler does.
Or say you added list comprehensions. That means you're allocating an array whose length isn't known at compile-time, which means you'll need to later deallocate it yourself. So the syntactic sugar could actually hurt you. The rule "If you malloc it, you free it" doesn't work if the compiler is adding in the malloc for you, unless it can figure out where to put the free (which, again, is generally possible but would take a lot of work).
So, while I'd love to someone give C++ the CoffeeScript treatment, I don't expect it to happen any time soon—if ever. I think it's more likely that the world will eventually move on to something like D or Go for system-level programming.
I think OOC is probably the closest thing to Coffeescript for C. It's a programming language with a lot of the features you'd expect from dynamic languages (objects, first class functions, clean syntax) that compiles directly into C99.
http://ooc-lang.org/
One item missing from Jacindas list you might want to know about: Vala/Genie is a compiler targetting C with the GObject library implementing objects, written by Gnome. Vala is a C#-like syntax, and Genie a Python-like syntax, but for the rest they are the same system. It was actually created because bare C + GObject became too much of a pain to work with for the Gnome guys. Vala does objects and automatic memory management based on reference counting or ownership tracking, and a lot of other things you'd expect in a C# like language.
As for the CoffeeScript-like property, I just saw that there was an experimental feature to disable the dependency of the generated code on GObject, so it generates just plain C without any runtime dependencies. Doing so disables a number of more advanced OO features, but it still leaves you with a better syntax, a basic object system, and (semi-)automatic memory management.
I don't know how readable the output is, but if you run it through a pretty printer it might be very close to what you're looking for.
SugarCpp is a language which can compile to C++11. It should be what you are looking for. Visit https://github.com/curimit/SugarCpp for more details.
For Python specifically, take a look at this question:
Convert Python program to C/C++ code?
They mention Shed Skin, which will take a subset of pure python and convert to standalone C++ code.
Cython is typically used to create Python extension modules, but can create standalone programs if the Python interpreter is embedded. This doesn't sound like what you're looking for, though.
Cython is based on Pyrex, and they are compatible with each other in many ways.
For some of the other languages you mentioned there seem to be similar projects: Ruby and PHP. Toba for Java (though no longer maintained), Marst for Algol, BCX for BASIC, COB2C, PtoC for Pascal and I should probably stop there before this turns into "List of Converters from Foo to C/C++."
Hope that helps!
Take a look at this fresh new project: https://bixense.com/coffeepp/
Coffee++
Coffee++ is a little language that compiles into C++. It has been created to have something similar to CoffeeScript for C++. Currently Coffee++ is in a alpha state and not at all usable or final. Check out the source on Github to get involved.
The golden rule of Coffee++ is: "It's just C++". The code compiles one-to-one into the equivalent C++, and there is no runtime library. You can use any existing C++ library seamlessly from Coffee++ (and vice-versa).
Overview:
source file Test.cf++
include iostream
int main():
age := 5
dog := Dog(age)
if age != 7:
dog.bark()
class Dog:
public Dog(int age):
this->age := age
public void bark():
std::cout << "Woof!\n"
private int age
};
compiled Test.hpp
#pragma once
int main();
class Dog {
public:
Dog();
void bark();
private:
int age;
};
compiled Test.cpp
#include "test.hpp"
#include <iostream>
int main() {
auto age = 5;
auto dog = Dog(age);
if (age != 7) {
dog.bark();
}
}
Dog::Dog(int age) : age(age) {
}
void Dog::bark() {
std::cout << "Woof!\n";
}
Since vala and genie were already mentioned, I'll put BaCon (Basic Converter) out there for those who reminisce about hand coding programs from a monthly print publication, but want to use it with a modern GUI.
Must run on each Unix/Linux/BSD platform, including MacOSX
Converted sourcecode must be compilable with GCC
Must resemble genuine BASIC with implicit variable delarations
Spoken language constructs are preferred
The website http://www.basic-converter.org/ has lots of examples (some of theme pretty complex for "BASIC") and plugins for nearly every opensource IDE or you can use the BACON IDE.
Well, this is not what you want, but.. : http://www.campbell.nu/oscar/cython/index.html - This cython/cytoc is a significant space (pythonish) transpiler for C/C++ that I coded around 1999/2000, it has no relation to the cython project that arrived seven years later.
Frankly, I wrote it in Perl and it's heuristical, using regular expressions. I used it for an entire project of a Gameboy Color game (regular ansi C). But I wouldn't trust it... Which is why I'm looking around too, instead of using that dusty old bugger ;)
Follow up:
I've been working on Onyx (https://github.com/ozra/onyx-lang) for a year plus now, and finally realized the obvious thing to do is rewrite it to compile to C++ instead of LLVM-IR. The re-target idea is brand fresh, so rewrite is still vapor. But your input would be made well use of in RFC's, if you like the idea of the language, it's your chance to shape it.

Quadruple Precision in C++ (GCC)

Just recently, the GCC 4.6.0 came out along with libquadmath. Unfortunately, GNU has supported Fortran, but not C or C++ (all that is included is a .so). I have not found a way to use these new features in C++, however, GNU C does support the __float128 type for guaranteed quadruple-precision floats. GNU C does not seem to support the math functions in libquadmath, such fabsq (absolute value, q being the suffix for quad).
Is there any way to get these functions working in C++, or is there some alternative library that I could use for math functions with __float128? What is the best method for getting quadruple-precision floats working in the GCC? Right now, I can add, subtract, and multiply them, but this is useless to me, considering how I have no way to convert them to strings or use functions such as truncq and fabsq to create my own string function.
Apparently, this seems to have been an installation error on my part.
While the core C/C++ portion of the GCC includes libquadmath.so, the Fortran version supplies libquadmath.a and quadmath.h, which can be included to access the functions.
#include <quadmath.h>
#include <iostream>
int main()
{
char* y = new char[1000];
quadmath_snprintf(y, 1000, "%Qf", 1.0q);
std::cout << y << std::endl;
return 0;
}
nm the .so file, and see what function names really are. IIRC, fortran routines have an _ at end of name. In C++, you'll need to extern "C" {} prototypes. If this is a fortran interface, then all args are passed by reference, so proto might be something like
extern "C" { long double fabsq_(long double* x); }

How do I call a C++ static library from Perl?

I'm writing a C++ static library that needs to be shared among several applications, one of them written in Perl. Unfortunately, I only barely know the core Perl language (I read the Llama book), and I'm not all that familiar with its libraries. How do you make calls to an external C++ binary from a Perl script?
By Google search, I found information on the Perl Inline module, but if I understand it correctly, that isn't what I need. I'm pretty sure that's for writing C and C++ code directly in your Perl scripts, not for calling external C++ libraries.
The C++ static library is being cross-compiled for an ARM processor, so there will be no C++ compiler on the target machine where the Perl script will be running. (If that makes a difference.)
You can call code from other libraries via Inline::C (and likely the same via Inline::CPP) - have a look at Inline::C::Cookbook. Most likely you want to start out with Inline and after you're done experimenting use the resulting .XS file to work further.
You want to look at using XS, which is how Perl normally interfaces with C/C++ libraries. It's not quite trivial. A couple of relevant portions of the Perl documentation:
perlxs
perlxstut
First, it does need to be in a dynamic library, not a static library (unless you'll be re-compiling perl itself and linking it against your static library).
Second, since C++ will mangle the names (one of the most annoying "Features" of C++ if you ask me) you'll need an extern "C" block that contains hook functions. If you were using C++ you could probably get by with a single hook function that returns the C++ object that implements the interface you need to use. Since you're using perl, you may need to wrap an object in an interface like this:
CPPObject object;
extern "C"
{
int InitObject( void )
{
return object.init();
}
int DoCoolStuff( void )
{
return object.DoCoolStuff();
}
int DoOtherCoolStuff( int foo )
{
return object.DoOtherCoolStuff( foo );
}
int DestroyObject( void )
{
return object.Destroy();
}
}
You need to create a wrapper function that is callable from perl, and AFAIK, you'll need to have this wrapper function be in a dynamic library (unless you're going to rebuild the perl binary and link the static lib to it). I like to use a tool called SWIG (Simple Wrapper Interface Generator) to create the wrappers for me. It can create wrappers for 17 or so other languages too.
Probably not what you're thinking, but how about writing a stand-alone C++ program that the perl program communicates through pipes with?
I'm only starting to wrap my head around XS, so I can't offer much help. But here's what I do know...
There is XSpp, which is XS for C++. It is distributed with WxPerl. WxPerl is under active and responsive development.
Inline:CPP can be used to write your initial interface/wrapper code. Then you can analyze the generated XS. However, it doesn't look so well maintianed. If it works, it may provide you with a good head start.
You might find this short note on XS and C++ by John Keiser helpful, if a bit dated.

python executing existent (&big) c++ code

I have a program in C++ that uses the cryptopp library to decrypt/encrypt messages.
It offers two interface methods encrypt & decrypt that receive a string and operate on it through cryptopp methods.
Is there some way to use both methods in Python without manually wrapping all the cryptopp & files included?
Example:
import cppEncryptDecrypt
string foo="testing"
result = encrypt(foo)
print "Encrypted string:",result
If you can make a DLL from that C++ code, exposing those two methods (ideally as "extern C", that makes all interfacing tasks so much simpler), ctypes can be the answer, not requiring any third party tool or extension. Otherwise, it's your choice between cython, good old SWIG, SIP, Boost, ... -- many, many such 3rd party tools will let your Python code call those two C++ entry points without any need for wrapping anything else but them.
As Alex suggested you can make a dll, export the function you want to access from python and use ctypes(http://docs.python.org/library/ctypes.html) module to access e.g.
>>> libc = cdll.LoadLibrary("libc.so.6")
>>> printf = libc.printf
>>> printf("Hello, %s\n", "World!")
Hello, World
or there is alternate simpler approach, which many people do not consider but is equally useful in many cases i.e. directly call the program from command line. You said you have already working program, so I assume it does both encrypt/decrypt from commandline? if yes why don't you just call the program from os.system, or subprocess module, instead of delving into code and changing it and maintaining it.
I would say go the second way unless it can't fulfill your requirements.