搜索
bottom↓
回复: 2

自己写的将浮点数据转化为字符串的函数怎么在AVR上不对啊。

[复制链接]

出0入0汤圆

发表于 2009-5-4 22:25:39 | 显示全部楼层 |阅读模式
这几天自己动手拿MEGA16做个简单的4功能计算器,在数据计算完后为double型的,想把这个double转化为字符串,下午自己写了个程序,在TC2.0上运行的挺好,但是拿到ICC里 出来的数字总是不着边际,心急火燎上来发个帖子求助高手。
函数如下:
void TrFtoA(double DataF,unsigned char str[],unsigned char n) //双精度数据DataF,目标字符串str[],小数点的位数n最大为8位
{
        unsigned long int lengthZ,lengthX,Temp1,Temp2;
        double Temp3,Temp4,Data;
        unsigned char linshi2,linshi[20];
        short int i,j,sign;

        if(DataF < 0)
        {
                Data = 0 - DataF;
                sign = 1;
        }
        else
        {
                Data = DataF;
                sign = 0;
        }

        Temp2 = (unsigned long) Data;
        for(lengthZ = 1;Temp2 >= 10;lengthZ ++)
                Temp2 = Temp2 / 10;

        Temp3= Data;
        Temp1 = Temp2 = (unsigned long int)        Data;

        for(lengthX = 0; ;)
        {
                if(lengthX == n)
                        break;
                Temp4 = (Temp3 * 10);
                Temp1 = (Temp4);
                if((Temp1 - (Temp2 * 10)) == 0)
                        break;
                else
                {
                        Temp3 = Temp3 * 10;
                        Temp2 = (unsigned long int)Temp3;
                        lengthX ++;
                }
        }

        Temp1 = Temp1;
        for(i = 0;Temp1 >= 10;i ++)
        {
                linshi = ((Temp1 % 10) + 48);
                Temp1 = Temp1 / 10;
        }

        j = i;
        linshi = Temp1 + 48;
        linshi[i+1] = '\0';

        for(i = 0;j >= 0;i ++)
        {
                linshi2 = linshi[j --];
                str = linshi2;
        }
             str = '\0';

        for(i = (lengthZ + lengthX + 2); i >= lengthZ;i --)
        {
                if(i ==lengthZ)
                {
                        j = str;
                        str = 0x2E;
                }
                else
                {
                        str = str[i - 1];
                }
        }

        if(sign == 1)
        {
                for(i = (lengthZ + lengthX + 3); i >= 0;i --)
                {
                        if(i == 0)
                        {
                                str = 0x2D;
                        }
                        else
                        {
                                str = str[i - 1];
                        }
                }
        }

}


如果有高手那里有现成的浮点转换为字符串和字符串转化为浮点的函数最好也给发下。 感激不尽啊。。。

出0入296汤圆

发表于 2009-5-4 23:13:17 | 显示全部楼层
ICC支持64bit的double……你开启了么?
ICC有一个函数叫做ftoa,下面是使用方法……
使用前需要包含头文件stdlib.h

char *ftoa(float f, int *status)
converts a floating-point number to the its ASCII representation. It returns a static buffer of approximately 15 chars. If the input is out of range, *status is set to the constant, _FTOA_TOO_LARGE or _FTOA_TOO_SMALL, defined in stdlib.h, and 0 is returned. Otherwise, *status is set to 0 and the char buffer is returned. This version of the ftoa is fast but cannot handle values outside of the range listed. Please contact us if you need a (much) larger version that handles greater ranges.

As with most other C functions with similar prototypes, *status means that you must pass the address of a variable to this function. Do not declare a pointer variable and pass it without initializing its pointer value.

出0入0汤圆

 楼主| 发表于 2009-5-5 13:54:52 | 显示全部楼层
谢谢 傻孩子了,不过这个函数好像有点误差。
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-5-5 00:10

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表