草庐IT

float_val

全部标签

swift - 从字节数据中提取 float

我需要从二进制文件格式中提取一些listmodebigEndianFloat数据。我最初使用以下方法并且效果很好,但我无法将其转换为Swift3...原文:(示例20行15列数据,从300字节的标题开始)letfileData:NSData=NSData(contentsOfFile:fileInfo.path)!letptr=UnsafePointer(fileData.bytes)letbyteOffset=300foriin0..(ptr+byteOffset2)[p].bigEndianletf=Float._fromBitPattern(bits)eventData[p]=f

ios - swift 3 : How to access the value of matrix_float3x3 in a 48-byte CFData?

我正在尝试访问此answer之后的内在矩阵.通过运行下面的指令,我能够得到一个48字节的AnyObject。,我进一步将其转换为CFData.letcamData=CMGetAttachment(sampleBuffer,kCMSampleBufferAttachmentKey_CameraIntrinsicMatrix,nil)但是,我检查了CMSampleBuffer.h中sampleBuffer的输出:/*!@constantkCMSampleBufferAttachmentKey_CameraIntrinsicMatrix@abstractIndicatesthe3x3came

ios - Swift float 减法的精度损失

我正在尝试使用Swift在我的应用程序中创建一个函数来创建float的小数版本。它现在工作得很好,除非它必须构建一个混合数字(整数和小数部分)。作为下面的示例,当我使用0.4调用函数时,它工作正常,但使用1.4时却不行,因为它有一个完整的部分(1)。似乎当我将整个部分(integerPart)减去原始数量时它会失去精度。您可以直接在playground中检查。例子:0.4->2/51.4->12/52.4->22/50.5->1/20.7->7/10etc...funcfractionize(quantity:Float)->String{letintegerPart:Float=fl

ios - 有没有一种有效的方法可以快速地将 float 舍入到最近的百分之一位置?

我想快速舍入一个float,但我发现的所有内容都告诉您将其转换为字符串。有没有办法转换float本身?示例:14.14910001->14.15 最佳答案 您可以使用“比例因子”为100的round():letx=14.14910001lety=round(100.0*x)/100.0println(y)//14.15但是你应该注意二进制float不能准确地表示一个像14.15这样的数字,所以这将给出最接近的Double14.15的近似值。 关于ios-有没有一种有效的方法可以快速地将f

ios - swift 4 : Array for all types of Ints and Floating point numbers

我有一个程序可以处理所有类型的Ints和Floating指针值类型有没有一种方法可以创建数组来保存所有这些值?我尝试通过协议(protocol)来做到这一点,并用它们扩展Int和Double但没有成功,因为Swift中的协议(protocol)均等性限制(或类似的东西)protocolArrayStructValue:Comparable,Equatable{}extensionDouble:ArrayStructValue{}extensionInt:ArrayStructValue{} 最佳答案 如果你想在一个数组中有两种不同的

ios - 将 NSNumber 转换为 Float 时的意外行为

升级到Xcode9.3(9E145)后,我的应用出现了一些意外行为。问题似乎在于将NSNumber转换为Float。为此,我使用as类型转换运算符。请参阅以下示例。letn=NSNumber.init(value:1.12)letm=NSNumber.init(value:1.00)letx=nas?Floatlety=mas?Floatletxd=nas?Doubleletz=Float(truncating:n)这里,第一次转换失败,即x==nil。第二次转换成功,使用init:truncating构造函数的Float实例化也成功,即z==1.12。将n转换为Double成功,这对

swift - 如何在 Swift 中读取 BLE 特征 float

我正在尝试连接到蓝牙LE/蓝牙智能/BLE健康设备的健康温度计服务(0x1809),如此处的官方描述:https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.health_thermometer.xml.具体来说,我正在请求来自健康温度计特征(0x2A1C)的通知,并在此处进行说明:https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=

ios - 如何制作 float View ?

我所说的floatView是自定义View,它是ScrollView的subview(或看起来是subview),ScrollView一直滚动直到它锚定在某个点上。类似的行为是UITableView的部分标题。下面附上图片我的内容View(floatView下方的View)不是表格View布局。这意味着如果我仅将tableview用于floatView,我必须将我的内容View放在1个巨大的单元格中或将其分解为多个具有不同布局的单元格。内容View将有很多动态元素,这就是为什么我不想将它放在UITableViewCell中,除非我必须这样做。我可以通过编程方式制作floatView/在

struct - 错误 : 'Int' is not convertible to '@lvalue Float'

给定以下函数:funcgreatestCommonDenominator(first:Int,second:Int)->Int{returnsecond==0?first:greatestCommonDenominator(second,first%second)}还有一个包含以下内容的结构:structFraction{varnumerator:Intvardenominator:Intfuncreduce(){letgcd=greatestCommonDenominator(numerator,denominator)self.numerator/=gcdself.denomina

iphone - Float 不可转换为 CGFloat,CGFloat 不可转换为 Float

我在XCode6beta4上使用swift时遇到问题,这让我发疯我正在IOS8中开发游戏并且我遵循了教程但我得到了这个:FloatisnotconvertibletoCGFloat然后当我重铸时作为CGFloat,我得到另一个。这是代码:overridefuncdidMoveToView(view:SKView){/*Setupyourscenehere*///physicsself.physicsWorld.gravity=CGVectorMake(0.0,-5.0)//BirdvarbirdTexture=SKTexture(imageNamed:"kirby")birdTextu