var Nums = {};//Namespace for function working with numbers
Nums.SaveFloatPoint = function(){
	this.errVal = 0;
	if(this.constructor._TO_INSTANCES){
		this.constructor._instances.push(this);
	}
};
Nums.SaveFloatPoint.prototype.add = function(value){
	if(typeof value == "undefined"){
		this.errVal = this.tested;
		return false;
	}
	var err = (Math.abs(value) - Math.floor(Math.abs(value))) * value.signOf();
	var val = Math.floor(Math.abs(value)) * value.signOf();
	this.errVal += err;
	if(Math.abs(this.errVal) >= 1){
		val += value.signOf();
		this.errVal -= value.signOf();
	}
	return val;
};
Nums.SaveFloatPoint.prototype.test = function(value){
	this.saved = this.errVal;
	var val = this.add(value);
	this.tested = this.errVal;
	this.errVal = this.saved;
	return val;
};
Nums.SaveFloatPoint.prototype.refresh = function(){
	this.errVal = 0;
};
Nums.SaveFloatPoint._instances = [];
Nums.SaveFloatPoint._TO_INSTANCES = true;

Nums.Slide = function(from, to, step_num){
	var value = 0;
	var arr = [from + value];
	var len = to - from;
	var step = len / step_num;
	var saveFloat = new this.SaveFloatPoint();
	while(Math.abs(value) < Math.abs(len)){
		value += saveFloat.add(step);
		arr.push(from + value);
	}
	if(arr.length > step_num + 1){
		arr.pop();
		arr[arr.length - 1] = to;
	}
	return arr;
};
Nums.__hex_nums = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"];
Nums.Hex2Dec = function(hex){
	var num = 0;
	var splitted = hex.split('').invert();
	splitted.each(function(n, index){
		num += this.__hex_nums.indexOf(n) * (16).power(index);
	}.bind(this));
	return Number(num);
};
Nums.Dec2Hex = function(dec, len){
	var length = len || 1;
	var h_str = "";
	if(!dec){
		h_str = "0";
	}else{
		var num = dec;
		var ind;
		while(num){
			ind = num % 16;
			num = Math.floor(num / 16);
			var chr = Nums.__hex_nums[ind];
			h_str += chr;
		}
	}
	h_str = h_str.invert();
	while(h_str.length < length){
		h_str = "0" + h_str;
	}
	return h_str;
};
Nums.Dec2Bin = function(dec){
	if(!dec){
		return "0";
	}
	var d_str = "";
	var num = dec;
	var ind;
	while(num){
		ind = num % 2;
		num = Math.floor(num / 2);
		d_str += ind;
	}
	return d_str.invert();
};
Nums.toString = function(){
	var str = " object Nums: ";
	for(var i in this){
		if(i != "toString"){
			str += "\n";
			str += typeof this[i] + " " + i + "  :: ";
		}
	}
	return str;
};
Nums.Positive = function(val){
	this.val = 0;
	this.onzero = new DOMEvent();
	this.add(val);
};
Nums.Positive.prototype.add = function(val){
	this.val = Nums.Positive._check(this.val + val);
	if(this.val <= 0){
		this.onzero.fire();
	}
	return this.val;
};
Nums.Positive.prototype.set = function(val){
	this.val = Nums.Positive._check(val);
};
Nums.Positive.prototype.toString = function(){
	return this.val;
};
Nums.Positive._check = function(val){
	if(!val){
		return 0;
	}
	return val >= 0 ? val : 0;
};
Nums.rand = function(from, to){
	var range = to - from;
	return Math.floor(Math.random() * range) + from;
};
Nums.to_degree = function(val){
	return (val * 180) / Math.PI;
};
Nums.to_rad = function(val){
	return (val / 180) * Math.PI;
}
Nums.__loaded = true;