1. 程式人生 > 實用技巧 >float和double資料EEPROM儲存

float和double資料EEPROM儲存

一、float和double的儲存結構

1、參考部落格

https://blog.csdn.net/qq_36206070/article/details/89683848
https://blog.csdn.net/qq_36206070/article/details/89683848

http://blog.sina.cn/dpool/blog/s/blog_8a18c33d01013bke.html

https://blog.csdn.net/qq_36414647/article/details/98372398

二、浮點數存入EEPROM

http://www.openedv.com/posts/list/64691.htm

#include<stdio.h>
#include<stdlib.h>
union data
{
    unsigned char data[8];
    double result;
}test;
int main(void)
{
    unsigned char *p,i;
    float f1 = 123.123f;
    double d1 = 123.123;
    int x1 = 0x1234;    
    /*利用指標資料拆分變數*/
    unsigned char d[8] = {23,217,206,247,83,67,65,64
}; unsigned char *p1 = NULL; double a; /*檢視位元組及精度*/ /*float:4位元組;double:8位元組*/ /*float精度:7~8位有效數字;double精度:16~17位有效數字*/ printf("float(sizeof):%d,double(sizeof):%d\n",sizeof(f1),sizeof(d1)); printf("f1 = %f,d1=%f\n",f1,d1); /*利用聯合體檢視資料的拆分*/ test.result = 34.526; printf(
"%f\n",test.result); p = test.data; for(i=0;i<8;i++) { printf("%d\n",*p); p++; } /*利用指標檢視資料的拆分*/ p1 = d; a = *((double *)(p1+8*0)); //b = *((double *)(p1+8*1)); printf("%f\n",a); system("pause"); return 0; }