float f[100] ;数组。如何按照和C语言标准一样,按照6位小数位打印?
This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
以下分别是代码和执行结果。我们有效小数位是6位,一直打印4位。
void uchar_to_float_matrix(float (*dest)[col], unsigned char *src, int len) { int i, j, k; float_union float_tmp; int row = len / (4 * col); for (i = 0, k = 0; i < row; i++) { for (j = 0; j < col; j++) { float_tmp.float_byte.low_byte = src[k]; float_tmp.float_byte.mlow_byte = src[k + 1]; float_tmp.float_byte.mhigh_byte = src[k + 2]; float_tmp.float_byte.high_byte = src[k + 3]; dest[i][j] = float_tmp.float_val; k = k + 4; System_printf("[%d][%d]: %f, %f\n", i, j, dest[i][j], float_tmp.float_val); } } }
[ 237419.427] [6][1697]: 0.0001, 0.0001
[ 237419.428] [6][1698]: 0.0000, 0.0000
[ 237419.428] [6][1699]: -0.0000, -0.0000
[ 237419.429] [6][1700]: 0.0000, 0.0000
[ 237419.429] [6][1701]: 0.0003, 0.0003
[ 237419.430] [6][1702]: 0.0001, 0.0001
[ 237419.430] [6][1703]: -0.0002, -0.0002
[ 237419.431] [6][1704]: -0.0000, -0.0000
[ 237419.431] [6][1705]: -0.0004, -0.0004
[ 237419.432] [6][1706]: -0.0003, -0.0003
[ 237419.432] [6][1707]: 0.0000, 0.0000
[ 237419.433] [6][1708]: -0.0000, -0.0000
[ 237419.433] [6][1709]: 0.0001, 0.0001
[ 237419.434] [6][1710]: -0.0002, -0.0002
[ 237419.434] [6][1711]: 0.0000, 0.0000
[ 237419.435] [6][1712]: 0.0001, 0.0001
[ 237419.435] [6][1713]: 0.0002, 0.0002
[ 237419.436] [6][1714]: 0.0001, 0.0001
[ 237419.436] [6][1715]: 0.0001, 0.0001
[ 237419.437] [6][1716]: -0.0002, -0.0002
[ 237419.437] [6][1717]: 0.0000, 0.0000
[ 237419.438] [6][1718]: 0.0000, 0.0000
[ 237419.438] [6][1719]: -0.0001, -0.0001
[ 237419.439] [6][1720]: -0.0000, -0.0000
[ 237419.439] [6][1721]: -0.0001, -0.0001
请看下面e2e工程师的回复。
TI RTOS support on E2E forums is deprecated as indicated here:
Please refer to previous guidance given on this topic by Ashish on this thread and see if this helps:
Looking at the documentation for System_printf (http://rtsc.eclipse.org/cdoc-tip/xdc/runtime/System.html#printf), it looks like the precision specifier is not support for "%f" conversion specifier. If code size is not a problem then you can try the following workaround:
float val = 1.234556f; char buf[10]; /* Big enough buffer to hold digits before and after decimal point including decimal point */ char *bufPtr; System_snprintf(buf, sizeof(buf), "%f", val); bufPtr = strchr(buf, '.'); /* Find decimal point index */ bufPtr[3] = '\0'; /* Discard all but first 2 digits after decimal point */ System_printf("Val: %s\n", buf);
该方法没用,因为 System_snprintf(buf, sizeof(buf), "%f", val); 输入到buf中的小数也只有4位,所以后续转为字符串输出也只有4位小数。
另外最重要的不是只能输出4位小数,而是精度只保留到4位。
例如我把float类型的1.234567 * 100,输出后理论来讲是123.4567,但是实际上有小数只有123.45,后面的小数约等于67。
我们项目的算法要求小数位的精度是6位,而最后2位小数是近似值,这就导致出来的结果误差很大,根本就没法用!
现在打算换为double,但是double类型导致内存不够,默认DSP内存15MB。
太惨了!