我需要将一些额外的信息打包到浮点NaN值中.我在Python中使用单精度IEEE 754浮点数(32位浮点数).Python和NumPy如何处理这些值?
理论
如果指数位(23..30)被设置,并且至少有一个有效位被设置,则IEEE 754-2008标准似乎认为数字实际上不是数字.因此,如果我们将float转换为32位整数表示,则满足以下条件的任何内容都会变为:
i & 0x7f800000 == 0x7f800000i & 0x007fffff != 0这会让我有很多选择.但是,标准似乎说有效数字的最高位是is_quiet,应该设置为避免计算中的异常.
实际测试
Python 2.7
为了确定,我运行了一些有趣结果的测试:
import math
import struct
std_nan = struct.unpack("f4", struct.pack("I", 0x7fc00000))[0]
spec_nan = struct.unpack("f4", struct.pack("I", 0x7f800001))[0]
spec2_nan = struct.unpack("f4", struct.pack("I", 0x7fc00001))[0]
print "{:08x}".format(struct.unpack("I", struct.pack("f4", std_nan))[0])
print "{:08x}".format(struct.unpack("I", struct.pack("f4", spec_nan))[0])
print "{:08x}".format(struct.unpack("I", struct.pack("f4", spec2_nan))[0])
Run Code Online (Sandbox Code Playgroud)
这给出了:
7fc00000
7fc00001 <<< should be 7f800001
7fc00001
Run Code Online (Sandbox Code Playgroud)
这个和一些进一步的测试似乎暗示某些东西(struct.unpack?)总是设置is_quiet位.
NumPy的
我尝试使用NumPy,因为我总是可以依赖转换而不是改变一个位:
import numpy as np
intarr = np.array([0x7f800001], dtype='uint32')
f = np.fromstring(intarr.tostring(), dtype='f4') …Run Code Online (Sandbox Code Playgroud) 背景
我在NumPy数组中有很多数字消息代码,我需要快速将它们转换为字符串.我在性能方面遇到了一些问题,并希望了解为什么以及如何快速完成.
一些基准
我 - 琐碎的方法
import numpy as np
# dictionary to use as the lookup dictionary
lookupdict = {
1: "val1",
2: "val2",
27: "val3",
35: "val4",
59: "val5" }
# some test data
arr = np.random.choice(lookupdict.keys(), 1000000)
# create a list of words looked up
res = [ lookupdict[k] for k in arr ]
Run Code Online (Sandbox Code Playgroud)
字典查找占用了我咖啡休息时间的758毫秒.(我也试过,res = map(lookupdict.get, arr)但情况更糟.)
II - 没有NumPy
import random
# dictionary to use as the lookup dictionary
lookupdict = {
1: …Run Code Online (Sandbox Code Playgroud) 我正在运行带有ARM Cortex-M3(STM32F205)的裸机嵌入式系统.当我尝试使用snprintf()浮点数时,例如:
float f;
f = 1.23;
snprintf(s, 20, "%5.2f", f);
Run Code Online (Sandbox Code Playgroud)
我得到了垃圾s.格式似乎很荣幸,即垃圾是一个格式良好的字符串,包含数字,小数点和两个尾随数字.但是,如果我重复snprintf,则字符串可能会在两次调用之间发生变化.
浮点数学似乎在其他方面起作用,并且snprintf与整数一起工作,例如:
snprintf(s, 20, "%10d", 1234567);
Run Code Online (Sandbox Code Playgroud)
我使用链接器开关的newlib-nano实现-u _printf_float.编译器是arm-none-eabi-gcc.
我确实怀疑内存分配问题,因为整数打印没有任何打嗝,但浮动表现就好像它们在过程中被破坏一样.该printf系列函数调用malloc用浮漂,不是整数.
唯一不属于newlib我在此上下文中使用的代码是my _sbrk(),这是必需的malloc.
caddr_t _sbrk(int incr)
{
extern char _Heap_Begin; // Defined by the linker.
extern char _Heap_Limit; // Defined by the linker.
static char* current_heap_end;
char* current_block_address;
// first allocation
if (current_heap_end == 0)
current_heap_end = &_Heap_Begin; …Run Code Online (Sandbox Code Playgroud) 我有一个带有图形用户界面的低资源嵌入式系统。该接口需要字体数据。为了节省只读内存(闪存),需要压缩字体数据。我正在为此目的寻找算法。
要压缩的数据的属性
对压缩算法的要求
结论和想法
问题
迄今为止最好的算法
只是为了提供一些背景信息,我能够找出的最有用的算法如下:
该数据解压速度很快,因为可以通过较小的(243 x 3 = 729 个八位字节)查找表将 base-3 值解码为 base-4 值。压缩率高度依赖于字体大小,但使用我的典型数据,我可以得到大约 1:2。由于这比 LZ 变体(大约为 …
embedded ×2
numpy ×2
python ×2
python-2.7 ×2
algorithm ×1
arm ×1
c ×1
compression ×1
newlib ×1
printf ×1