pragma solidity ^0.4.18; /** * @title SafeMath * @dev Math operations with safety checks that throw on error */ library SafeMathPrecision { struct Precision { uint8 p; } function setPrecision(Precision storage self, uint8 p) internal { self.p=p; } function convertFromNoPrecision(Precision storage self, uint256 v) internal view returns(uint256){ return mul(v, 10**uint256(self.p)); } function convertToNoPrecision(Precision storage self, uint256 v) internal view returns(uint256){ return div(v, 10**uint256(self.p)); } function mul(uint256 a, uint256 b) internal pure returns (uint256) { if (a == 0) { return 0; } uint256 c = a * b; assert(c / a == b); return c; } /** * @dev Multiplies two numbers, throws on overflow. */ function mul(Precision storage self, uint256 a, uint256 b) internal view returns (uint256) { if (a == 0) { return 0; } uint256 c = a * b; assert(c / a == b); return div(c, 10**uint256(self.p)); } /** * @dev Integer division of two numbers, truncating the quotient. */ function div(uint256 a, uint256 b) internal pure returns (uint256) { // assert(b > 0); // Solidity automatically throws when dividing by 0 uint256 c = a / b; // assert(a == b * c + a % b); // There is no case in which this doesn't hold return c; } /** * @dev Integer division of two numbers, truncating the quotient. */ function div(Precision storage self, uint256 a, uint256 b) internal view returns (uint256) { // assert(b > 0); // Solidity automatically throws when dividing by 0 uint256 c = a / b; // assert(a == b * c + a % b); // There is no case in which this doesn't hold return mul(c, 10**uint256(self.p)); } /** * @dev Substracts two numbers, throws on overflow (i.e. if subtrahend is greater than minuend). */ function sub(uint256 a, uint256 b) internal pure returns (uint256) { assert(b <= a); return a - b; } /** * @dev Substracts two numbers, throws on overflow (i.e. if subtrahend is greater than minuend). */ function sub(Precision storage self, uint256 a, uint256 b) internal pure returns (uint256) { return sub(a,b); } /** * @dev Adds two numbers, throws on overflow. */ function add(uint256 a, uint256 b) internal pure returns (uint256) { uint256 c = a + b; assert(c >= a); return c; } function add(Precision storage self, uint256 a, uint256 b) internal pure returns (uint256) { return add(a,b); } } contract SimpleStore { using SafeMathPrecision for uint; using SafeMathPrecision for SafeMathPrecision.Precision; SafeMathPrecision.Precision math; function SimpleStore(uint8 precision) public { math.setPrecision(precision); } function doMul(uint base, uint percent) external view returns (uint) { return math.mul(base, percent); } function doDiv(uint numerator, uint divisor) external view returns (uint){ return math.div(numerator, divisor); } }
0.4.18