Click or drag to resize

FP12

The FP12 data type is used for two-dimensional floating point arrays.

If you want to do high-performance numerical computation in Excel, you will need to use the FP12 data type. It is a two dimensional array of double precision floating point numbers defined by Microsoft as follows.

C++
typedef struct _FP12
{
    int rows;
    int columns;
    double array[1];        /* Actually, array[rows][columns] */
} FP12;
The array of floating point numbers is not copied when supplied as an argument to a function, it is a pointer to memory managed by Excel. Both VB and C# add-ins copy the data out of Excel, and copy the data back in to Excel if used as a return value.

xll::FP12 class

This class provides a wrapper around the vanilla struct modeled after std::vector from the C++ standard library.

C++
xll::FP a;
assert (a.is_empty());
assert (a.size() == 0);

xll::FP b(2, 3);
assert (b.rows() == 2 && b.columns() == 3);
b[3] = 4; // 1-d index
assert (b(1, 0) == 4); // 2-d index

b.reshape(3,2);
assert (b.rows() == 3 && b.columns() == 2);
assert (b[3] == 4);
assert (b(1, 1) == 4);

std::sort(b.begin(), b.end()); // STL friendly

Of course Excel knows nothing about the C++ class so when you get an array passed in from Excel you should declare it as either _FP12 or ::FP12, but not xll::FP. Likewise when you return this data type.

Here is an example of calling std::unique from the C++ standard library.

C++
AddIn xai_array_unique(
	Function(XLL_FP, L"?xll_array_unique", L"ARRAY.UNIQUE")
	.Arg(XLL_FP, L"Array", L"is an array of numbers.")
	.Category(L"STL")
	.FunctionHelp(L"Remove consecutive duplicates from Array.")
);
_FP12* WINAPI
xll_array_unique(const _FP12* pa)
{
#pragma XLLEXPORT
	static xll::FP12 a; // static so a.get() exists after return

	a = *pa; // copy to a
	double* pe = std::unique(a.begin(), a.end());
	a.reshape(pe - a.begin(), 1);
	
	return a.get(); // pointer to underlying FP12 data type
}