Swift 除法是“/"吗?操作员不工作还是我错过了什么? [英] Is the Swift divide "/" operator not working or have I missed something?

查看:55
本文介绍了Swift 除法是“/"吗?操作员不工作还是我错过了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Swift 游乐场中初始化以下分数数组

I am trying to initialise the following array of fractions in a Swift playground

var probabilitiesX = Array(repeating: Double(1/36), count: 36)

但我的输出数组是:

[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]

基本上都是零.如果我将分数输入为小数0.02777777",那么我会得到正确的输出,但是我使用了各种分数,理想情况下不想将它们全部输入.有什么想法吗?

basically all zeros. If I type in the fraction as a decimal "0.02777777" then I get the correct output however I am using various fractions and ideally do not want to have to type them all out. Any ideas?

推荐答案

您的问题在这里:1/36.136 都是 Int.试试这个:

Your problem is here: 1/36. Both 1 and 36 are Ints. Try this:

var probabilitiesX = Array(repeating: 1.0 / 36.0, count: 36)

这篇关于Swift 除法是“/"吗?操作员不工作还是我错过了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆