netlogo - 如何计算 Netlogo 中分母有时为零的百分比?
问题描述
我正在尝试将变量设置为代理集中的代理集的百分比。因此,程序有时会尝试除以零,我收到一条错误消息。
我尝试使用 ifelse 条件,以便仅在分母大于 0 时运行该位代码。但是,我仍然收到错误消息。我在以下代码中遗漏了什么吗?
ifelse any? people with [recentvent? and trait = 1]
[set %minority-affiliated-vents 100 * (count people with [groupid > 0 and recentvent? and trait = 1] / count people with [recentvent? and trait = 1])]
[set %minority-affiliated-vents 0]
我仍然得到零运行时错误的除法。
解决方案
我看不出你的代码有什么问题。作为一般方法,您可以按如下方式修改您的代码 - 这减少了您需要构建代理集的次数,但也确保您不会意外地在测试和用作分母之间以不同的方式构建它。它也更容易阅读。
let test-agentset people with [recentvent? and trait = 1]
ifelse any? test-agentset
[set %minority-affiliated-vents 100 * count test-agentset with [groupid > 0] / count test-agentset]
[set %minority-affiliated-vents 0]
通过这种构造,您可以看到分母不能为零,因此错误必须在代码中的其他位置。
推荐阅读
- mysql - NodeJS - 获取新创建的 ID 后更新 MySQL 字段
- android - 删除 jcenter() 后如何解决 Android Studio 4.2 构建错误?
- asp.net-core-mvc - 在 ASP.NET Core 中注册具有特定角色的第一个用户(如何修改 AccountController 操作)
- c - 为什么 fread 和 fwrite 给出错误“包含太多参数”?
- tensorflow - 在 tensorflow 分词器中获取词汇量的大小
- unit-testing - Xamarin Forms System.MissingMethodException:'找不到方法'System.String [] System.String.Split(Char,System.StringSplitOptions)'
- api - Slack 工作区中用户之间交换的消息数量?
- excel - 减去月、周和日的公式
- bash - 如何通过 pdftk 管道生成 pdf 输出以删除注释?
- amazon-web-services - EMR 生成文件的 Spark Kryo 反序列化在本地失败