0

I have some code that currently uses NSArrays to save data to a file. The file size is too large for my needs so I wanted to be able to write an array of doubles to a file while possibly also pruning some of the insignificant bits.

var arr: [[Float]] = []
arr.append([Double(1.0), Double(2.0), Double(3.0)]
arr.append([Double(4.0), Double(5.0), Double(6.0)]

if let outputStream = OutputStream(url: getFileUrl(), append: true) {
    outputStream.open()
    for sub_array in arr {
         for item in sub_array {
             outputStream.write(one_quarter_of_bits(item))
         }
    }
    outputStream.close()
}

Above is the psuedo-code. It is an important design goal that the file be as small as possible including dropping some of the insignificant bits.

Victor 'Chris' Cabral
  • 2,135
  • 1
  • 16
  • 33
  • You can do that custom math, but you would have to implement it yourself. You can read the mantissa and exponent of the `Float`, round them to whatever makes sense for your application, and then combine them into a `Data` instance. – Alexander May 25 '18 at 21:31
  • If I understand you correctly, you want to convert an array of Doubles (64-bit) to half-precisions (16-bit)? Then see this question: https://stackoverflow.com/questions/32959278/convert-half-precision-float-bytes-to-float-in-swift – Code Different May 25 '18 at 22:48
  • @alexander, if i write an nsdata object to file, does it write the raw bits or a binary representation of the object. It seemes like the latter when i read the docs and from practice. i want to write the smallest files possible. – Victor 'Chris' Cabral May 28 '18 at 00:04
  • It depends, what serialization format are you using, exactly? – Alexander May 28 '18 at 01:25
  • @alexander was used nested nsmutablearrays of nsmutablearrays and using the write method. https://developer.apple.com/documentation/foundation/nsdata/1415134-write – Victor 'Chris' Cabral May 28 '18 at 02:32

0 Answers0