// Copyright 2008 Nanorex, Inc. See LICENSE file for details.
#include "Nanorex/Interface/NXNumbers.h"
namespace Nanorex {
/* FUNCTION: ToReal */
/** Returns a NXReal constructed from the given string. */
NXReal NXRealUtils::ToReal(const char* realChars) {
return atof(realChars);
}
/**
Returns a real constructed from the given realChars and scaleString.
The scaleString is one of:
"Y" (1024),
"Z" (1021),
"E" (1018),
"P" (1015),
"T" (1012),
"G" (109),
"M" (106),
"k" (103),
"h" (102),
"da" (101),
"d" (10-1),
"c" (10-2),
"m" (10-3),
"u" (10-6),
"n" (10-9),
"A" (10-10),
"p" (10-12),
"f" (10-15),
"a" (10-18),
"z" (10-21), or
"y" (10-24)
*/
NXReal NXRealUtils::ToReal(const char* realChars, char* scaleString) {
NXReal result = pow(10, ScaleStringToEnum(scaleString));
result *= atof(realChars);
return result;
}
/* FUNCTION: ToChar */
/**
* Puts a string representing the given NXReal with the given precision into the
* given buffer. Make sure your buffer is large enough to handle the resulting
* string.
*/
void NXRealUtils::ToChar(const NXReal& number, char* charBuffer,
int precision) {
sprintf(charBuffer, "%.*g", precision, number);
}
/* FUNCTION: ScaleStringToEnum */
/** Returns the scale for the given string. */
NXRealUtils::Scale NXRealUtils::ScaleStringToEnum(const char* scaleString) {
if (strcmp(scaleString, "Y") == 0) return YOTTA;
else if (strcmp(scaleString, "Z") == 0) return ZETTA;
else if (strcmp(scaleString, "E") == 0) return EXA;
else if (strcmp(scaleString, "P") == 0) return PETA;
else if (strcmp(scaleString, "T") == 0) return TERA;
else if (strcmp(scaleString, "G") == 0) return GIGA;
else if (strcmp(scaleString, "M") == 0) return MEGA;
else if (strcmp(scaleString, "k") == 0) return KILO;
else if (strcmp(scaleString, "h") == 0) return HECTO;
else if (strcmp(scaleString, "da") == 0) return DECA;
else if (strcmp(scaleString, "d") == 0) return DECI;
else if (strcmp(scaleString, "c") == 0) return CENTI;
else if (strcmp(scaleString, "m") == 0) return MILLI;
else if (strcmp(scaleString, "u") == 0) return MICRO;
else if (strcmp(scaleString, "n") == 0) return NANO;
else if (strcmp(scaleString, "p") == 0) return PICO;
else if (strcmp(scaleString, "f") == 0) return FEMTO;
else if (strcmp(scaleString, "a") == 0) return ATTO;
else if (strcmp(scaleString, "z") == 0) return ZEPTO;
else if (strcmp(scaleString, "y") == 0) return YOCTO;
else if (strcmp(scaleString, "A") == 0) return ANGSTROM;
else return NONE;
}
} // Nanorex::