const{createApp}=Vue const{createVuetify,useGoTo,useDisplay}=Vuetify var data={alert:{show:false,color:'success',text:'',timeout:0,},theme:{dark:false,},nav:{showDrawer:false,showTOC:true,tocPanel:0,tab:'account',post:{discussionId:33,currentPage:1,targetPage:1,total:1,anchor:0,goToOptions:{container:null,duration:0,easing:'easeInOutCubic',offset:-100,},worker:null,task:[],active:[],apiLock:[],originLike:new Map([]),},related:{block:1,}},search:{width:80,text:null,loading:false,},tags:[{id:17,url:'/t/2506260162497c407b741e087f02',name:'Simulink基础教程',color:'#607D8B',icon:'mdi-book-open',},{id:31,url:'/t/37002d3762497c427d743e3e0121',name:'Product模块',color:'#3F51B5',icon:'mdi-multiplication',},],posts:[{id:33,num:0,uid:1,content:'\u003Ch2\u003EC语言中的除零\u003C/h2\u003E\u003Ch3\u003E浮点数除零\u003C/h3\u003E\u003Cp\u003E在C语言中,当浮点数(float和double类型)除法的除数为零时,结果会返回无穷大(infinity),而不是导致程序崩溃。\u003C/p\u003E\u003Cp\u003E这是因为IEEE 754浮点数标准定义了对无穷大和NaN(Not a Number)的处理方式。\u003C/p\u003E\u003Cp\u003E使用Simulink的\u003Ca href\u003D\"https://simulink.wiki/t/2d65182b62497c437a74485c0706\" rel\u003D\"nofollow\"\u003EEmbeded Coder\u003C/a\u003E生成代码,会自动处理浮点数除零的情况。\u003C/p\u003E\u003Ch3\u003E整数除零\u003C/h3\u003E\u003Cp\u003E在C语言中,整数除以零是未定义行为(Undefined Behavior)。\u003C/p\u003E\u003Cp\u003E这意味着编译器和运行时环境可以以任何方式处理这种情况,包括但不限于以下几种:\u003C/p\u003E\u003Col\u003E\u003Cli data-list\u003D\"bullet\"\u003E\u003Cspan class\u003D\"ql-ui\" contenteditable\u003D\"false\"\u003E\u003C/span\u003E程序崩溃或硬件异常:特别是在嵌入式系统,最常见的情况是程序会立即崩溃,并产生一个运行时错误,例如段错误(Segmentation Fault)或浮点异常(Floating Point Exception)。\u003C/li\u003E\u003Cli data-list\u003D\"bullet\"\u003E\u003Cspan class\u003D\"ql-ui\" contenteditable\u003D\"false\"\u003E\u003C/span\u003E未定义结果:在某些情况下,程序可能不会崩溃,但会产生未定义的结果。这种行为是不可预测的,可能会导致程序逻辑错误或其他意外行为。\u003C/li\u003E\u003Cli data-list\u003D\"bullet\"\u003E\u003Cspan class\u003D\"ql-ui\" contenteditable\u003D\"false\"\u003E\u003C/span\u003E编译器优化:现代编译器可能会在编译时检测到除零操作,并生成警告或错误。在某些情况下,编译器可能会优化掉这部分代码,导致程序行为不符合预期。\u003C/li\u003E\u003C/ol\u003E\u003Cp\u003E\u003Cbr\u003E\u003C/p\u003E\u003Ch2\u003E如何检测到除零\u003C/h2\u003E\u003Ch3\u003E仿真中除零检测\u003C/h3\u003E\u003Cp\u003E整数类型除以零会触发“溢出时绕回”诊断警告,提示“出现除以零。商已饱和。”\u003C/p\u003E\u003Cp\u003E浮点数类型除以零默认不会警告,通过Simulink\u003Cstrong\u003E模型设置\u003C/strong\u003E-\u003Cstrong\u003E诊断\u003C/strong\u003E-\u003Cstrong\u003E数据有效性\u003C/strong\u003E,开启“\u003Cstrong\u003E模块输出Inf或NaN\u003C/strong\u003E”,可以配置为触发警告或错误。\u003C/p\u003E\u003Ch3\u003E生成代码除零检测\u003C/h3\u003E\u003Cp\u003E通过模型顾问(Model Advisior)的模型检查功能实现\u003C/p\u003E\u003Cp\u003EMAB、JMAAB中都有“\u003Cstrong\u003E检查Simulink中是否存在除以零的情况\u003C/strong\u003E”检查项\u003C/p\u003E\u003Cp\u003E通常在模型层面就应该发现并解决除零问题,当然,在后续的静态代码检查阶段,利用QAC(Quality Assurance Compiler)或PolySpace的Bug Finder和Coder Prover依然可以检测出除零问题\u003C/p\u003E\u003Cp\u003E\u003Cbr\u003E\u003C/p\u003E\u003Ch2\u003E代码生成编译器优化\u003C/h2\u003E\u003Cp\u003ESimulink生成代码默认会对整数除零保护进行优化,例如unit8类型除以0生成的代码中会直接赋值为MAX_uint16_T,即65535。\u003C/p\u003E\u003Cp\u003E这虽然可以防止程序直接崩溃,但依然会导致程序非预期的行为。\u003C/p\u003E\u003Cp\u003E在Simulink\u003Cstrong\u003E模型设置\u003C/strong\u003E-\u003Cstrong\u003E代码生成\u003C/strong\u003E-\u003Cstrong\u003E优化\u003C/strong\u003E-\u003Cstrong\u003E高级参数\u003C/strong\u003E,可以设置开启“\u003Cstrong\u003E删除防止除法算术异常的代码\u003C/strong\u003E”,此时代码生成编译器不再添加整数类型除零保护代码,遇到整数除零BUG程序直接崩溃。\u003C/p\u003E\u003Cp\u003E\u003Cbr\u003E\u003C/p\u003E\u003Ch2\u003E如何正确处理除零\u003C/h2\u003E\u003Cp\u003ESimulink生成代码的除零保护只能避免程序直接崩溃,依然可能导致程序出现非预期的行为。\u003C/p\u003E\u003Cp\u003E模型设计阶段,在使用Product模块(Divide模块)进行除法运算时,应提前考虑除数为零的情况,并进行针对设计。\u003C/p\u003E\u003Cp\u003E此外,使用\u003Ca href\u003D\"https://simulink.wiki/t/593b1e0b62497c427974373e5f07\" rel\u003D\"nofollow\"\u003ESaturation模块\u003C/a\u003E限制涉及除法运算的局部运算结果的上、下限值可以一定程度缓解除零导致的数值急剧增大导致的异常,尤其是当除零只是瞬态时。\u003C/p\u003E\u003Ch3\u003E滑移率计算除零处理示例\u003C/h3\u003E\u003Cp\u003E在ABS/ESC控制器开发中,滑移率是非常关键的参数,其计算公式为:\u003C/p\u003E\u003Cp\u003E(车速-轮速)/车速×100%\u003C/p\u003E\u003Cp\u003E当车速为零时,上述计算会面临除零情况,鉴于车速很低时已经无需ABS介入,我们可以先判断车速是否大于某个阈值,例如1km/h,当车速大于阈值时使用公式计算滑移率,当车速小于阈值时,直接输出默认值,例如0%\u003C/p\u003E\u003Cp\u003E通过\u003Ca href\u003D\"https://simulink.wiki/t/2501291d62497c42787408164434\" rel\u003D\"nofollow\"\u003ESwitch模块\u003C/a\u003E可以实现上述功能,我们在Switch模块后端再添加一个Saturation模块,限制滑移率输出范围为0~1,进一步提高模型生成代码的健壮性。\u003C/p\u003E',ipRegion:'',updatedByUid:1,createdAt:'2025-04-20 17:17:55',updatedAt:'2025-04-20 17:36:58',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},],usersMap:new Map([[1,{uid:1,url:'/u/5b180a1c62497c417c6940494325070c',avatar:'/a/5b180a1c62497c417c6940494325070c',username:'Simulink',}],]),related:[{title:'Simulink Sum of Elements,Simulink Sum of Elements模块',url:'/d/041d2b3162497c417c69415404660778462a0027',},{title:'Simulink Sum模块上下翻转,Simulink Sum模块输入的位置,Simulink Sum里面加减号位置',url:'/d/0d60273e62497c417c6941540466067824325920',},{title:'Simulink Sum方向,Simulink Sum模块怎么旋转,Simulink Sum模块怎么改变方向',url:'/d/0121380562497c417c694154046605781e4e6432',},{title:'Simulink Sum 模块简介',url:'/d/59030a0a62497c417c694154046604783539463e',},{title:'Simulink Gain模块旋转,Simulink Gain怎么变方向',url:'/d/26172f0b62497c417c69415404610d7818177135',},{title:'Simulink Unary Minus模块简介',url:'/d/5b18031f62497c417c69415404610378033a5f36',},{title:'Simulink Gain模块作用,Simulink Gain模块参数设置,Simulink Gain模块设置变量,Simulink Gain模块怎么设置kp',url:'/d/161f343c62497c417c694154046101782d2e663d',},{title:'Simulink Gain模块简介',url:'/d/013d094262497c417c694154046100780e0f645d',},{title:'Simulink Divide,Simulink Divide模块,Simulink Divide block',url:'/d/3937204a62497c417c6941540461067811164200',},{title:'Simulink Product除法,Simulink Product模块除法,Simulink Product */',url:'/d/2e61021c62497c417c6941540461057830197808',},{title:'Simulink Product模块作用,Simulink Product怎么用,Simulink Product用法,Simulink Product模块用法',url:'/d/37045c2262497c417c69415404610478363b7f34',},{title:'Simulink Product 模块简介',url:'/d/3518091962497c417c69415404600d78364a6e37',},{title:'Simulink Data Type Conversion模块的2个典型应用场景',url:'/d/3927263c62497c417c69415404600c781c345110',},{title:'Simulink Data Type Conversion模块简介',url:'/d/5e060b2462497c417c69415404600378072e551d',},{title:'给Simulink Constant模块赋值的3类方法',url:'/d/2b15364162497c417c694154046002782c28472a',},{title:'Simulink Constant模块简介',url:'/d/391f041962497c417c6941540460007804156536',},{title:'Simulink From怎么找Goto',url:'/d/181a224162497c417c69415404600578324c0434',},{title:'Simulink Goto模块颜色,Simulink Goto高亮,Simulink Goto品红色阴影边框、青色背景怎么恢复颜色',url:'/d/1835252862497c417c694154046004782d4c710e',},{title:'Simulink Goto Tag Visibility模块,Simulink Goto模块 作用域,Simulink Goto模块 local',url:'/d/1c2b581362497c417c69415404630d782c094f16',},{title:'Simulink Goto怎么用,Simulink Goto模块设置,Simulink Goto模块怎么用',url:'/d/0828581962497c417c694154046303784601642b',},],} const App={setup(){const goTo=useGoTo() const{mdAndUp}=useDisplay() return{goTo,mdAndUp}},data(){return data;},mounted(){const themeDark=localStorage.getItem("themeDark") if(themeDark!==null){this.theme.dark=JSON.parse(themeDark)} if(this.nav.post.total>(this.nav.post.currentPage-1)*100+20){let moreLen=100 if(this.nav.post.total({id:null,num:(this.nav.post.currentPage-1)*100+v,uid:null,content:null,ipRegion:null,updatedByUid:null,createdAt:null,updatedAt:null,mentionNum:null,mentionedBy:null,mentionUsers:null,likeUsers:null,})) this.posts.push(...morePosts.slice(20))} this.workerStart() const hash=window.location.hash const match=hash.match(/#(\d+)/) if(match){const n=parseInt(match[1],10) if(n>=(this.nav.post.currentPage-1)*100&&n{this.jumpTo(n)})}} this.$nextTick(()=>{this.addHeadingIds() tocbot.init({tocSelector:'.toc',contentSelector:'#post-content-0',headingSelector:'h2, h3, h4',headingsOffset:100,scrollSmoothOffset:-100,scrollSmooth:true,collapseDepth:6,onClick:function(e){setTimeout(()=>{history.replaceState(null,'',window.location.pathname+window.location.search)},0)},}) tocbot.refresh()});},beforeUnmount(){this.workerStop() if(this.quill){this.quill.destroy() this.quill=null}},computed:{dposts(){return this.posts.slice(20);},},created(){},methods:{successAlert(msg){this.alert={show:true,color:'success',text:msg,timeout:1500,}},failureAlert(msg){this.alert={show:true,color:'error',text:msg,timeout:5000,}},flipThemeDark(){this.theme.dark=!this.theme.dark localStorage.setItem("themeDark",JSON.stringify(this.theme.dark))},toSearch(){if(!this.search.text){this.failureAlert('搜索词不能为空') return} let keywords=this.search.text.trim() if(keywords.length<1){this.failureAlert('搜索词不能为空') return} if(keywords.length>100){this.failureAlert('搜索词过长') return} this.doSearch(keywords)},toReg(){window.location.href="/reg"},toLogin(){window.location.href="/login"},toPage(){let url=window.location.href url=url.replace(/(\/\d+)?(#[0-9]+)?$/,this.nav.post.targetPage>1?`/${this.nav.post.targetPage}`:'') window.location.href=url},toLoadRelated({done}){if(this.my&&this.my.uid){this.apiLoadRelated({done})}else{done('ok')}},workerStart(){this.nav.post.worker=setInterval(()=>{this.workerLoad()},500);},workerStop(){if(this.nav.post.worker){clearInterval(this.nav.post.worker);this.nav.post.worker=null;}},async jumpTo(num){const page=Math.floor(num/100)+1 const i=num-(page-1)*100 if(page===this.nav.post.currentPage){this.goTo("#post-"+num,this.nav.post.goToOptions) if(!this.posts[i].id){const block=Math.floor(num/20)+1 this.nav.post.apiLock[block]=true await this.apiLoadPosts(block) this.$nextTick(()=>{this.goTo("#post-"+num,this.nav.post.goToOptions)})}}else{let url=window.location.href url=url.replace(/(\/\d+)?(#[0-9]+)?$/,page>1?`/${page}`:'') url=url+"#"+num window.location.href=url}},postIntersect(num){return(isIntersecting,entries,observer)=>{if(isIntersecting){this.nav.post.task.push(num) this.nav.post.active.push(num) this.nav.post.active=this.nav.post.active.filter(item=>Math.abs(item-num)<=5) this.nav.post.active.sort((a,b)=>a-b)}else{this.nav.post.active=this.nav.post.active.filter(item=>item!==num)} if(this.nav.post.active[0]){this.nav.post.anchor=this.nav.post.active[0]}else{this.nav.post.anchor=0}}},async apiLoadPosts(block){try{const response=await axios.post('/fapi/v1/post/block/'+block,{discussionId:this.nav.post.discussionId,}) if(response.data.code===0){response.data.data.posts.forEach(post=>{const i=post.num%100 Object.assign(this.posts[i],post)}) response.data.data.users.forEach(user=>{this.usersMap.set(user.uid,user)})}else{this.failureAlert('回帖数据加载失败: '+response.data.msg)}}catch(error){this.failureAlert('回帖数据加载失败: '+error)} this.nav.post.apiLock[block]=false},workerLoad(){while(this.nav.post.task.length){const num=this.nav.post.task.pop() const i=num-(this.nav.post.currentPage-1)*100 if(!this.posts[i].id){const block=Math.floor(num/20)+1 if(!this.nav.post.apiLock[block]){this.nav.post.apiLock[block]=true this.apiLoadPosts(block)}}}},getTimeInfo(t){if(!t){return ""} const now=new Date();const then=new Date(t);const diff=now-then;const minute=60*1000;const hour=minute*60;const day=hour*24;const month=day*30;const year=month*12;if(diffpost.num===num) if(!post){return "#"+num} const uid=post.uid const username=this.usersMap.get(uid)?.username if(!username){return "#"+num} return username},getUsernameByPostId(id){const post=this.posts.find(post=>post.id===id) if(!post){return "#"+this.getPostNumByPostId(id)} const uid=post.uid const username=this.usersMap.get(uid).username if(!username){return "#"+this.getPostNumByPostId(id)} return username},getPostNumByPostId(id){const post=this.posts.find(post=>post.id===id) return post.num},getPostById(id){const post=this.posts.find(post=>post.id===id) return post},getPostByNum(num){const post=this.posts.find(post=>post.num===num) return post},getAvatarByUid(uid){const avatar=this.usersMap.get(uid)?.avatar if(!avatar){return this.getRandomAvatar()} return avatar},getAvatarByPostNum(num){const post=this.posts.find(post=>post.num===num) if(!post){return this.getRandomAvatar()} const uid=post.uid return this.getAvatarByUid(uid)},getRandomAvatar(){const num=Math.floor(Math.random()*100) return "https://randomuser.me/api/portraits/men/"+num+".jpg"},getUrlByUid(uid){const url=this.usersMap.get(uid)?.url if(!url){return ""} return url},getTextByPostNum(num){const post=this.posts.find(post=>post.num===num) if(!post||!post.content){return '点击跳转到#'+num+'查看'} const parser=new DOMParser() const doc=parser.parseFromString(post.content,'text/html') const text=doc.body.textContent||'' return text.slice(0,100)},addHeadingIds(){const content=document.getElementById('post-content-0') if(!content){this.nav.showTOC=false return} const headings=content.querySelectorAll('h2, h3, h4') headings.forEach((heading,index)=>{if(!heading.id){heading.id=`toc-nav-${index}`}}) if(headings.length==0){this.nav.showTOC=false}},async doSearch(keywords){this.search.loading=true try{const response=await axios.post('/fapi/v1/search',{keywords:keywords,}) if(response.data.code===0){if(response.data.data.hash&&response.data.data.hash.length===32){window.location.href="/s/"+response.data.data.hash}else{this.failureAlert('搜索失败: 搜索服务异常')}}else{this.failureAlert('搜索失败: '+response.data.msg)}}catch(error){this.failureAlert('搜索失败: '+error)} this.search.loading=false},debounce(fn,delay){let timer=null return function(...args){if(timer)clearTimeout(timer) timer=setTimeout(()=>{fn.apply(this,args)},delay);};},},watch:{'nav.post.targetPage':{handler:async function(newV,oldV){this.toPage()},immediate:false},},} const vuetify=createVuetify({defaults:{global:{ripple:true,},},}) const app=createApp(App) app.use(vuetify).mount("#app")