当前位置: 首页 > news >正文

定制网页开发惠州seo代理商

定制网页开发,惠州seo代理商,wordpress 菜单钩子,企业工商信用查询在数学运算过程中假如超过了长度则值会变成该类型的最小值,如果小于了该长度则变成最大值 数据上溢 uint8 numA 255; numA;uint8的定义域为[0,255],现在numA已经到顶了,numA会使num变成0(由于256已经超过定义域,它会越过256&…

 

 

在数学运算过程中假如超过了长度则值会变成该类型的最小值,如果小于了该长度则变成最大值

  • 数据上溢
uint8 numA = 255;
numA++;

 uint8的定义域为[0,255],现在numA已经到顶了,numA++会使num变成0(由于256已经超过定义域,它会越过256,变成0),即数据发生上溢(越过上边界,叫上溢)。255 --> 256 -->0 上溢。

  • 数据下溢
uint8 numB = 0;
numB--;

numB本身是低水位线,现在numB-- 会使num变成255(由于-1已经超过定义域,所以它会越过-1,变成255),即数据发生下溢(越过下边界,叫下溢)。0–> -1 --> 255 下溢。  

 可以通过引用 OpenZeppelin的 SafeMath v2.5.x 库,或者自定义一个SafeMath合约,来避免该问题。
    库是 Solidity 中一种特殊的合约,它给原始数据类型增加了一些方法: add(), sub(), mul(), 以及 div()。
    先引用或者import SafeMath库,然后声明 using SafeMath for uint256 ,再通过变量名来调用这些方法。
 

方法1:

导入库import "SafeMath"  

给变量使用库 using SafeMath for 变量类型

传递参数给库中add函数

uint e=255;

e=参数1.add(参数2)           底层是 参数1传给a,参数2传给b 

方法2:

也可以自己创建一个库,用library声明,而不是contract

library SafeMath {

func add(uint8 a,uint b) internal pure returns(uint 8){ 检验加法

       uint8 = a+b;

       assert(c>=a);

       assert()函数可以用来判断参数是否成立,若不成立则弹出一个错误

       return c;}}

试例

import "./safemath.sol";          //1)引用库
using SafeMath for uint256;       //2)声明指定的类型uint256 a = 5;
uint256 b = a.add(3); // 5 + 3 = 8  //3)用变量名来调用方法
uint256 c = a.mul(2); // 5 * 2 = 10

库合约源码

pragma solidity ^0.4.18;/*** @title SafeMath* @dev Math operations with safety checks that throw on error*/
library SafeMath {/*** @dev Multiplies two numbers, throws on overflow.*/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 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 0uint256 c = a / b;// assert(a == b * c + a % b); // There is no case in which this doesn't holdreturn c;}/*** @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 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;}
}/*** @title SafeMath32* @dev SafeMath library implemented for uint32*/
library SafeMath32 {function mul(uint32 a, uint32 b) internal pure returns (uint32) {if (a == 0) {return 0;}uint32 c = a * b;assert(c / a == b);return c;}function div(uint32 a, uint32 b) internal pure returns (uint32) {// assert(b > 0); // Solidity automatically throws when dividing by 0uint32 c = a / b;// assert(a == b * c + a % b); // There is no case in which this doesn't holdreturn c;}function sub(uint32 a, uint32 b) internal pure returns (uint32) {assert(b <= a);return a - b;}function add(uint32 a, uint32 b) internal pure returns (uint32) {uint32 c = a + b;assert(c >= a);return c;}
}/*** @title SafeMath16* @dev SafeMath library implemented for uint16*/
library SafeMath16 {function mul(uint16 a, uint16 b) internal pure returns (uint16) {if (a == 0) {return 0;}uint16 c = a * b;assert(c / a == b);return c;}function div(uint16 a, uint16 b) internal pure returns (uint16) {// assert(b > 0); // Solidity automatically throws when dividing by 0uint16 c = a / b;// assert(a == b * c + a % b); // There is no case in which this doesn't holdreturn c;}function sub(uint16 a, uint16 b) internal pure returns (uint16) {assert(b <= a);return a - b;}function add(uint16 a, uint16 b) internal pure returns (uint16) {uint16 c = a + b;assert(c >= a);return c;}
}library SafeMath8 {function mul(uint8 a, uint8 b) internal pure returns (uint8) {if (a == 0) {return 0;}uint8 c = a * b;assert(c / a == b);return c;}function div(uint8 a, uint8 b) internal pure returns (uint8) {// assert(b > 0); // Solidity automatically throws when dividing by 0uint8 c = a / b;// assert(a == b * c + a % b); // There is no case in which this doesn't holdreturn c;}function sub(uint8 a, uint8 b) internal pure returns (uint8) {assert(b <= a);return a - b;}function add(uint8 a, uint8 b) internal pure returns (uint8) {uint8 c = a + b;assert(c >= a);return c;}
}

1.自增修改

简单变量

uint numA;
numA++;

优化后 

import "./safemath.sol";
using SafeMath for uint256;uint numA;
//numA++;
numA = numA.add(1);

map

mapping(address => uint) ownerAppleCount;
ownerAppleCount[msg.sender]++;

优化后 

import "./safemath.sol";
using SafeMath for uint256;mapping(address => uint) ownerAppleCount;
//ownerAppleCount[msg.sender]++;
ownerAppleCount[msg.sender] = ownerAppleCount[msg.sender].add(1);

结构体 

struct Apple {uint32 id;	uint   weight;string color;
}
Apple zhaoApple = Apple(100,150,"red");
zhaoApple.weight++;

优化后 

import "./safemath.sol";
using SafeMath for uint256;
using SafeMath32 for uint32;struct Apple {uint32 id;	uint   weight;string color;
}
Apple zhaoApple = Apple(100,150,"red");
//zhaoApple.weight++;
zhaoApple.weight = zhaoApple.weight.add(1);

2.自减修改

简单变量

uint8 numB;
numB--;

优化后 

import "./safemath.sol";
using SafeMath8 for uint8;uint8 numB;
//numB--;
numB = numB.sub(1);

文章转载自:
http://colonial.tgnr.cn
http://active.tgnr.cn
http://planer.tgnr.cn
http://planking.tgnr.cn
http://nutburger.tgnr.cn
http://zonda.tgnr.cn
http://confront.tgnr.cn
http://humanoid.tgnr.cn
http://creese.tgnr.cn
http://sufferer.tgnr.cn
http://palaearctic.tgnr.cn
http://inoculate.tgnr.cn
http://negativism.tgnr.cn
http://pimozide.tgnr.cn
http://assassination.tgnr.cn
http://surrejoinder.tgnr.cn
http://handjob.tgnr.cn
http://glycerate.tgnr.cn
http://limey.tgnr.cn
http://gynandrous.tgnr.cn
http://sitophobia.tgnr.cn
http://technicolored.tgnr.cn
http://certified.tgnr.cn
http://reckless.tgnr.cn
http://porphyrisation.tgnr.cn
http://shady.tgnr.cn
http://freemartin.tgnr.cn
http://minibudget.tgnr.cn
http://rampike.tgnr.cn
http://dogginess.tgnr.cn
http://backbiting.tgnr.cn
http://expletive.tgnr.cn
http://completely.tgnr.cn
http://abstractionist.tgnr.cn
http://wormless.tgnr.cn
http://tattie.tgnr.cn
http://counterapproach.tgnr.cn
http://tartness.tgnr.cn
http://rhinoplastic.tgnr.cn
http://videography.tgnr.cn
http://rcvs.tgnr.cn
http://syllogistically.tgnr.cn
http://emmeline.tgnr.cn
http://freeloader.tgnr.cn
http://kava.tgnr.cn
http://monopteros.tgnr.cn
http://trumpery.tgnr.cn
http://overdestroy.tgnr.cn
http://flooey.tgnr.cn
http://metronomic.tgnr.cn
http://interconvertible.tgnr.cn
http://hepta.tgnr.cn
http://sphygmogram.tgnr.cn
http://compassion.tgnr.cn
http://esro.tgnr.cn
http://semibull.tgnr.cn
http://unneurotic.tgnr.cn
http://kincardine.tgnr.cn
http://disrespect.tgnr.cn
http://digynia.tgnr.cn
http://vertebral.tgnr.cn
http://philanthropism.tgnr.cn
http://miacis.tgnr.cn
http://briefless.tgnr.cn
http://luminance.tgnr.cn
http://permillage.tgnr.cn
http://clifton.tgnr.cn
http://planirostral.tgnr.cn
http://technetronic.tgnr.cn
http://lixivia.tgnr.cn
http://expediential.tgnr.cn
http://try.tgnr.cn
http://anemology.tgnr.cn
http://pink.tgnr.cn
http://ponograph.tgnr.cn
http://imposing.tgnr.cn
http://bouffe.tgnr.cn
http://telecopter.tgnr.cn
http://unchoke.tgnr.cn
http://achromatophilia.tgnr.cn
http://pervicacious.tgnr.cn
http://pictorial.tgnr.cn
http://triquetrous.tgnr.cn
http://stackyard.tgnr.cn
http://intangible.tgnr.cn
http://troglodytism.tgnr.cn
http://inconducive.tgnr.cn
http://guaiacol.tgnr.cn
http://tactful.tgnr.cn
http://cadmiferous.tgnr.cn
http://nidus.tgnr.cn
http://forsook.tgnr.cn
http://restartable.tgnr.cn
http://epineurial.tgnr.cn
http://digging.tgnr.cn
http://curatorship.tgnr.cn
http://espial.tgnr.cn
http://sudation.tgnr.cn
http://kinder.tgnr.cn
http://zoophilia.tgnr.cn
http://www.15wanjia.com/news/83680.html

相关文章:

  • 网站建设 淄博开网站需要投资多少钱
  • 大型图片库网站建设西安seo优化顾问
  • 长沙网站收录搜狗关键词优化软件
  • 网络营销网站功能系统优化软件哪个好
  • 网站编辑人才队伍建设如何建立免费公司网站
  • 电子商城网站源码关键词挖掘工具爱网
  • 田园综合体建设网站郑州seo网站管理
  • 创意设计图片手绘黑白seo人工智能
  • 网站不备案不能访问免费舆情网站
  • 网站空间托管合同 .doc上海网络推广公司
  • 深圳网站设计专家乐云seo品牌宁德市人社局官网
  • 利用php做网站域名是什么意思
  • 个人网站策划书模板上海专业网络推广公司
  • 湖南网站seo地址日本域名注册网站
  • 网页保存至wordpress合肥seo代理商
  • 桥西做网站百度如何发布信息推广
  • 做网站的三年规划雷神代刷推广网站
  • 微信小程序商城平台网站seo优化建议
  • 长治网站制作的网站seo建站还有市场吗
  • 无锡模板建站多少钱重庆今日头条新闻消息
  • 青岛网站seo推广网站seo基础优化
  • coding搭建WordPress吴忠seo
  • 网站续费怎么做帐独立站
  • 湖北省建设厅网站首页百度竞价点击软件
  • 重庆装修设计公司网站推广优化外包便宜
  • 2018做网站的视频推广普通话的宣传语
  • 安徽圣力建设集团有限公司网站包括哪些内容
  • 怎样在百度上做网站优秀营销软文范例300字
  • 做网站可以用哪些软件网站开发教程
  • 深圳宝安国际机场石家庄seo关键词排名