size_t BigNumber::printTo(Print& p) const { char *buf = bc_num2str(num_); size_t len = p.write(buf); free(buf); return len; }
static Variant HHVM_FUNCTION(bcpowmod, const String& left, const String& right, const String& modulus, int64_t scale /* = -1 */) { if (scale < 0) scale = BCG(bc_precision); bc_num first, second, mod, result; bc_init_num(&first); bc_init_num(&second); bc_init_num(&mod); bc_init_num(&result); SCOPE_EXIT { bc_free_num(&first); bc_free_num(&second); bc_free_num(&mod); bc_free_num(&result); }; php_str2num(&first, (char*)left.data()); php_str2num(&second, (char*)right.data()); php_str2num(&mod, (char*)modulus.data()); if (bc_raisemod(first, second, mod, &result, scale) == -1) { return false; } if (result->n_scale > scale) { result->n_scale = scale; } String ret(bc_num2str(result), AttachString); return ret; }
static Variant HHVM_FUNCTION(bcmod, const String& left, const String& right) { bc_num first, second, result; bc_init_num(&first); bc_init_num(&second); bc_init_num(&result); php_str2num(&first, (char*)left.data()); php_str2num(&second, (char*)right.data()); if (bc_modulo(first, second, &result, 0) == -1) { raise_warning("Division by zero"); return Variant(); } String ret(bc_num2str(result), AttachString); bc_free_num(&first); bc_free_num(&second); bc_free_num(&result); return ret; }
Variant f_bcsqrt(CStrRef operand, int64 scale /* = -1 */) { if (scale < 0) scale = BCG(bc_precision); bc_num result; bc_init_num(&result); php_str2num(&result, (char*)operand.data()); Variant ret; if (bc_sqrt(&result, scale) != 0) { if (result->n_scale > scale) { result->n_scale = scale; } ret = String(bc_num2str(result), AttachString); } else { Logger::Warning("Square root of negative number"); } bc_free_num(&result); return ret; }
String f_bcmod(CStrRef left, CStrRef right) { bc_num first, second, result; bc_init_num(&first); bc_init_num(&second); bc_init_num(&result); php_str2num(&first, (char*)left.data()); php_str2num(&second, (char*)right.data()); if (bc_modulo(first, second, &result, 0) == -1) { Logger::Warning("Division by zero"); return String(); } String ret(bc_num2str(result), AttachString); bc_free_num(&first); bc_free_num(&second); bc_free_num(&result); return ret; }
static Variant HHVM_FUNCTION(bcsqrt, const String& operand, int64_t scale /* = -1 */) { if (scale < 0) scale = BCG(bc_precision); bc_num result; bc_init_num(&result); php_str2num(&result, (char*)operand.data()); Variant ret; if (bc_sqrt(&result, scale) != 0) { if (result->n_scale > scale) { result->n_scale = scale; } ret = String(bc_num2str(result), AttachString); } else { raise_warning("Square root of negative number"); } bc_free_num(&result); return ret; }
String f_bcsub(CStrRef left, CStrRef right, int64 scale /* = -1 */) { if (scale < 0) scale = BCG(bc_precision); bc_num first, second, result; bc_init_num(&first); bc_init_num(&second); bc_init_num(&result); php_str2num(&first, (char*)left.data()); php_str2num(&second, (char*)right.data()); bc_sub(first, second, &result, scale); if (result->n_scale > scale) { result->n_scale = scale; } String ret(bc_num2str(result), AttachString); bc_free_num(&first); bc_free_num(&second); bc_free_num(&result); return ret; }
String f_bcdiv(CStrRef left, CStrRef right, int64 scale /* = -1 */) { if (scale < 0) scale = BCG(bc_precision); bc_num first, second, result; bc_init_num(&first); bc_init_num(&second); bc_init_num(&result); php_str2num(&first, (char*)left.data()); php_str2num(&second, (char*)right.data()); if (bc_divide(first, second, &result, scale) == -1) { Logger::Warning("Division by zero"); return String(); } String ret(bc_num2str(result), AttachString); bc_free_num(&first); bc_free_num(&second); bc_free_num(&result); return ret; }
static String HHVM_FUNCTION(bcsub, const String& left, const String& right, int64_t scale /* = -1 */) { if (scale < 0) scale = BCG(bc_precision); bc_num first, second, result; bc_init_num(&first); bc_init_num(&second); bc_init_num(&result); php_str2num(&first, (char*)left.data()); php_str2num(&second, (char*)right.data()); bc_sub(first, second, &result, scale); if (result->n_scale > scale) { result->n_scale = scale; } String ret(bc_num2str(result), AttachString); bc_free_num(&first); bc_free_num(&second); bc_free_num(&result); return ret; }
static Variant HHVM_FUNCTION(bcdiv, const String& left, const String& right, int64_t scale /* = -1 */) { if (scale < 0) scale = BCG(bc_precision); bc_num first, second, result; bc_init_num(&first); bc_init_num(&second); bc_init_num(&result); php_str2num(&first, (char*)left.data()); php_str2num(&second, (char*)right.data()); if (bc_divide(first, second, &result, scale) == -1) { raise_warning("Division by zero"); return Variant(); } String ret(bc_num2str(result), AttachString); bc_free_num(&first); bc_free_num(&second); bc_free_num(&result); return ret; }
Variant f_bcpowmod(CStrRef left, CStrRef right, CStrRef modulus, int64 scale /* = -1 */) { if (scale < 0) scale = BCG(bc_precision); bc_num first, second, mod, result; bc_init_num(&first); bc_init_num(&second); bc_init_num(&mod); bc_init_num(&result); php_str2num(&first, (char*)left.data()); php_str2num(&second, (char*)right.data()); php_str2num(&mod, (char*)modulus.data()); if (bc_raisemod(first, second, mod, &result, scale) == -1) { return false; } if (result->n_scale > scale) { result->n_scale = scale; } String ret(bc_num2str(result), AttachString); bc_free_num(&first); bc_free_num(&second); bc_free_num(&mod); bc_free_num(&result); return ret; }
// return a pointer to a string containing the number // MUST FREE THIS after use! // eg: char * s = mynumber.toString (); // Serial.println (s); // free (s); char * BigNumber::toString () const { return bc_num2str(num_); } // end of BigNumber::toString
// Allow Arduino's Serial.print() to print BigNumber objects! size_t BigNumber::printTo(Print& p) const { char *buf = bc_num2str(num_); p.write(buf); free(buf); }