# 1.8.1 Numeric Data

This section gives examples of working with numeric data in Origin C. Numeric data can be stored in variables of the following data types:

1. double
2. integer
3. vector
4. matrix

Numeric data and strings can be stored in the nodes of a tree, provided the nodes have one of the data types above.

Note:Values such as 0.0, NANUM (missing value) and values between -1.0E-290 to 1.0E-290 will be evaluated to be False in logic statement.

## Missing Values

As important as numeric data is, it is also important to be able to represent missing data. Origin C defines the NANUM macro for comparing and assigning values to missing data. Missing values are only supported with the double data type.

double d = NANUM;
if( NANUM == d )
out_str("The value is a missing value.");

Origin C also provides the is_missing_value function for testing if a value is a missing value.

if( is_missing_value(d) )
out_str("The value is a missing value.");

## Precision and Comparison

In the following example code, the prec and round functions are used to control the precision of double type numeric data. The is_equal function is used to compare two pieces of double type numeric data.

double dVal = PI; // PI defined as 3.1415926535897932384626

// convert the double value to have 6 significant digits
int nSignificantDigits = 6;
printf("%f\n", prec(dVal, nSignificantDigits));

// force the double value to only have two decimal digits
uint nDecimalPlaces = 2;
double dd = round(dVal, nDecimalPlaces);
printf("%f\n", dd);

// compare two double values
if( is_equal(dd, 3.14) )
{
out_str("equal\n");
}
else
{
out_str("not equal\n");
}

## Convert Numeric to String

// assign int type numeric to string
string str = 10;
out_str(str);

int nn = 0;
str = nn;
out_str(str);

// convert double type numeric to string
double dd = PI;
str = ftoa(dd, "*"); // Use "*" for Origin's global setting in Options dialog
out_str(str);

str = ftoa(dd, "*8"); // Use "*8" for 8 significant
out_str(str);

## Vector

// One-Dimensional array with basic data type, for example, double, int, string,
// complex.
vector vx, vy;

int nMax = 10;
vx.Data(1, nMax, 1); // assign value to vx from 1 to 10 with increment 1
vy.SetSize(nMax); // set size(10) to vy

for(int nn = 0; nn < nMax; nn++)
{
vy[nn] = rnd(); // assign random data to each item in vy
printf("index = %d, x = %g, y = %g\n", nn+1, vx[nn], vy[nn]);
}
// Access the data in a worksheet window
Worksheet wks = Project.ActiveLayer();
Column col(wks, 0);

vector& vec = col.GetDataObject();
vec = vec * 0.1; // Multiply 0.1 by each piece of data in vec

vec = sin(vec);	// Find the sine of each piece of data in vec

## Matrix

// Two-Dimensional array with basic data type, for example, double, int, complex,
// but not string.
matrix mat(5, 6);

for(int ii = 0; ii < 5; ii++)
{
for(int jj = 0; jj < 6; jj++)
{
mat[ii][jj] = ii + jj;
printf("%g\t", mat[ii][jj]);
}
printf("\n"); // new line
}
// Access the data in matrix window
MatrixLayer ml = Project.ActiveLayer();
MatrixObject mo = ml.MatrixObjects(0);

matrix& mat = mo.GetDataObject();
mat = mat + 0.1; // Add 0.1 for the each data in matrix

## TreeNode

The Origin C TreeNode class provides several methods for constructing multi-level trees, traversing trees and accessing the value/attributes of tree nodes.

Tree tr;

// Access the value of a tree node
trName.strVal = "Jane";

tr.UserID.nVal = 10;

vector<string> vsBooks = {"C++", "MFC"};
tr.Books.strVals = vsBooks;

out_tree(tr); // output tree

## Complex

complex cc(1.5, 2.2);

cc.m_re = cc.m_re +1;
cc.m_im = cc.m_im * 0.1;

out_complex("cc = ", cc); // output cc = 2.500000+0.220000i
// Access complex dataset
Worksheet wks = Project.ActiveLayer();
Column col(wks, 1);
if( FSI_COMPLEX == col.GetInternalDataType() )
{
vector<complex>& vcc = col.GetDataObject();
vcc[0] = 0.5 + 3.6i;
}
// Access complex matrix
MatrixLayer ml = Project.ActiveLayer();
MatrixObject mo = ml.MatrixObjects();

if( FSI_COMPLEX == mo.GetInternalDataType() )
{
matrix<complex>& mat = mo.GetDataObject();
mat[0][0] = 1 + 2.5i;
}

## DataRange

The DataRange class is a versatile mechanism to get and put data in a Worksheet, Matrix or Graph window.

### Data Range in Worksheet

For a Worksheet, a data range can be specified by column/row index as one column, one row, any sub block range, one cell or entire Worksheet.

// Construct a data range on the active worksheet, all columns and rows
// from 1st row to 5th row.
Worksheet wks = Project.ActiveLayer();
int r1 = 0, c1 = 0, r2 = 4, c2 = -1;

DataRange dr;
// range name should be make sense, for example, "X", "Y",
// "ED"(Y error), "Z". If the data range is not belong to dependent
// or independent type, default can be "X".
dr.Add("X", wks, r1, c1, r2, c2);

Get data from data range to vector. DataRange::GetData supports multiple overloaded methods. For example:

vector vData;
int index = 0; // range index
dr.GetData(&vData, index);

### Data Range in Matrixsheet

For a Matrix window, the data range can be a matrix object index.

MatrixLayer ml = Project.ActiveLayer();

DataRange dr;
int nMatrixObjectIndex = 0;
dr.Add(ml, nMatrixObjectIndex, "X");

Get data from data range to matrix.

matrix mat;
dr.GetData(mat);

### Data Range in Graph

For a Graph window, the data range can be one data plot, or a sub range of one data plot.

GraphLayer gl = Project.ActiveLayer();
DataPlot dp = gl.DataPlots(); // Get active data plot

DataRange dr;
int i1 = 0; // from the first data point
int i2 = -1; // to the last data point
dp.GetDataRange(dr, i1, i2);

Get XY data from data plot to vector by data range object.

vector vx, vy;
DWORD dwRules = DRR_GET_DEPENDENT;
dr.GetData(dwRules, 0, NULL, NULL, &vy, &vx);

### Data Range Control

OriginC supports a GetN dialog interactive control to choose a data range.

#include <GetNBox.h>
// Open a dialog to choose a range from one graph data plot.
// And construct a data range object by this selection.
GETN_TREE(tr)
GETN_INTERACTIVE(Range1, "Select Range", "")
if( GetNBox(tr) ) // returns true if click OK button
{

DataRange dr;

vector vData;
int index = 0; // range index
dr.GetData(&vData, index); // The data in vData is the selected data points
}